forked from piranna/redux-offline-crud-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreducer.js
163 lines (110 loc) · 3.44 KB
/
reducer.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const genActionTypes = require('./actionTypes')
function deleteIndex(array, index) {
array.splice(index, 1)
}
function idStrictEqual(element) {
const { id, uid } = element // TODO deprecate `uid`
return (id || uid) === this.toString()
}
function reduceNamespaces(acum, name) {
return { ...acum, [name]: reducer(name, this) }
}
const defaultOnRollback = console.error.bind(console)
function reducer(basePath, options = {}) {
if (Array.isArray(basePath)) {
return basePath.reduce(reduceNamespaces.bind(options), {})
}
if (basePath.endsWith('/')) basePath = basePath.slice(0, basePath.length - 1)
const { childReducer } = options
const onRollback = options.onRollback || defaultOnRollback
const actionTypes = genActionTypes(basePath)
return (state = [], action) => {
const { meta, payload, type } = action
if (!type.startsWith(basePath)) return state
let result = [...state]
let index = -1
let item
const { func, id } = (meta || {})
if (meta) {
if (func) {
if (type.endsWith('commit')) setImmediate(func, null, payload)
else setImmediate(func, payload)
return state
}
if (id) {
index = result.findIndex(idStrictEqual, id)
item = result[index]
}
}
switch (type) {
// Create
case actionTypes.create:
result.push({ ...payload, id })
break
case actionTypes.create_commit:
result[index] = { ...item, id: payload }
break
case actionTypes.create_rollback:
onRollback(payload)
deleteIndex(result, index)
break
// Read
case actionTypes.read:
console.info(actionTypes.read)
return state
case actionTypes.read_commit:
// Collection
if (Array.isArray(payload)) result = [...payload]
// Non-existing resource
else if (index === -1) result.push({ ...payload })
// Existing resource
else result[index] = { ...payload }
break
case actionTypes.read_rollback:
onRollback(payload)
return state
// Update & Patch
case actionTypes.update:
result[index] = { ...payload, _rollback: item }
break
case actionTypes.patch:
result[index] = { ...item, ...payload, _rollback: item }
break
case actionTypes.update_commit:
case actionTypes.patch_commit:
result[index] = { ...item, _rollback: undefined }
break
case actionTypes.update_rollback:
case actionTypes.patch_rollback:
onRollback(payload)
result[index] = { ...item._rollback }
break
// Delete
case actionTypes.delete:
result[index] = { ...item, _pendingDeletion: true }
break
case actionTypes.delete_commit:
deleteIndex(result, index)
break
case actionTypes.delete_rollback:
onRollback(payload)
result[index] = { ...item, _pendingDeletion: undefined }
break
// Unknown action, use child redurec or return untouched current state
default:
if (!childReducer) return state
if (!item) {
// TODO notify user
return state
}
item.projects = childReducer(item.projects,
{
...action,
meta: { ...meta, id: meta.project_id, project_id: undefined },
})
}
// Return modified state
return result
}
}
module.exports = reducer