"use client"; import React, { useEffect, useState } from "react"; import { ScheduleType } from "@/types/ScheduleType"; import Spinner from "@/components/Spinner"; import PageTitle from "@/components/ui/PageTitle"; import { getScheduleForDate, scheduleUserDish, updateScheduleForDate } from "@/utils/api/scheduleApi"; import { UserDishType } from "@/types/ScheduledUserDishType"; import Label from "@/components/ui/Label"; import SectionTitle from "@/components/ui/SectionTitle"; import { useFetchUsers } from "@/hooks/useFetchUsers"; import { listUserDishes } from "@/utils/api/userDishApi"; import scheduleBuilder from "@/utils/scheduleBuilder"; import transformDate from "@/utils/dateBuilder"; import { ChevronLeftIcon } from "@heroicons/react/16/solid"; import Hr from "@/components/ui/Hr" import Button from "@/components/ui/Button" interface Props { date: string; } const ScheduleEditForm = ({ date }: Props) => { const [schedule, setSchedule] = useState() const [userDishes, setUserDishes] = useState([]) const [isScheduleLoading, setIsScheduleLoading] = useState(true); const [areUserDishesLoading, setAreUserDishesLoading] = useState(true); const { users } = useFetchUsers(); useEffect(() => { getScheduleForDate(date) .then((sched: ScheduleType) => setSchedule(sched)) .finally(() => setIsScheduleLoading(false)) }, [date]); useEffect(() => { listUserDishes() .then((user_dishes: UserDishType[]) => setUserDishes(user_dishes)) .finally(() => setAreUserDishesLoading(false)) }, []); const handleSkipDay = () => { updateScheduleForDate(date, true) .then((schedule: ScheduleType) => { setSchedule(schedule) }) } const handleUnskipDay = () => { updateScheduleForDate(date, false) .then((schedule: ScheduleType) => { setSchedule(schedule) }) } const handleChange = (e: React.ChangeEvent, userId: number) => { const userDishId = parseInt(e.currentTarget.value); if (userDishId === 0) { scheduleUserDish(date, userId, null, true).then(() => window.location.reload()); return; } scheduleUserDish(date, userId, userDishId).then(() => window.location.reload()); } if (isScheduleLoading || areUserDishesLoading || !schedule) { return } const scheduleData = scheduleBuilder(schedule, users, userDishes) return
Edit Day
{ transformDate(schedule.date) }

{ userDishes.length === 0 &&
No dishes found assigned to this user.
Go ahead and add some first, or choose to skip the day.
(dishes ={`>`} edit ={`>`} add user)
} { schedule.is_skipped ? : ( <> { scheduleData .map((scheduleData) =>
{ scheduleData.user.name }
) } ) }
Changes are saved automatically

{ schedule.is_skipped ? : }
} export default ScheduleEditForm