import { GetStaticPaths, GetStaticProps } from "next"; import { useRouter } from "next/router"; import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { z } from "zod"; import { availabilityAsString, DEFAULT_SCHEDULE } from "@calcom/lib/availability"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import showToast from "@calcom/lib/notification"; import { stringOrNumber } from "@calcom/prisma/zod-utils"; import { inferQueryOutput, trpc } from "@calcom/trpc/react"; import Button from "@calcom/ui/Button"; import { BadgeCheckIcon } from "@calcom/ui/Icon"; import Switch from "@calcom/ui/Switch"; import { Form } from "@calcom/ui/form/fields"; import { QueryCell } from "@lib/QueryCell"; import { HttpError } from "@lib/core/http/error"; import Shell from "@components/Shell"; import Schedule from "@components/availability/Schedule"; import EditableHeading from "@components/ui/EditableHeading"; import TimezoneSelect from "@components/ui/form/TimezoneSelect"; export function AvailabilityForm(props: inferQueryOutput<"viewer.availability.schedule">) { const { t } = useLocale(); const router = useRouter(); const utils = trpc.useContext(); const form = useForm({ defaultValues: { schedule: props.availability || DEFAULT_SCHEDULE, isDefault: !!props.isDefault, timeZone: props.timeZone, }, }); const updateMutation = trpc.useMutation("viewer.availability.schedule.update", { onSuccess: async ({ schedule }) => { await utils.invalidateQueries(["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 (
{ updateMutation.mutate({ scheduleId: parseInt(router.query.schedule as string, 10), name: props.schedule.name, ...values, }); }} className="grid grid-cols-3 gap-2">

{t("change_start_end")}

{props.isDefault ? (
{t("default")}
) : ( ( )} /> )}
( onChange(timezone.value)} /> )} />

{t("something_doesnt_look_right")}

{t("troubleshoot_availability")}

); } const querySchema = z.object({ schedule: stringOrNumber, }); export default function Availability() { const router = useRouter(); const { i18n } = useLocale(); const { schedule: scheduleId } = router.isReady ? querySchema.parse(router.query) : { schedule: -1 }; const query = trpc.useQuery(["viewer.availability.schedule", { scheduleId }], { enabled: router.isReady }); const [name, setName] = useState(); return (
{ return ( } subtitle={data.schedule.availability.map((availability) => ( {availabilityAsString(availability, i18n.language)}
))}>
); }} />
); } 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", }; };