From 938bc94ce0bde5c3d9a977dd2e73d358c2cc567a Mon Sep 17 00:00:00 2001 From: Estifo77 <139631617+Estifo77@users.noreply.github.com> Date: Sat, 8 Aug 2026 11:00:33 +0300 Subject: [PATCH] Update ProfilePage to handle user updates more reliably and prevent race conditions --- .../features/profile/pages/ProfilePage.tsx | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx index fa4373973..14f709b8b 100644 --- a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx +++ b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx @@ -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);