import { GetStaticPaths, GetStaticProps } from "next"; import { useRouter } from "next/router"; import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; import { z } from "zod"; import Schedule from "@calcom/features/schedules/components/Schedule"; import { availabilityAsString } from "@calcom/lib/availability"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { stringOrNumber } from "@calcom/prisma/zod-utils"; import { trpc } from "@calcom/trpc/react"; import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery"; import type { Schedule as ScheduleType } from "@calcom/types/schedule"; import { Icon } from "@calcom/ui"; import TimezoneSelect from "@calcom/ui/form/TimezoneSelect"; import Button from "@calcom/ui/v2/core/Button"; import Shell from "@calcom/ui/v2/core/Shell"; import Switch from "@calcom/ui/v2/core/Switch"; import VerticalDivider from "@calcom/ui/v2/core/VerticalDivider"; import { Form, Label } from "@calcom/ui/v2/core/form/fields"; import showToast from "@calcom/ui/v2/core/notifications"; import { Skeleton, SkeletonText } from "@calcom/ui/v2/core/skeleton"; import { HttpError } from "@lib/core/http/error"; import EditableHeading from "@components/ui/EditableHeading"; import { SelectSkeletonLoader } from "@components/v2/availability/SkeletonLoader"; const querySchema = z.object({ schedule: stringOrNumber, }); type AvailabilityFormValues = { name: string; schedule: ScheduleType; timeZone: string; isDefault: boolean; }; export default function Availability({ schedule }: { schedule: number }) { const { t, i18n } = useLocale(); const router = useRouter(); const utils = trpc.useContext(); const me = useMeQuery(); const { data, isLoading } = trpc.useQuery(["viewer.availability.schedule", { scheduleId: schedule }]); const form = useForm(); const { control, reset } = form; useEffect(() => { if (!isLoading && data) { reset({ name: data?.schedule?.name, schedule: data.availability, timeZone: data.timeZone, isDefault: data.isDefault, }); } }, [data, isLoading, reset]); const updateMutation = trpc.useMutation("viewer.availability.schedule.update", { onSuccess: async ({ schedule }) => { await utils.invalidateQueries(["viewer.availability.schedule"]); await utils.refetchQueries(["viewer.availability.schedule"]); await router.push("/availability"); showToast( t("availability_updated_successfully", { scheduleName: schedule.name, }), "success" ); }, onError: (err) => { if (err instanceof HttpError) { const message = `${err.statusCode}: ${err.message}`; showToast(message, "error"); } }, }); return ( } /> } subtitle={ data ? ( data.schedule.availability.map((availability) => ( {availabilityAsString(availability, { locale: i18n.language })}
)) ) : ( ) } CTA={
{t("set_to_default")} { form.setValue("isDefault", e); }} />
}>
{/* TODO: Find a better way to guarantee alignment, but for now this'll do. */}
{ updateMutation.mutate({ scheduleId: schedule, ...values, }); }} className="-mx-4 flex flex-col pb-16 sm:mx-0 xl:flex-row xl:space-x-6">

{t("change_start_end")}

{typeof me.data?.weekStart === "string" && ( )}
value ? ( onChange(timezone.value)} /> ) : ( ) } />

{t("something_doesnt_look_right")}

); } export const getStaticProps: GetStaticProps = (ctx) => { const params = querySchema.safeParse(ctx.params); if (!params.success) return { notFound: true }; return { props: { schedule: params.data.schedule, }, revalidate: 10, // seconds }; }; export const getStaticPaths: GetStaticPaths = () => { return { paths: [], fallback: "blocking", }; };