import { zodResolver } from "@hookform/resolvers/zod"; import dayjs from "dayjs"; import timezone from "dayjs/plugin/timezone"; import utc from "dayjs/plugin/utc"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { QueryCell } from "@lib/QueryCell"; import { DEFAULT_SCHEDULE } from "@lib/availability"; import { useLocale } from "@lib/hooks/useLocale"; import showToast from "@lib/notification"; import { inferQueryOutput, trpc } from "@lib/trpc"; import { Schedule as ScheduleType } from "@lib/types/schedule"; import Shell from "@components/Shell"; import { Form } from "@components/form/fields"; import { Alert } from "@components/ui/Alert"; import Button from "@components/ui/Button"; import Schedule from "@components/ui/form/Schedule"; dayjs.extend(utc); dayjs.extend(timezone); type FormValues = { schedule: ScheduleType; }; export function AvailabilityForm(props: inferQueryOutput<"viewer.availability">) { const { t } = useLocale(); const createSchedule = async ({ schedule }: FormValues) => { const res = await fetch(`/api/schedule`, { method: "POST", body: JSON.stringify({ schedule, timeZone: props.timeZone }), headers: { "Content-Type": "application/json", }, }); if (!res.ok) { throw new Error((await res.json()).message); } const responseData = await res.json(); showToast(t("availability_updated_successfully"), "success"); return responseData.data; }; const schema = z.object({ schedule: z .object({ start: z.date(), end: z.date(), }) .superRefine((val, ctx) => { if (dayjs(val.end).isBefore(dayjs(val.start))) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Invalid entry: End time can not be before start time", path: ["end"], }); } }) .optional() .array() .array(), }); const days = [ t("sunday_time_error"), t("monday_time_error"), t("tuesday_time_error"), t("wednesday_time_error"), t("thursday_time_error"), t("friday_time_error"), t("saturday_time_error"), ]; const form = useForm({ defaultValues: { schedule: props.schedule || DEFAULT_SCHEDULE, }, resolver: zodResolver(schema), }); return (
{t("troubleshoot_availability")}