2022-09-06 22:58:16 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { Schedule } from "@calcom/features/schedules";
|
2022-09-15 05:49:59 +00:00
|
|
|
import { DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
2022-09-06 22:58:16 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { TRPCClientErrorLike } from "@calcom/trpc/react";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
import type { AppRouter } from "@calcom/trpc/server/routers/_app";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, Form } from "@calcom/ui";
|
2023-05-17 12:47:44 +00:00
|
|
|
import { ArrowRight } from "@calcom/ui/components/icon";
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
interface ISetupAvailabilityProps {
|
|
|
|
nextStep: () => void;
|
|
|
|
defaultScheduleId?: number | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SetupAvailability = (props: ISetupAvailabilityProps) => {
|
|
|
|
const { defaultScheduleId } = props;
|
|
|
|
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { nextStep } = props;
|
|
|
|
|
2023-08-22 11:02:40 +00:00
|
|
|
const scheduleId = defaultScheduleId === null ? undefined : defaultScheduleId;
|
2023-07-07 00:06:37 +00:00
|
|
|
const queryAvailability = trpc.viewer.availability.schedule.get.useQuery(
|
2023-08-25 11:56:20 +00:00
|
|
|
{ scheduleId: defaultScheduleId ?? undefined },
|
2023-07-07 00:06:37 +00:00
|
|
|
{
|
2023-08-22 11:02:40 +00:00
|
|
|
enabled: !!scheduleId,
|
2023-07-07 00:06:37 +00:00
|
|
|
}
|
|
|
|
);
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
const availabilityForm = useForm({
|
2022-09-15 05:49:59 +00:00
|
|
|
defaultValues: {
|
|
|
|
schedule: queryAvailability?.data?.availability || DEFAULT_SCHEDULE,
|
|
|
|
},
|
2022-09-06 22:58:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mutationOptions = {
|
|
|
|
onError: (error: TRPCClientErrorLike<AppRouter>) => {
|
|
|
|
throw new Error(error.message);
|
|
|
|
},
|
|
|
|
onSuccess: () => {
|
|
|
|
nextStep();
|
|
|
|
},
|
|
|
|
};
|
2022-11-10 23:40:01 +00:00
|
|
|
const createSchedule = trpc.viewer.availability.schedule.create.useMutation(mutationOptions);
|
|
|
|
const updateSchedule = trpc.viewer.availability.schedule.update.useMutation(mutationOptions);
|
2022-09-06 22:58:16 +00:00
|
|
|
return (
|
2022-09-15 05:49:59 +00:00
|
|
|
<Form
|
2023-04-21 08:24:59 +00:00
|
|
|
className="bg-default dark:text-inverted text-emphasis w-full [--cal-brand-accent:#fafafa] dark:bg-opacity-5"
|
2022-09-06 22:58:16 +00:00
|
|
|
form={availabilityForm}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}>
|
2022-09-15 05:49:59 +00:00
|
|
|
<Schedule control={availabilityForm.control} name="schedule" weekStart={1} />
|
2022-09-06 22:58:16 +00:00
|
|
|
|
|
|
|
<div>
|
|
|
|
<Button
|
|
|
|
data-testid="save-availability"
|
|
|
|
type="submit"
|
2022-09-07 01:34:33 +00:00
|
|
|
className="mt-2 w-full justify-center p-2 text-sm sm:mt-8"
|
2022-09-06 22:58:16 +00:00
|
|
|
disabled={availabilityForm.formState.isSubmitting}>
|
2023-05-17 12:47:44 +00:00
|
|
|
{t("next_step_text")} <ArrowRight className="ml-2 h-4 w-4 self-center" />
|
2022-09-06 22:58:16 +00:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { SetupAvailability };
|