-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
73 lines (64 loc) · 2.51 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { createContext, ReactElement, ReactNode, useEffect, useReducer, useRef } from 'react';
import Reducer from './reducer';
import { ContextType, GlobalStateInterface } from './types';
/**
* React Context-based Global Store with a reducer
* and persistent saves to sessionStorage/localStorage
**/
export function GlobalStore({ children }: { children: ReactNode }): ReactElement {
const [globalState, dispatch] = useReducer(Reducer, initializeState());
const initialRenderGlobalState = useRef(true);
const initialRenderPersistenceType = useRef(true);
useEffect(() => {
/*
populate either sessionStorage or localStorage
data from globalState based on persistenceType
*/
if (initialRenderGlobalState.current) {
initialRenderGlobalState.current = false;
return;
}
const getPersistenceType = globalState.persistenceType;
if (getPersistenceType === 'sessionStorage') {
sessionStorage.setItem('globalState', JSON.stringify(globalState));
} else if (getPersistenceType === 'localStorage') {
localStorage.setItem('globalState', JSON.stringify(globalState));
}
}, [globalState]);
useEffect(() => {
/*
purge sessionStorage or localStorage when either is selected
*/
if (initialRenderPersistenceType.current) {
initialRenderPersistenceType.current = false;
return;
}
const getPersistenceType = globalState.persistenceType;
if (getPersistenceType === 'sessionStorage') {
localStorage.removeItem('globalState');
} else if (getPersistenceType === 'localStorage') {
sessionStorage.removeItem('globalState');
}
}, [globalState.persistenceType]);
return <globalContext.Provider value={{ globalState, dispatch }}>{children}</globalContext.Provider>;
}
export const globalContext = createContext({} as ContextType);
export const initialState: GlobalStateInterface = {
favoriteMovies: [],
isUserAuthenticated: false,
loggedUser: '',
persistenceType: 'sessionStorage',
};
function initializeState() {
/*
the order in which the the data is compared is very important;
first try to populate the state from Storage if not return initialState
*/
if (typeof (Storage) !== 'undefined') {
} else {
throw new Error('You need to enable Storage to run this app.');
}
const fromLocalStorage = JSON.parse(localStorage.getItem('globalState') as string);
const fromSessionStorage = JSON.parse(sessionStorage.getItem('globalState') as string);
return fromSessionStorage || fromLocalStorage || initialState;
}