Update ProfilePage to handle user updates more reliably and prevent race conditions

This commit is contained in:
Estifo77
2026-08-08 11:00:33 +03:00
parent fdcdd9fd13
commit 938bc94ce0

View File

@@ -269,13 +269,11 @@ export function ProfilePage() {
});
const onSavePersonal = async (values: PersonalValues) => {
if (!user) return;
setIsSavingProfile(true);
try {
// The PATCH already returns the updated user — trusting it instead of
// an immediate follow-up GET avoids a read-after-write race where the
// GET can return pre-edit data and silently undo `setUser` below,
// which is why the change used to only show up after a re-login.
const updated = await updateTrigger({
await updateTrigger({
url: '/auth/update-profile',
method: 'PATCH',
body: {
@@ -286,7 +284,25 @@ export function ProfilePage() {
},
}).unwrap();
dispatch(setUser(updated));
// Update the session from the values that were just accepted. The
// endpoint is allowed to return no body (or a response wrapper), so
// treating its response as an AuthUser can blank or retain stale UI
// state until the next login.
const updatedUser: AuthUser = {
...user,
email: values.email,
username: values.username,
phoneNumber: values.phoneNumber,
name: { am: values.nameAm, en: values.nameEn },
};
dispatch(setUser(updatedUser));
resetPersonal({
nameEn: updatedUser.name.en,
nameAm: updatedUser.name.am,
username: updatedUser.username,
email: updatedUser.email,
phoneNumber: updatedUser.phoneNumber,
});
notify.success(t('profile.profileUpdated'));
} catch (e) {
handleError(e);