fix: save button in availibility disabled when no changes (#26174)
* fix: save button in availibility disabled when no changes * fix: updated comparison logic * fix: changed logic from and to or
This commit is contained in:
@@ -320,7 +320,10 @@ export const AvailabilitySettings = forwardRef<AvailabilitySettingsFormRef, Avai
|
||||
|
||||
const form = useForm<AvailabilityFormValues>({
|
||||
defaultValues: {
|
||||
...schedule,
|
||||
name: schedule.name,
|
||||
timeZone: schedule.timeZone,
|
||||
isDefault: schedule.isDefault,
|
||||
dateOverrides: schedule.dateOverrides,
|
||||
schedule: schedule.availability || [],
|
||||
},
|
||||
});
|
||||
@@ -329,6 +332,20 @@ export const AvailabilitySettings = forwardRef<AvailabilitySettingsFormRef, Avai
|
||||
control: form.control,
|
||||
});
|
||||
|
||||
const initialValuesRef = useRef<AvailabilityFormValues | null>(null);
|
||||
useEffect(() => {
|
||||
initialValuesRef.current = form.getValues() as AvailabilityFormValues;
|
||||
}, [form, schedule]);
|
||||
|
||||
const formHasChanges = useMemo(() => {
|
||||
if (!initialValuesRef.current) return false;
|
||||
try {
|
||||
return (JSON.stringify(form.watch("schedule")) !== JSON.stringify(initialValuesRef.current.availability) || JSON.stringify(watchedValues) !== JSON.stringify(initialValuesRef.current));
|
||||
} catch {
|
||||
return form.formState.isDirty;
|
||||
}
|
||||
}, [watchedValues, form.formState.isDirty]);
|
||||
|
||||
// Trigger callback whenever the form state changes
|
||||
useEffect(() => {
|
||||
if (onFormStateChange && watchedValues) {
|
||||
@@ -623,7 +640,9 @@ export const AvailabilitySettings = forwardRef<AvailabilitySettingsFormRef, Avai
|
||||
className="ml-4 lg:ml-0"
|
||||
type="submit"
|
||||
form="availability-form"
|
||||
loading={isSaving}>
|
||||
loading={isSaving}
|
||||
disabled={isLoading || !formHasChanges}
|
||||
>
|
||||
{t("save")}
|
||||
</Button>
|
||||
<Button
|
||||
|
||||
@@ -14,6 +14,7 @@ export type Availability = {
|
||||
export type WeekdayFormat = "short" | "long";
|
||||
|
||||
export type AvailabilityFormValues = {
|
||||
availability: number[][];
|
||||
name: string;
|
||||
schedule: ScheduleType;
|
||||
dateOverrides: { ranges: TimeRange[] }[];
|
||||
|
||||
Reference in New Issue
Block a user