forked from leanflutter/flutter_distributor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
76 lines (66 loc) · 1.71 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
import { useMemo } from "react";
import { createStore, applyMiddleware, Action, Store, compose } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import {
PersistConfig,
Persistor,
persistReducer,
persistStore,
} from "redux-persist";
import storage from "redux-persist/lib/storage";
export interface AppState {
user: any;
value: number;
}
let _store: Store<AppState> | undefined;
let _persistor: Persistor | undefined;
const exampleInitialState: AppState = {
user: null,
value: 0,
};
export const actionTypes = {
LOGIN: "LOGIN",
LOGOUT: "LOGOUT",
};
// REDUCERS
export const reducer = (state: AppState = exampleInitialState, action: any) => {
switch (action.type) {
case actionTypes.LOGIN:
return {
...state,
user: action.user,
};
case actionTypes.LOGOUT:
return {
...state,
user: null,
};
default:
return state;
}
};
// ACTIONS
export const login = (user: any) => {
return { type: actionTypes.LOGIN, user };
};
export const logout = () => {
return { type: actionTypes.LOGOUT };
};
const persistConfig: PersistConfig<AppState, any> = {
key: "primary",
storage,
whitelist: ["user"], // place to select which state you want to persist
};
const persistedReducer = persistReducer(persistConfig, reducer);
function makeStore(initialState: any = exampleInitialState): Store<AppState> {
return createStore(
persistedReducer,
initialState,
composeWithDevTools(applyMiddleware())
);
}
export const configureStore = () => {
if (_store == undefined) _store = makeStore();
if (_persistor == undefined) _persistor = persistStore(_store);
return { store: _store, persistor: _persistor };
};