Skip to content

Commit

Permalink
Try to cache state
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Oct 10, 2023
1 parent 2341e88 commit e31ada2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Provider as ReduxProvider } from 'react-redux';

import { ER_INTERN_FLATE } from './constant';
import FeatureToggle from './moduler/feature/FeatureToggle';
import createStore from './store';
import createStore, { getPreloadedStoreFromSessionStorage } from './store';

interface Props {
children: React.ReactNode;
Expand Down Expand Up @@ -33,7 +33,9 @@ const Provider = ({ children, setFnrRef, fnr: propFnr }: Props) => {
};
}, []);

const store = useMemo(createStore, [fnr]);
const store = useMemo(() => {
return createStore(getPreloadedStoreFromSessionStorage());
}, [fnr]);

return (
<FnrContext.Provider value={fnr}>
Expand Down
4 changes: 3 additions & 1 deletion src/moduler/dialog/DialogFlateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MouseEvent } from 'react';

import { ARBEIDSRETTET_DIALOG_URL } from '../../constant';
import { saveReduxStateToSessionStorage } from '../../store';

interface DialogEventDetails {
dialogId?: string;
Expand All @@ -18,14 +19,15 @@ export const byttTilDialogFlate = ({
dialogId?: string;
}) => {
event.preventDefault();
saveReduxStateToSessionStorage();
window.history.pushState('', 'Dialog', getDialogLenke({ erVeileder: true, aktivitetId, dialogId }));
window.dispatchEvent(
new CustomEvent<DialogEventDetails>('visDialog', {
detail: {
dialogId: dialogId,
aktivitetId: aktivitetId,
},
})
}),
);
};

Expand Down
32 changes: 29 additions & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import { configureStore } from '@reduxjs/toolkit';

import reducer from './reducer';
import { EnhancedStore } from '@reduxjs/toolkit/src/configureStore';

const createStore = () =>
configureStore({
type Store = ReturnType<typeof createStore>;

let store: EnhancedStore | null = null;
const createStore = (preloadedState: any = undefined): EnhancedStore => {
const newStore: EnhancedStore = configureStore({
reducer: reducer,
preloadedState,
});
store = newStore;
return newStore;
};

const key = 'aktivitetsplan-state';
export const getPreloadedStoreFromSessionStorage = (): Store | undefined => {
const serializedState = sessionStorage.getItem(key);
if (serializedState) {
try {
console.log('Cache hit');
return JSON.parse(serializedState);
} catch (e) {
console.warn(e);
return undefined;
}
}
return undefined;
};
export const saveReduxStateToSessionStorage = () => {
const state = JSON.stringify(store?.getState());
sessionStorage.setItem(key, JSON.stringify(state));
};

type Store = ReturnType<typeof createStore>;
export type RootState = ReturnType<Store['getState']>;
export type Dispatch = Store['dispatch'];

Expand Down

0 comments on commit e31ada2

Please sign in to comment.