import { useRouter } from "next/router"; import { useForm } from "react-hook-form"; import { Schedule } from "@calcom/features/schedules"; import { DEFAULT_SCHEDULE } from "@calcom/lib/availability"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { TRPCClientErrorLike } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; import type { AppRouter } from "@calcom/trpc/server/routers/_app"; import { Button, Form } from "@calcom/ui"; import { ArrowRight } from "@calcom/ui/components/icon"; interface ISetupAvailabilityProps { nextStep: () => void; defaultScheduleId?: number | null; } const SetupAvailability = (props: ISetupAvailabilityProps) => { const { defaultScheduleId } = props; const { t } = useLocale(); const { nextStep } = props; const router = useRouter(); const queryAvailability = trpc.viewer.availability.schedule.get.useQuery( { scheduleId: defaultScheduleId! }, { enabled: router.isReady && !!defaultScheduleId, } ); const availabilityForm = useForm({ defaultValues: { schedule: queryAvailability?.data?.availability || DEFAULT_SCHEDULE, }, }); const mutationOptions = { onError: (error: TRPCClientErrorLike) => { throw new Error(error.message); }, onSuccess: () => { nextStep(); }, }; const createSchedule = trpc.viewer.availability.schedule.create.useMutation(mutationOptions); const updateSchedule = trpc.viewer.availability.schedule.update.useMutation(mutationOptions); return (
{ try { if (defaultScheduleId) { await updateSchedule.mutate({ scheduleId: defaultScheduleId, name: t("default_schedule_name"), ...values, }); } else { await createSchedule.mutate({ name: t("default_schedule_name"), ...values, }); } } catch (error) { if (error instanceof Error) { // setError(error); // @TODO: log error } } }}>
); }; export { SetupAvailability };