2022-08-24 20:18:42 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-28 19:14:38 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
DialogTrigger,
|
|
|
|
Form,
|
|
|
|
Icon,
|
|
|
|
showToast,
|
|
|
|
} from "@calcom/ui";
|
2022-08-24 20:18:42 +00:00
|
|
|
|
|
|
|
export function NewScheduleButton({ name = "new-schedule" }: { name?: string }) {
|
|
|
|
const router = useRouter();
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
const form = useForm<{
|
|
|
|
name: string;
|
|
|
|
}>();
|
|
|
|
const { register } = form;
|
2022-09-21 05:40:58 +00:00
|
|
|
const utils = trpc.useContext();
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const createMutation = trpc.viewer.availability.schedule.create.useMutation({
|
2022-08-24 20:18:42 +00:00
|
|
|
onSuccess: async ({ schedule }) => {
|
|
|
|
await router.push("/availability/" + schedule.id);
|
|
|
|
showToast(t("schedule_created_successfully", { scheduleName: schedule.name }), "success");
|
2022-11-10 23:40:01 +00:00
|
|
|
utils.viewer.availability.list.setData(undefined, (data) => {
|
2022-09-21 05:40:58 +00:00
|
|
|
const newSchedule = { ...schedule, isDefault: false, availability: [] };
|
|
|
|
if (!data)
|
|
|
|
return {
|
|
|
|
schedules: [newSchedule],
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
schedules: [...data.schedules, newSchedule],
|
|
|
|
};
|
|
|
|
});
|
2022-08-24 20:18:42 +00:00
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.data?.code === "UNAUTHORIZED") {
|
|
|
|
const message = `${err.data.code}: You are not able to create this event`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog name={name} clearQueryParamsOnClose={["copy-schedule-id"]}>
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
<Button data-testid={name} StartIcon={Icon.FiPlus}>
|
2022-09-06 19:13:20 +00:00
|
|
|
{t("new")}
|
2022-08-24 20:18:42 +00:00
|
|
|
</Button>
|
|
|
|
</DialogTrigger>
|
2022-12-01 00:59:27 +00:00
|
|
|
<DialogContent title={t("add_new_schedule")}>
|
2022-08-24 20:18:42 +00:00
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
createMutation.mutate(values);
|
|
|
|
}}>
|
2022-12-01 00:59:27 +00:00
|
|
|
<div className="space-y-2">
|
2022-08-24 20:18:42 +00:00
|
|
|
<label htmlFor="label" className="block text-sm font-medium text-gray-700">
|
|
|
|
{t("name")}
|
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
required
|
|
|
|
className="block w-full rounded-sm border-gray-300 text-sm"
|
|
|
|
placeholder={t("default_schedule_name")}
|
|
|
|
{...register("name")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogFooter>
|
2022-12-01 00:59:27 +00:00
|
|
|
<DialogClose />
|
2022-08-24 20:18:42 +00:00
|
|
|
<Button type="submit" loading={createMutation.isLoading}>
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
2022-11-28 19:14:38 +00:00
|
|
|
</DialogFooter>
|
2022-08-24 20:18:42 +00:00
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|