diff --git a/packages/features/schedules/components/Schedule.tsx b/packages/features/schedules/components/Schedule.tsx index 0d09c34af5404c..b753ddb80ce4e6 100644 --- a/packages/features/schedules/components/Schedule.tsx +++ b/packages/features/schedules/components/Schedule.tsx @@ -12,6 +12,7 @@ import type { import { Controller, useFieldArray, useFormContext } from "react-hook-form"; import type { GroupBase, Props } from "react-select"; +import type { AvailabilityFormValues } from "@calcom/atoms/availability/types"; import type { ConfigType } from "@calcom/dayjs"; import dayjs from "@calcom/dayjs"; import { defaultDayRange as DEFAULT_DAY_RANGE } from "@calcom/lib/availability"; @@ -47,6 +48,7 @@ export const ScheduleDay = ({ name, weekday, control, + handleSubmit, CopyButton, disabled, labels, @@ -56,6 +58,7 @@ export const ScheduleDay = ({ name: ArrayPath; weekday: string; control: Control; + handleSubmit?: (data: AvailabilityFormValues) => Promise; CopyButton: JSX.Element; disabled?: boolean; labels?: ScheduleLabelsType; @@ -68,7 +71,7 @@ export const ScheduleDay = ({ scheduleContainer?: string; }; }) => { - const { watch, setValue } = useFormContext(); + const { watch, setValue, getValues } = useFormContext(); const watchDayRange = watch(name); return ( @@ -94,6 +97,7 @@ export const ScheduleDay = ({ data-testid={`${weekday}-switch`} onCheckedChange={(isChecked) => { setValue(name, (isChecked ? [DEFAULT_DAY_RANGE] : []) as TFieldValues[typeof name]); + handleSubmit && handleSubmit(getValues() as AvailabilityFormValues); }} /> @@ -111,6 +115,7 @@ export const ScheduleDay = ({ control={control} name={name} disabled={disabled} + handleSubmit={handleSubmit} className={{ dayRanges: className?.dayRanges, timeRangeField: className?.timeRangeField, @@ -128,10 +133,12 @@ const CopyButton = ({ getValuesFromDayRange, weekStart, labels, + handleSubmit, }: { getValuesFromDayRange: string; weekStart: number; labels?: ScheduleLabelsType; + handleSubmit?: (data: AvailabilityFormValues) => Promise; }) => { const { t } = useLocale(); const [open, setOpen] = useState(false); @@ -160,6 +167,7 @@ const CopyButton = ({ onClick={(selected) => { selected.forEach((day) => setValue(`${fieldArrayName}.${day}`, getValues(getValuesFromDayRange))); setOpen(false); + handleSubmit && handleSubmit(getValues() as AvailabilityFormValues); }} onCancel={() => setOpen(false)} /> @@ -176,6 +184,7 @@ const Schedule = < control: Control; weekStart?: number; disabled?: boolean; + handleSubmit?: (data: AvailabilityFormValues) => Promise; labels?: ScheduleLabelsType; userTimeFormat?: number | null; }) => { @@ -191,6 +200,7 @@ export const ScheduleComponent = < >({ name, control, + handleSubmit, disabled, weekStart = 0, labels, @@ -199,6 +209,7 @@ export const ScheduleComponent = < }: { name: TPath; control: Control; + handleSubmit?: (data: AvailabilityFormValues) => Promise; weekStart?: number; disabled?: boolean; labels?: ScheduleLabelsType; @@ -234,8 +245,14 @@ export const ScheduleComponent = < key={weekday} weekday={weekday} control={control} + handleSubmit={handleSubmit} CopyButton={ - + } /> ); @@ -251,6 +268,7 @@ export const DayRanges = ({ labels, userTimeFormat, className, + handleSubmit, }: { name: ArrayPath; control?: Control; @@ -261,6 +279,7 @@ export const DayRanges = ({ dayRanges?: string; timeRangeField?: string; }; + handleSubmit?: (data: AvailabilityFormValues) => Promise; }) => { const { t } = useLocale(); const { getValues } = useFormContext(); @@ -283,6 +302,7 @@ export const DayRanges = ({ )} @@ -306,16 +326,23 @@ export const DayRanges = ({ if (slotRange?.append) { append(slotRange.append); + handleSubmit && handleSubmit(getValues() as AvailabilityFormValues); } if (slotRange?.prepend) { prepend(slotRange.prepend); + handleSubmit && handleSubmit(getValues() as AvailabilityFormValues); } }} /> )} {index !== 0 && ( - + )} @@ -330,14 +357,17 @@ const RemoveTimeButton = ({ disabled, className, labels, + handleSubmit, }: { index: number | number[]; remove: UseFieldArrayRemove; className?: string; disabled?: boolean; labels?: ScheduleLabelsType; + handleSubmit?: (data: AvailabilityFormValues) => Promise; }) => { const { t } = useLocale(); + const { getValues } = useFormContext(); return (