2022-03-17 16:48:23 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-03-17 16:48:23 +00:00
|
|
|
import { Button } from "@calcom/ui";
|
|
|
|
import { Dialog, DialogClose, DialogContent, DialogTrigger } from "@calcom/ui/Dialog";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-05-17 20:43:27 +00:00
|
|
|
import { Form } from "@calcom/ui/form/fields";
|
2022-10-21 08:54:18 +00:00
|
|
|
import showToast from "@calcom/ui/v2/core/notifications";
|
2022-03-17 16:48:23 +00:00
|
|
|
|
|
|
|
import { HttpError } from "@lib/core/http/error";
|
|
|
|
|
|
|
|
export function NewScheduleButton({ name = "new-schedule" }: { name?: string }) {
|
|
|
|
const router = useRouter();
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
const form = useForm<{
|
|
|
|
name: string;
|
|
|
|
}>();
|
|
|
|
const { register } = form;
|
|
|
|
|
|
|
|
const createMutation = trpc.useMutation("viewer.availability.schedule.create", {
|
|
|
|
onSuccess: async ({ schedule }) => {
|
|
|
|
await router.push("/availability/" + schedule.id);
|
|
|
|
showToast(t("schedule_created_successfully", { scheduleName: schedule.name }), "success");
|
|
|
|
},
|
|
|
|
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>
|
2022-08-03 16:01:29 +00:00
|
|
|
<Button data-testid={name} StartIcon={Icon.FiPlus}>
|
2022-03-17 16:48:23 +00:00
|
|
|
{t("new_schedule_btn")}
|
|
|
|
</Button>
|
|
|
|
</DialogTrigger>
|
|
|
|
<DialogContent>
|
2022-05-30 17:26:58 +00:00
|
|
|
<div className="mb-8">
|
2022-03-17 16:48:23 +00:00
|
|
|
<h3 className="text-lg font-bold leading-6 text-gray-900" id="modal-title">
|
|
|
|
{t("add_new_schedule")}
|
|
|
|
</h3>
|
|
|
|
</div>
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
createMutation.mutate(values);
|
|
|
|
}}>
|
2022-05-30 17:26:58 +00:00
|
|
|
<div className="mt-3 space-y-2">
|
2022-04-14 14:58:23 +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
|
2022-07-15 16:39:11 +00:00
|
|
|
className="block w-full rounded-sm border-gray-300 text-sm"
|
2022-04-14 14:58:23 +00:00
|
|
|
placeholder={t("default_schedule_name")}
|
|
|
|
{...register("name")}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-03-17 16:48:23 +00:00
|
|
|
</div>
|
|
|
|
<div className="mt-8 flex flex-row-reverse gap-x-2">
|
|
|
|
<Button type="submit" loading={createMutation.isLoading}>
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
<DialogClose asChild>
|
|
|
|
<Button color="secondary">{t("cancel")}</Button>
|
|
|
|
</DialogClose>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|