|
| 1 | +import _ from 'lodash'; |
| 2 | +import { Dispatch, Store } from 'redux'; |
| 3 | + |
| 4 | +const INVALID_REDUCER_PATH = Symbol('INVALID_REDUCER_PATH'); |
| 5 | +const REHYDRATE_STATE_ACTION = 'REHYDRATE_STATE_ACTION'; |
| 6 | + |
| 7 | +export interface RehydrateStateAction { |
| 8 | + type: typeof REHYDRATE_STATE_ACTION; |
| 9 | + payload: { |
| 10 | + reducerPath: string; |
| 11 | + inflatedState: unknown; |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +type Action = RehydrateStateAction | { type: string; [key: string]: unknown }; |
| 16 | + |
| 17 | +interface ActionsToPersist { |
| 18 | + [actionType: string]: string[]; |
| 19 | +} |
| 20 | + |
| 21 | +const rehydrateState = (reducerPath: string, inflatedState: unknown): RehydrateStateAction => ({ |
| 22 | + type: REHYDRATE_STATE_ACTION, |
| 23 | + payload: { |
| 24 | + reducerPath, |
| 25 | + inflatedState |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +const rehydrateStateReducer = (state: unknown, action: Action): unknown => { |
| 30 | + if (action.type === REHYDRATE_STATE_ACTION) { |
| 31 | + const appState = _.cloneDeep(state); |
| 32 | + _.set( |
| 33 | + appState as object, |
| 34 | + (action.payload as RehydrateStateAction['payload']).reducerPath.split('/'), |
| 35 | + (action.payload as RehydrateStateAction['payload']).inflatedState |
| 36 | + ); |
| 37 | + return appState; |
| 38 | + } |
| 39 | + return state; |
| 40 | +}; |
| 41 | + |
| 42 | +export const initReduxPersist = (actionsToPersist: ActionsToPersist) => { |
| 43 | + const createPersistEnhancedReducer = |
| 44 | + (reducer: (state: unknown, action: Action) => unknown) => |
| 45 | + (state: unknown, action: Action): unknown => { |
| 46 | + const newState = rehydrateStateReducer(state, action); |
| 47 | + return reducer(newState, action); |
| 48 | + }; |
| 49 | + |
| 50 | + const persistMiddleware = |
| 51 | + (store: Store) => |
| 52 | + (next: (action: Action) => unknown) => |
| 53 | + (action: Action): unknown => { |
| 54 | + const result = next(action); |
| 55 | + |
| 56 | + const reducersToPersist = actionsToPersist[action.type]; |
| 57 | + |
| 58 | + if (reducersToPersist) { |
| 59 | + const appState = store.getState(); |
| 60 | + reducersToPersist.forEach((reducerPath) => { |
| 61 | + const path = reducerPath.split('/'); |
| 62 | + const stateToPersist = _.get(appState, path, INVALID_REDUCER_PATH); |
| 63 | + |
| 64 | + if (stateToPersist === INVALID_REDUCER_PATH) { |
| 65 | + throw new Error(`Reducer Path to Persist Is Invalid: ${reducerPath}`); |
| 66 | + } |
| 67 | + |
| 68 | + localStorage.setItem(reducerPath, JSON.stringify(stateToPersist)); |
| 69 | + }); |
| 70 | + } |
| 71 | + return result; |
| 72 | + }; |
| 73 | + |
| 74 | + const loadPersistedState = () => (dispatch: Dispatch<RehydrateStateAction>) => { |
| 75 | + Object.values(actionsToPersist).forEach((reducerPaths) => { |
| 76 | + reducerPaths.forEach((path) => { |
| 77 | + let inflatedState = localStorage.getItem(path); |
| 78 | + try { |
| 79 | + if (inflatedState) { |
| 80 | + inflatedState = JSON.parse(inflatedState); |
| 81 | + dispatch(rehydrateState(path, inflatedState)); |
| 82 | + } |
| 83 | + } catch (e) { |
| 84 | + console.error(`Error rehydrating state for reducer ${path}`, inflatedState); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + return { |
| 91 | + persistMiddleware, |
| 92 | + createPersistEnhancedReducer, |
| 93 | + loadPersistedState |
| 94 | + }; |
| 95 | +}; |
0 commit comments