generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [NMP-79] Frontend State Management (#92)
- Loading branch information
1 parent
6c655e4
commit 472690b
Showing
11 changed files
with
219 additions
and
16 deletions.
There are no files selected for viewing
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,5 @@ | ||
const constants = { | ||
NMP_FILE_KEY: 'nmpFile', | ||
}; | ||
|
||
export default constants; |
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,27 @@ | ||
/* eslint-disable react-refresh/only-export-components */ | ||
/** | ||
* @summary context provider for app data | ||
*/ | ||
import { createContext, ReactNode } from 'react'; | ||
import BaseProvider from './BaseProvider'; | ||
import { initialState, reducer } from '../services/app/AppReducer'; | ||
|
||
export const AppContext = createContext(initialState); | ||
|
||
type AppProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
function AppProvider({ children }: AppProviderProps) { | ||
return ( | ||
<BaseProvider | ||
Context={AppContext} | ||
initialState={initialState} | ||
reducer={reducer} | ||
> | ||
{children} | ||
</BaseProvider> | ||
); | ||
} | ||
|
||
export default AppProvider; |
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,33 @@ | ||
/** | ||
* @summary A base context provider to avoid repeating code. | ||
* @param Context The context of the given piece of state, | ||
* this is what gets returned from React.useContext | ||
* @param reducer The reducer for the piece of state. | ||
* @param initialState The initial values for the piece of state. | ||
* @param children The child component of this component | ||
*/ | ||
import { Context as ContextType, ReactNode, useReducer, useMemo } from 'react'; | ||
|
||
type BaseProviderProps<ContextObjType, StateType, ChildCompsType> = { | ||
Context: ContextType<ContextObjType>; | ||
reducer: (state: StateType, action: any) => StateType; | ||
initialState: StateType; | ||
children: ChildCompsType; | ||
}; | ||
|
||
export default function BaseProvider< | ||
ContextObjType extends Record<string, any>, | ||
StateType extends Record<string, any>, | ||
ChildCompsType extends ReactNode, | ||
>(props: BaseProviderProps<ContextObjType, StateType, ChildCompsType>) { | ||
const { Context, reducer, initialState, children } = props; | ||
|
||
const [state, dispatch] = useReducer(reducer, initialState); | ||
|
||
const contextValue = useMemo(() => ({ state, dispatch }), [state, dispatch]); | ||
return ( | ||
<Context.Provider value={contextValue as unknown as ContextObjType}> | ||
{children} | ||
</Context.Provider> | ||
); | ||
} |
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,6 @@ | ||
/* eslint-disable no-shadow */ | ||
enum AppActionType { | ||
SET_NMP_FILE = 'SET_NMP_FILE', | ||
} | ||
|
||
export default AppActionType; |
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,28 @@ | ||
import AppActionType from './AppActions'; | ||
|
||
const { SET_NMP_FILE } = AppActionType; | ||
|
||
export type AppAction = { | ||
type: AppActionType; | ||
payload?: object; | ||
}; | ||
|
||
// Initial settings state. | ||
export const initialState = { | ||
nmpFile: '', | ||
}; | ||
|
||
/** | ||
* @summary Handles app actions and returns the updated app state. | ||
* @param {object} state - The current app state. | ||
* @param {AppAction} action - The app action to be handled. | ||
* @returns {object} - The updated app state. | ||
*/ | ||
export const reducer = (state: object, action: AppAction): object => { | ||
switch (action.type) { | ||
case SET_NMP_FILE: | ||
return { ...state, nmpFile: action.payload }; | ||
default: | ||
throw new Error(); | ||
} | ||
}; |
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,36 @@ | ||
/* eslint-disable no-console */ | ||
import { useContext, useMemo } from 'react'; | ||
import constants from '../../constants/Constants'; | ||
import { AppContext } from '../../providers/AppProvider'; | ||
import AppActionType from './AppActions'; | ||
import { saveDataToLocalStorage } from '../../utils/AppLocalStorage'; | ||
|
||
const { SET_NMP_FILE } = AppActionType; | ||
|
||
/** | ||
* @summary Custom hook that provides app related functions | ||
*/ | ||
const useAppService = () => { | ||
const { state, dispatch } = useContext<any>(AppContext); | ||
|
||
return useMemo(() => { | ||
/** | ||
* @summary Set nmp to local storage and state | ||
*/ | ||
const setNMPFile = async (nmpFile: string | ArrayBuffer) => { | ||
try { | ||
saveDataToLocalStorage(constants.NMP_FILE_KEY, nmpFile); | ||
dispatch({ type: SET_NMP_FILE, payload: nmpFile }); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
return { | ||
setNMPFile, | ||
state, | ||
}; | ||
}, [state, dispatch]); | ||
}; | ||
|
||
export default useAppService; |
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,40 @@ | ||
/** | ||
* @summary Saves data to localStorage | ||
* @param key is the name that the data will be stored by in localStorage | ||
* @param data is the data to be stored | ||
* @type {( key: string, data: any)} | ||
*/ | ||
export const saveDataToLocalStorage = (key: string, data: any) => { | ||
try { | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log(error); | ||
} | ||
}; | ||
|
||
/** | ||
* @summary Retrieves data from localStorage if the key exists | ||
* @param key is the key name used to store the value in localStorage | ||
* @type {( key: string )} | ||
* @returns the parsed JSON data or null if the key does not exist in localStorage | ||
*/ | ||
export const getDataFromLocalStorage = (key: string) => { | ||
const data = localStorage.getItem(key); | ||
return data ? JSON.parse(data) : null; | ||
}; | ||
|
||
/** | ||
* @summary Checks to see if the key exists in localstorage | ||
* @param key is the key name used to store the value in localStorage | ||
* @type {( key: string )} | ||
* @returns boolean values | ||
*/ | ||
export const localStorageKeyExists = (key: string) => getDataFromLocalStorage(key) !== null; | ||
|
||
/** | ||
* @summary Deletes localStorage key | ||
* @param key is the key name used to store the value in localStorage | ||
* @type {( key: string )} | ||
*/ | ||
export const deleteLocalStorageKey = (key: string) => localStorage.removeItem(key); |
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