@@ -13,7 +13,7 @@ export function isSettingExist({ settings, targetDevice, targetType }: IProps): return null } - for (let i = 0; settings.length; i++) { + for (let i = 0; i < settings.length; i++) { if (settings[i].type === targetType && settings[i].device === targetDevice) { return settings[i] } @@ -56,20 +56,31 @@ const initialState: { state: IUserSetting[] | null } = { export const settingsSlice = createSlice({ name: 'balance', initialState, - reducers: {}, + reducers: { + setSettings: (state, action) => { + state.state = action.payload + }, + }, extraReducers: (builder) => { builder.addCase(getUserAccountSettings.fulfilled, (state, action) => { state.state = action.payload + localStorage.setItem('global_settings', JSON.stringify(action.payload)) }) builder.addCase(addUserAccountSettings.fulfilled, (state, action) => { if (state.state === null) state.state = [] - if (action.payload) state.state = [...state.state, action.payload] + if (action.payload) { + state.state = [...state.state, action.payload] + localStorage.setItem('global_settings', JSON.stringify([...state.state, action.payload])) + } }) builder.addCase(updateUserAccountSettings.fulfilled, (state, action) => { - if (action.payload) state.state = action.payload + if (action.payload) { + state.state = action.payload + localStorage.setItem('global_settings', JSON.stringify(action.payload)) + } }) }, }) -export default settingsSlice.reducer +export const { setSettings } = settingsSlice.actions @@ -9,7 +9,7 @@ import { getUserBalance } from '@/src/entities/balance' import { useTheme } from '@/src/entities/theme' import { getAllInfo } from '@/src/entities/user-account' import { isSettingExist } from '@/src/entities/user-account/lib/helpers/is-setting-exist' -import { getUserAccountSettings } from '@/src/entities/user-account/model/settings' +import { getUserAccountSettings, setSettings } from '@/src/entities/user-account/model/settings' import InfoBar from '@/src/main/layout/ui/info-bar' import { useAppDispatch, useAppSelector } from '@/src/main/store/store' import { Loader } from '@/src/shared' @@ -44,6 +44,7 @@ export const Layout: React.FC = ({ const desktop = device === 'desktop' const [sidemenuDefaultOpen, setSidemenuDefaultOpen] = useState(true) + const [readyToDisplay, setReadyToDisplay] = useState(false) useTheme() @@ -63,11 +64,26 @@ export const Layout: React.FC = ({ }, [status]) React.useEffect(() => { - if (sessionData && appState.settings.state === null) { + if ((sessionData && !sessionStorage.getItem('firstRender')) || (sessionData && !localStorage.getItem('global_settings'))) { dispatch(getUserAccountSettings(sessionData.access)) + sessionStorage.setItem('firstRender', 'true') + setReadyToDisplay(true) } }, [sessionData]) + React.useEffect(() => { + const lsData = localStorage.getItem('global_settings') + if (lsData !== null) { + dispatch(setSettings(JSON.parse(lsData))) + setReadyToDisplay(true) + } + + window.addEventListener('beforeunload', () => { + sessionStorage.removeItem('firstRender') + sessionStorage.clear() + }) + }, []) + React.useEffect(() => { if (appState.settings.state !== null && device) { let setting = isSettingExist({ @@ -114,44 +130,36 @@ export const Layout: React.FC = ({ padding: isAuthPage ? '0px' : desktop ? '0px' : '10px', }} > - - - {!isAuthPage ? ( - desktop ? ( - - - - - - {children} + {readyToDisplay && ( + + + {!isAuthPage ? ( + desktop ? ( + + + + + + {children} + - + ) : ( + + + {children} + + ) ) : ( - - - {children} - - ) - ) : ( - <>{children} - )} - - + <>{children} + )} + + + )} )