From c68d0d658bd4505a4fda55bb815a5295a2617a8b Mon Sep 17 00:00:00 2001 From: Harsh Khandeparkar Date: Wed, 15 Nov 2023 21:12:49 +0530 Subject: [PATCH] feat: update ls when updating info --- src/pages/MentorForm.tsx | 5 ++++- src/util/auth.tsx | 25 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/pages/MentorForm.tsx b/src/pages/MentorForm.tsx index 20a61f3d..bc95a0cc 100644 --- a/src/pages/MentorForm.tsx +++ b/src/pages/MentorForm.tsx @@ -82,7 +82,10 @@ function MentorForm() { authContext.onRegister({ ...userData, type: "mentor" }); navigate(authContext.dashboardLink); - } else setInfo("Information successfully changed."); + } else { + authContext.updateUserData(responses.name, responses.email); + setInfo("Information successfully changed."); + } } return res.is_ok; diff --git a/src/util/auth.tsx b/src/util/auth.tsx index d6530e5e..616c9ee8 100644 --- a/src/util/auth.tsx +++ b/src/util/auth.tsx @@ -41,6 +41,7 @@ interface IAuthContext { formLink: ROUTER_PATHS.STUDENT_FORM | ROUTER_PATHS.MENTOR_FORM; dashboardLink: ROUTER_PATHS.STUDENT_DASHBOARD | ROUTER_PATHS.MENTOR_DASHBOARD; setUserType: (type: UserType) => void; + updateUserData: (name: string, email: string) => void, onLogin: (auth: ILocalStorageAuthObj) => void; onRegister: (auth: IUserAuthData) => void; onLogout: () => void; @@ -55,6 +56,7 @@ const DEFAULT_AUTH_CONTEXT: IAuthContext = { dashboardLink: ROUTER_PATHS.STUDENT_DASHBOARD, jwt: DEFAULT_AUTH_OBJ.jwt, setUserType: () => {}, + updateUserData: () => {}, onLogin: () => {}, onRegister: () => {}, onLogout: () => {}, @@ -124,14 +126,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { ? ROUTER_PATHS.STUDENT_DASHBOARD : ROUTER_PATHS.MENTOR_DASHBOARD, ); - - localStorage.setItem("auth", JSON.stringify(userAuth)); }; - const updateAuth = (auth: ILocalStorageAuthObj) => { - // Set the JWT in the local storage - localStorage.setItem("auth", JSON.stringify(auth)); + const updateUserData = (name: string, email: string) => { + setUserAuth({ + ...userAuth, + userData: { + ...userAuth.userData, + name: name, + email: email + } + }) + } + const updateAuth = (auth: ILocalStorageAuthObj) => { setFormLink( auth.userData.type === "student" ? ROUTER_PATHS.STUDENT_FORM @@ -169,6 +177,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { setUserAuth(DEFAULT_AUTH_OBJ); }; + useEffect(() => { + localStorage.setItem('auth', JSON.stringify(userAuth)); + }, [userAuth]) + useEffect(() => { const auth = getLsAuthObj(); @@ -191,8 +203,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { onLogin, onRegister, onLogout, + updateUserData }), - [isAuthenticated, userAuth, onLogin, onRegister, onLogout], + [isAuthenticated, userAuth, onLogin, onRegister, onLogout, updateUserData], ); return {children};