fix: Add onFormStateChange callback on EventTypeSettings atom [CAL-5856] (#21615)
* Adds onFormStateChange callback on EventTypeSettings atom * updated docs for event-type * added watch all form values --------- Co-authored-by: Morgan <[email protected]>
This commit is contained in:
1 parent
84171928cb
commit
ad697365ce
3 files changed
+34
-1
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useMemo, useState } from "react";
|
||||
import { useMemo, useState, useEffect } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -27,9 +27,15 @@ type Fields = z.infer<typeof eventTypeBookingFieldsSchema>;
|
||||
export const useEventTypeForm = ({
|
||||
eventType,
|
||||
onSubmit,
|
||||
onFormStateChange,
|
||||
}: {
|
||||
eventType: EventTypeSetupProps["eventType"];
|
||||
onSubmit: (data: EventTypeUpdateInput) => void;
|
||||
onFormStateChange?: (formState: {
|
||||
isDirty: boolean;
|
||||
dirtyFields: Partial<FormValues>;
|
||||
values: FormValues;
|
||||
}) => void;
|
||||
}) => {
|
||||
const { t } = useLocale();
|
||||
const [periodDates] = useState<{ startDate: Date; endDate: Date }>({
|
||||
@@ -181,6 +187,19 @@ export const useEventTypeForm = ({
|
||||
formState: { isDirty: isFormDirty, dirtyFields },
|
||||
} = form;
|
||||
|
||||
// Watch all form values to trigger onFormStateChange on any change
|
||||
const watchedValues = form.watch();
|
||||
|
||||
useEffect(() => {
|
||||
if (onFormStateChange) {
|
||||
onFormStateChange({
|
||||
isDirty: isFormDirty,
|
||||
dirtyFields: dirtyFields as Partial<FormValues>,
|
||||
values: watchedValues,
|
||||
});
|
||||
}
|
||||
}, [isFormDirty, watchedValues, onFormStateChange]);
|
||||
|
||||
const isObject = <T>(value: T): boolean => {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
};
|
||||
|
||||
@@ -57,6 +57,11 @@ export type EventTypePlatformWrapperProps = {
|
||||
customClassNames?: EventTypeCustomClassNames;
|
||||
disableToasts?: boolean;
|
||||
isDryRun?: boolean;
|
||||
onFormStateChange?: (formState: {
|
||||
isDirty: boolean;
|
||||
dirtyFields: Partial<FormValues>;
|
||||
values: FormValues;
|
||||
}) => void;
|
||||
};
|
||||
|
||||
const EventType = ({
|
||||
@@ -70,6 +75,7 @@ const EventType = ({
|
||||
customClassNames,
|
||||
disableToasts = false,
|
||||
isDryRun = false,
|
||||
onFormStateChange,
|
||||
...props
|
||||
}: EventTypeSetupProps & EventTypePlatformWrapperProps) => {
|
||||
const { t } = useLocale();
|
||||
@@ -150,6 +156,7 @@ const EventType = ({
|
||||
toast({ description: t("event_type_updated_successfully", { eventTypeTitle: eventType.title }) });
|
||||
}
|
||||
},
|
||||
onFormStateChange: onFormStateChange,
|
||||
});
|
||||
const slug = form.watch("slug") ?? eventType.slug;
|
||||
|
||||
@@ -320,6 +327,7 @@ export const EventTypePlatformWrapper = ({
|
||||
allowDelete = true,
|
||||
customClassNames,
|
||||
isDryRun,
|
||||
onFormStateChange,
|
||||
}: EventTypePlatformWrapperProps) => {
|
||||
const { data: eventTypeQueryData } = useAtomsEventTypeById(id);
|
||||
const queryClient = useQueryClient();
|
||||
@@ -352,6 +360,7 @@ export const EventTypePlatformWrapper = ({
|
||||
allowDelete={allowDelete}
|
||||
customClassNames={customClassNames}
|
||||
isDryRun={isDryRun}
|
||||
onFormStateChange={onFormStateChange}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user