* init: create schedule atom * feat: api v2 endpoints for create schedule atom * fixup * fix: merge conflicts * feat: update platform libraries * chore: add disableToasts prop * fixup: remove classname, not needed * fix: reset form after schedule is created * docs for create schedule atom * better names inside classnames prop * chore: implement PR feedback * implement coderabbit feedback
109 lines
3.0 KiB
TypeScript
109 lines
3.0 KiB
TypeScript
import { useForm } from "react-hook-form";
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import type { ApiErrorResponse } from "@calcom/platform-types";
|
|
import { Button } from "@calcom/ui/components/button";
|
|
import { Form } from "@calcom/ui/components/form";
|
|
import { InputField } from "@calcom/ui/components/form";
|
|
|
|
import { useAtomCreateSchedule } from "../hooks/schedules/useAtomCreateSchedule";
|
|
import { AtomsWrapper } from "../src/components/atoms-wrapper";
|
|
import { useToast } from "../src/components/ui/use-toast";
|
|
import { cn } from "../src/lib/utils";
|
|
|
|
export type ActionButtonsClassNames = {
|
|
container?: string;
|
|
continue?: string;
|
|
close?: string;
|
|
};
|
|
|
|
export const CreateScheduleForm = ({
|
|
onSuccess,
|
|
onError,
|
|
onCancel,
|
|
customClassNames,
|
|
disableToasts = false,
|
|
}: {
|
|
onSuccess?: (scheduleId: number) => void;
|
|
onError?: (err: ApiErrorResponse) => void;
|
|
onCancel?: () => void;
|
|
customClassNames?: {
|
|
formWrapper?: string;
|
|
inputField?: string;
|
|
actionsButtons?: ActionButtonsClassNames;
|
|
};
|
|
disableToasts?: boolean;
|
|
}) => {
|
|
const { toast } = useToast();
|
|
const { t } = useLocale();
|
|
const form = useForm<{
|
|
name: string;
|
|
}>();
|
|
const { register } = form;
|
|
|
|
const { mutateAsync: createSchedule, isPending: isCreateSchedulePending } = useAtomCreateSchedule({
|
|
onSuccess: (res) => {
|
|
if (!disableToasts) {
|
|
toast({
|
|
description: t("schedule_created_successfully", { scheduleName: res.data.schedule.name }),
|
|
});
|
|
}
|
|
form.reset();
|
|
onSuccess?.(res.data.schedule.id);
|
|
},
|
|
onError: (err) => {
|
|
onError?.(err);
|
|
if (!disableToasts) {
|
|
toast({
|
|
description: `Could not create schedule: ${err.error.message}`,
|
|
});
|
|
}
|
|
},
|
|
});
|
|
|
|
return (
|
|
<AtomsWrapper>
|
|
<Form
|
|
className={customClassNames?.formWrapper}
|
|
form={form}
|
|
handleSubmit={async (values) => {
|
|
await createSchedule(values);
|
|
}}>
|
|
<InputField
|
|
className={customClassNames?.inputField}
|
|
label={t("name")}
|
|
type="text"
|
|
id="name"
|
|
required
|
|
placeholder={t("default_schedule_name")}
|
|
{...register("name", {
|
|
setValueAs: (v) => (!v || v.trim() === "" ? null : v),
|
|
})}
|
|
/>
|
|
<div
|
|
className={cn(
|
|
"mt-5 justify-end space-x-2 rtl:space-x-reverse sm:mt-4 sm:flex",
|
|
customClassNames?.actionsButtons?.container
|
|
)}>
|
|
{onCancel && (
|
|
<Button
|
|
type="button"
|
|
color="secondary"
|
|
onClick={onCancel}
|
|
className={customClassNames?.actionsButtons?.close}>
|
|
{t("close")}
|
|
</Button>
|
|
)}
|
|
<Button
|
|
type="submit"
|
|
loading={isCreateSchedulePending}
|
|
className={customClassNames?.actionsButtons?.continue}>
|
|
{" "}
|
|
{t("continue")}
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</AtomsWrapper>
|
|
);
|
|
};
|