-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.ts
122 lines (112 loc) · 2.69 KB
/
store.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { useMemo } from "react";
import { createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
let store;
const initialState = {
siteurl: null,
list: [],
approved: null,
auth: null,
done: {
success: [],
failure: [],
},
uploading: false,
authenticating: false,
columnMappings: null,
};
export type RootState = ReturnType<typeof reducer>;
const reducer = (state = initialState, action) => {
switch (action.type) {
case "CHANGE_SITE_URL":
return {
...state,
siteurl: action.payload,
};
case "START_UPLOADING":
return {
...state,
uploading: true,
};
case "STOP_UPLOADING":
return {
...state,
uploading: false,
};
case "START_AUTHENTICATING":
return {
...state,
authenticating: true,
};
case "STOP_AUTHENTICATING":
return {
...state,
authenticating: false,
};
case "SET_AUTH":
return {
...state,
auth: action.payload,
};
case "SET_LIST":
return {
...state,
list: action.payload,
};
case "APPROVE_LIST":
return {
...state,
approved: action.payload,
};
case "ADD_DONE":
return {
...state,
done:
action.payload.status === "success"
? {
...state.done,
success: state.done.success.concat(action.payload.record),
}
: {
...state.done,
failure: state.done.failure.concat(action.payload.record),
},
};
case "ASSIGN_COLUMNS":
return {
...state,
columnMappings: action.payload,
};
default:
return state;
}
};
function initStore(preloadedState = initialState) {
return createStore(
reducer,
preloadedState,
composeWithDevTools(applyMiddleware())
);
}
export const initializeStore = (preloadedState?: any) => {
let _store = store ?? initStore(preloadedState);
// After navigating to a page with an initial Redux state, merge that state
// with the current state in the store, and create a new store
if (preloadedState && store) {
_store = initStore({
...store.getState(),
...preloadedState,
});
// Reset the current store
store = undefined;
}
// For SSG and SSR always create a new store
if (typeof window === "undefined") return _store;
// Create the store once in the client
if (!store) store = _store;
return _store;
};
export function useStore(initialState) {
const store = useMemo(() => initializeStore(initialState), [initialState]);
return store;
}