From ee35f8760e2de877f07ae24bd7ff4002a86c2857 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 20 Dec 2023 11:34:47 +0100 Subject: [PATCH] Client - change UI display only on login ou user preferences update --- .../src/store/modules/authUser/actions.ts | 27 +++++++++++-------- .../src/store/modules/authUser/enums.ts | 1 + .../src/store/modules/authUser/getters.ts | 3 +++ .../src/store/modules/authUser/types.ts | 7 ++++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/fittrackee_client/src/store/modules/authUser/actions.ts b/fittrackee_client/src/store/modules/authUser/actions.ts index 9880d50d..057fe893 100644 --- a/fittrackee_client/src/store/modules/authUser/actions.ts +++ b/fittrackee_client/src/store/modules/authUser/actions.ts @@ -57,7 +57,7 @@ export const actions: ActionTree & AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, window.localStorage.authToken ) - context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE) + context.dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE, true) } // after logout in another tab if ( @@ -118,13 +118,16 @@ export const actions: ActionTree & }) }, [AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE]( - context: ActionContext + context: ActionContext, + updateUI: boolean = false ): void { context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES) authApi .get('auth/profile') .then((res) => { if (res.data.status === 'success') { + const profileNotLoaded = + context.getters[AUTH_USER_STORE.GETTERS.IS_PROFILE_NOT_LOADED] context.commit( AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_USER_PROFILE, res.data.data @@ -133,16 +136,18 @@ export const actions: ActionTree & // refresh privacy policy context.dispatch(ROOT_STORE.ACTIONS.GET_APPLICATION_PRIVACY_POLICY) } - if (res.data.data.language) { - context.dispatch( - ROOT_STORE.ACTIONS.UPDATE_APPLICATION_LANGUAGE, - res.data.data.language + if (profileNotLoaded || updateUI) { + if (res.data.data.language) { + context.dispatch( + ROOT_STORE.ACTIONS.UPDATE_APPLICATION_LANGUAGE, + res.data.data.language + ) + } + context.commit( + ROOT_STORE.MUTATIONS.UPDATE_DARK_MODE, + res.data.data.use_dark_mode ) } - context.commit( - ROOT_STORE.MUTATIONS.UPDATE_DARK_MODE, - res.data.data.use_dark_mode - ) context.dispatch(SPORTS_STORE.ACTIONS.GET_SPORTS) } else { handleError(context, null) @@ -174,7 +179,7 @@ export const actions: ActionTree & window.localStorage.setItem('authToken', token) context.commit(AUTH_USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN, token) context - .dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE) + .dispatch(AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE, true) .then(() => router.push( typeof data.redirectUrl === 'string' ? data.redirectUrl : '/' diff --git a/fittrackee_client/src/store/modules/authUser/enums.ts b/fittrackee_client/src/store/modules/authUser/enums.ts index b520b11e..0ccb8f72 100644 --- a/fittrackee_client/src/store/modules/authUser/enums.ts +++ b/fittrackee_client/src/store/modules/authUser/enums.ts @@ -26,6 +26,7 @@ export enum AuthUserGetters { AUTH_USER_PROFILE = 'AUTH_USER_PROFILE', IS_ADMIN = 'IS_ADMIN', IS_AUTHENTICATED = 'IS_AUTHENTICATED', + IS_PROFILE_NOT_LOADED = 'IS_PROFILE_NOT_LOADED', IS_SUCCESS = 'IS_SUCCESS', IS_REGISTRATION_SUCCESS = 'IS_REGISTRATION_SUCCESS', USER_LOADING = 'USER_LOADING', diff --git a/fittrackee_client/src/store/modules/authUser/getters.ts b/fittrackee_client/src/store/modules/authUser/getters.ts index 7e866b42..7dccc7ea 100644 --- a/fittrackee_client/src/store/modules/authUser/getters.ts +++ b/fittrackee_client/src/store/modules/authUser/getters.ts @@ -35,4 +35,7 @@ export const getters: GetterTree & [AUTH_USER_STORE.GETTERS.USER_LOADING]: (state: IAuthUserState) => { return state.loading }, + [AUTH_USER_STORE.GETTERS.IS_PROFILE_NOT_LOADED]: (state: IAuthUserState) => { + return state.authUserProfile.username === undefined + }, } diff --git a/fittrackee_client/src/store/modules/authUser/types.ts b/fittrackee_client/src/store/modules/authUser/types.ts index f297bfb9..b9d6d561 100644 --- a/fittrackee_client/src/store/modules/authUser/types.ts +++ b/fittrackee_client/src/store/modules/authUser/types.ts @@ -47,7 +47,8 @@ export interface IAuthUserActions { ): void [AUTH_USER_STORE.ACTIONS.GET_USER_PROFILE]( - context: ActionContext + context: ActionContext, + updateUI: boolean ): void [AUTH_USER_STORE.ACTIONS.LOGIN_OR_REGISTER]( @@ -142,6 +143,10 @@ export interface IAuthUserGetters { [AUTH_USER_STORE.GETTERS.IS_AUTHENTICATED](state: IAuthUserState): boolean + [AUTH_USER_STORE.GETTERS.IS_PROFILE_NOT_LOADED]( + state: IAuthUserState + ): boolean + [AUTH_USER_STORE.GETTERS.IS_REGISTRATION_SUCCESS]( state: IAuthUserState ): boolean