2022-09-06 22:58:16 +00:00
|
|
|
import { ArrowRightIcon } from "@heroicons/react/solid";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
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";
|
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;
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
let queryAvailability;
|
|
|
|
if (defaultScheduleId) {
|
2022-11-10 23:40:01 +00:00
|
|
|
queryAvailability = trpc.viewer.availability.schedule.get.useQuery(
|
|
|
|
{ scheduleId: defaultScheduleId },
|
|
|
|
{
|
|
|
|
enabled: router.isReady,
|
|
|
|
}
|
|
|
|
);
|
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-05 18:14:46 +00:00
|
|
|
className="bg-default dark:text-inverted text-emphasis w-full 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}>
|
|
|
|
{t("next_step_text")} <ArrowRightIcon className="ml-2 h-4 w-4 self-center" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { SetupAvailability };
|