diff --git a/apps/nova/src/components/Settings/ProfileForm.tsx b/apps/nova/src/components/Settings/ProfileForm.tsx index 434eb593..8f6143f3 100644 --- a/apps/nova/src/components/Settings/ProfileForm.tsx +++ b/apps/nova/src/components/Settings/ProfileForm.tsx @@ -21,6 +21,7 @@ import { QueryKey } from "@litespace/headless/constants"; import { useUpdateUser } from "@litespace/headless/user"; import TopicSelector from "@/components/Settings/TopicSelector"; import { useTopics, useUserTopics } from "@litespace/headless/topic"; +import { isEqual } from "lodash"; type IForm = { name: string; @@ -83,6 +84,25 @@ export const ProfileForm: React.FC<{ user: IUser.Self }> = ({ user }) => { !!form.watch("password.current") || !!form.watch("password.confirm"); + const currentValues = form.watch(); + /** + * flag to indicate that change has happened in user settings to enable sending requests + */ + const canSubmit = useMemo(() => { + const initialValues = form.control._defaultValues; + + console.log({ userTopics, topics }); + console.log(isEqual(userTopics, topics)); + + return ( + isEqual(initialValues, currentValues) && + !photo && + isEqual(userTopics, topics) + ); + }, [form, currentValues, photo, topics, userTopics]); + + console.log(canSubmit); + const onSuccess = useCallback(() => { invalidateQuery([QueryKey.FindCurrentUser]); toast.success({ title: intl("profile.update.success") }); @@ -120,6 +140,7 @@ export const ProfileForm: React.FC<{ user: IUser.Self }> = ({ user }) => { const onSubmit = useCallback( (data: IForm) => { + if (!canSubmit) return; if (photo) console.log("upload photo"); if (!user) return; @@ -141,7 +162,7 @@ export const ProfileForm: React.FC<{ user: IUser.Self }> = ({ user }) => { }, }); }, - [mutation, photo, user] + [mutation, photo, user, canSubmit] ); useEffect(() => { @@ -310,7 +331,7 @@ export const ProfileForm: React.FC<{ user: IUser.Self }> = ({ user }) => { loading={allTopicsQuery.query.isPending || userTopicsQuery.isPending} /> - + +
+ + {label} + + +
+
); if (idx === 2) @@ -111,7 +110,7 @@ export const MultiSelect = ({ > {selectedOptions.length - idx} +