Skip to content

Commit

Permalink
refactor(profile): migrated change detection from useMemo to useEffec…
Browse files Browse the repository at this point in the history
…t for cleaner comparison logic
  • Loading branch information
JavidSumra committed Oct 17, 2024
1 parent 7fff299 commit 5fee85e
Showing 1 changed file with 14 additions and 46 deletions.
60 changes: 14 additions & 46 deletions src/Components/Users/UserProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useReducer, FormEvent, useMemo } from "react";
import { useState, useReducer, FormEvent, useEffect } from "react";
import { GENDER_TYPES } from "../../Common/constants";
import { validateEmailAddress } from "../../Common/validation";
import * as Notification from "../../Utils/Notifications.js";
Expand Down Expand Up @@ -117,6 +117,8 @@ export default function UserProfile() {
isChecking: false,
isUpdateAvailable: false,
});
const [hasChanges, setHasChanges] = useState<boolean>(false);
const [isFieldLoaded, setIsFieldLoaded] = useState<boolean>(false);

const authUser = useAuthUser();

Expand Down Expand Up @@ -172,6 +174,7 @@ export default function UserProfile() {
type: "set_form",
form: formData,
});
setHasChanges(false);
},
});

Expand All @@ -182,51 +185,6 @@ export default function UserProfile() {
},
);

const hasChanges = useMemo(() => {
if (!userData) return false;

const fieldsToCheck: (keyof EditForm)[] = [
"altPhoneNumber",
"date_of_birth",
"doctor_experience_commenced_on",
"doctor_medical_council_registration",
"email",
"firstName",
"gender",
"lastName",
"phoneNumber",
"qualification",
"video_connect_link",
"weekly_working_hours",
];

return fieldsToCheck.some((field) => {
if (field === "date_of_birth") {
return (
dayjs(states.form[field]).format("YYYY-MM-DD") !==
dayjs(userData[field]).format("YYYY-MM-DD")
);
}
if (field === "doctor_experience_commenced_on") {
const userDataYears = dayjs().diff(dayjs(userData[field]), "years");
return Number(states.form[field]) !== Number(userDataYears);
}
if (field === "weekly_working_hours")
return Number(states.form[field]) !== Number(userData[field]);
if (field === "firstName")
return states.form[field] !== userData.first_name;
if (field === "lastName")
return states.form[field] !== userData.last_name;
if (field === "phoneNumber")
return states.form[field] !== userData.phone_number;
if (field === "altPhoneNumber")
return states.form[field] !== userData.alt_phone_number;
if (field === "video_connect_link")
return states.form[field] !== userData[field] && !!states.form[field];
return states.form[field] !== userData[field];
});
}, [states.form, userData]);

const validateNewPassword = (password: string) => {
if (
password.length < 8 ||
Expand Down Expand Up @@ -369,8 +327,18 @@ export default function UserProfile() {
type: "set_form",
form: { ...states.form, [event.name]: event.value },
});
setIsFieldLoaded(true);
};

useEffect(() => {
if (showEdit && isFieldLoaded) {
setHasChanges(true);
} else {
setIsFieldLoaded(false);
setHasChanges(false);
}
}, [handleFieldChange]);

const getDate = (value: any) =>
value && dayjs(value).isValid() && dayjs(value).toDate();

Expand Down

0 comments on commit 5fee85e

Please sign in to comment.