generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from rishabhsharma1997/redux/middleware
Redux/middleware
- Loading branch information
Showing
8 changed files
with
1,507 additions
and
573 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { ThunkDispatch } from '@reduxjs/toolkit'; | ||
import { FC, ReactNode, useEffect, useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { Dispatch } from 'redux'; | ||
import { RehydrateStateAction } from './initReduxPersist'; | ||
|
||
interface PersistedStateProviderProps { | ||
children: ReactNode; | ||
loadPersistedState: () => (dispatch: Dispatch<RehydrateStateAction>) => void; | ||
} | ||
|
||
export const PersistedStateProvider: FC<PersistedStateProviderProps> = ({ | ||
children, | ||
loadPersistedState | ||
}) => { | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
const dispatch = useDispatch<ThunkDispatch<any, unknown, RehydrateStateAction>>(); | ||
|
||
useEffect(() => { | ||
if (!loading) { | ||
return; | ||
} | ||
try { | ||
dispatch(loadPersistedState()); | ||
} catch (e) { | ||
setError(e as Error); | ||
} | ||
setLoading(false); | ||
}, [loading, dispatch, loadPersistedState]); | ||
|
||
if (error) { | ||
console.error('Error Loading Persisted State', error); | ||
} | ||
|
||
if (loading) { | ||
return null; | ||
} | ||
|
||
return <>{children}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './PersistedStateProvider'; | ||
export * from './initReduxPersist'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import _ from 'lodash'; | ||
import { Dispatch, Store } from 'redux'; | ||
|
||
const INVALID_REDUCER_PATH = Symbol('INVALID_REDUCER_PATH'); | ||
const REHYDRATE_STATE_ACTION = 'REHYDRATE_STATE_ACTION'; | ||
|
||
export interface RehydrateStateAction { | ||
type: typeof REHYDRATE_STATE_ACTION; | ||
payload: { | ||
reducerPath: string; | ||
inflatedState: unknown; | ||
}; | ||
} | ||
|
||
type Action = RehydrateStateAction | { type: string; [key: string]: unknown }; | ||
|
||
interface ActionsToPersist { | ||
[actionType: string]: string[]; | ||
} | ||
|
||
const rehydrateState = (reducerPath: string, inflatedState: unknown): RehydrateStateAction => ({ | ||
type: REHYDRATE_STATE_ACTION, | ||
payload: { | ||
reducerPath, | ||
inflatedState | ||
} | ||
}); | ||
|
||
const rehydrateStateReducer = (state: unknown, action: Action): unknown => { | ||
if (action.type === REHYDRATE_STATE_ACTION) { | ||
const appState = _.cloneDeep(state); | ||
_.set( | ||
appState as object, | ||
(action.payload as RehydrateStateAction['payload']).reducerPath.split('/'), | ||
(action.payload as RehydrateStateAction['payload']).inflatedState | ||
); | ||
return appState; | ||
} | ||
return state; | ||
}; | ||
|
||
export const initReduxPersist = (actionsToPersist: ActionsToPersist) => { | ||
const createPersistEnhancedReducer = | ||
(reducer: (state: unknown, action: Action) => unknown) => | ||
(state: unknown, action: Action): unknown => { | ||
const newState = rehydrateStateReducer(state, action); | ||
return reducer(newState, action); | ||
}; | ||
|
||
const persistMiddleware = | ||
(store: Store) => | ||
(next: (action: Action) => unknown) => | ||
(action: Action): unknown => { | ||
const result = next(action); | ||
|
||
const reducersToPersist = actionsToPersist[action.type]; | ||
|
||
if (reducersToPersist) { | ||
const appState = store.getState(); | ||
reducersToPersist.forEach((reducerPath) => { | ||
const path = reducerPath.split('/'); | ||
const stateToPersist = _.get(appState, path, INVALID_REDUCER_PATH); | ||
|
||
if (stateToPersist === INVALID_REDUCER_PATH) { | ||
throw new Error(`Reducer Path to Persist Is Invalid: ${reducerPath}`); | ||
} | ||
|
||
localStorage.setItem(reducerPath, JSON.stringify(stateToPersist)); | ||
}); | ||
} | ||
return result; | ||
}; | ||
|
||
const loadPersistedState = () => (dispatch: Dispatch<RehydrateStateAction>) => { | ||
Object.values(actionsToPersist).forEach((reducerPaths) => { | ||
reducerPaths.forEach((path) => { | ||
let inflatedState = localStorage.getItem(path); | ||
try { | ||
if (inflatedState) { | ||
inflatedState = JSON.parse(inflatedState); | ||
dispatch(rehydrateState(path, inflatedState)); | ||
} | ||
} catch (e) { | ||
console.error(`Error rehydrating state for reducer ${path}`, inflatedState); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
return { | ||
persistMiddleware, | ||
createPersistEnhancedReducer, | ||
loadPersistedState | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters