-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
86 lines (72 loc) · 2.09 KB
/
store.js
File metadata and controls
86 lines (72 loc) · 2.09 KB
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
import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import omit from 'lodash/omit'
import uniqid from 'uniqid'
import { persistStore, autoRehydrate } from 'redux-persist'
import { REHYDRATE } from 'redux-persist/constants'
const initialState = {
people: {},
formEditPerson: null
}
export const actionTypes = {
EDIT_PERSON: 'EDIT_PERSON',
SELECT_PERSON: 'SELECT_PERSON',
REMOVE_PERSON: 'REMOVE_PERSON'
}
// REDUCERS
export const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.EDIT_PERSON:
const id = action.data.id != null ? action.data.id : uniqid.time();
return {
...state,
people: {
...state.people,
[id]: {
...action.data,
id,
}
}
}
case actionTypes.REMOVE_PERSON:
return {
...state,
people: omit(state.people, [action.id])
}
case actionTypes.SELECT_PERSON:
return {
...state,
formEditPerson: state.people[action.id]
}
case REHYDRATE:
const people = action.payload.people
return {people: people}
default:
return state
}
}
// ACTIONS
export const editPerson = (data) => dispatch => {
return dispatch({ type: actionTypes.EDIT_PERSON, data })
}
export const removePerson = (id) => dispatch => {
return dispatch({ type: actionTypes.REMOVE_PERSON, id })
}
export const selectPerson = (id) => dispatch => {
return dispatch({ type: actionTypes.SELECT_PERSON, id })
}
export const initStore = (initialState = initialState) => {
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
}) : compose;
const store = createStore(reducer, initialState, composeEnhancers(
applyMiddleware(thunkMiddleware),
autoRehydrate()
))
// begin periodically persisting the store
persistStore(store)
return store;
}