generated from nandiheath/gatsby-template-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcontextStore.js
95 lines (84 loc) · 2.22 KB
/
contextStore.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from "react"
import drawerReducer from "@/reducers/drawer"
import routeReducer from "@/reducers/route"
import pageOptionsReducer, {
FONT_ZOOM_LOCALSTORAGE_KEY,
} from "@/reducers/pageOptions"
import casesReducer, { CASES_CARD_VIEW } from "@/reducers/cases"
import { loadFromLocalStorage } from "@/utils"
export const drawerInitialState = {
open: false,
}
export const routeInitialState = {
path: "/",
fullPath: "/",
}
export const pageOptionsInitialState = {
closedAlerts: [],
fontZoom: parseFloat(
loadFromLocalStorage(FONT_ZOOM_LOCALSTORAGE_KEY) || "1.0"
),
}
export const casesInitialState = {
view: CASES_CARD_VIEW,
// 1: by date : from latest to oldest
// 2: by date : from oldest to latest
// 3: by area : from greatest to least
// 4: by area : from least to greatest
// 5: by group : from more to less
// 6: by group : from less to more
// 7: by status
group: 1,
}
const ContextStore = React.createContext({
drawer: drawerInitialState,
route: routeInitialState,
pageOptions: pageOptionsInitialState,
cases: casesInitialState,
})
export const ContextStoreProvider = props => {
const { initialStore = {} } = props
const [drawerState, drawerDispatch] = React.useReducer(drawerReducer, {
...drawerInitialState,
...initialStore.drawer,
})
const [routeState, routeDispatch] = React.useReducer(routeReducer, {
...routeInitialState,
...initialStore.route,
})
const [pageOptionsState, pageOptionsDispatch] = React.useReducer(
pageOptionsReducer,
{
...pageOptionsInitialState,
...initialStore.pageOptions,
}
)
const [casesState, casesDispatch] = React.useReducer(casesReducer, {
...casesInitialState,
...initialStore.cases,
})
return (
<ContextStore.Provider
value={{
drawer: {
state: drawerState,
dispatch: drawerDispatch,
},
route: {
state: routeState,
dispatch: routeDispatch,
},
pageOptions: {
state: pageOptionsState,
dispatch: pageOptionsDispatch,
},
cases: {
state: casesState,
dispatch: casesDispatch,
},
}}
{...props}
/>
)
}
export default ContextStore