-
Notifications
You must be signed in to change notification settings - Fork 109
/
state-mutation.js
270 lines (218 loc) · 7.47 KB
/
state-mutation.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as imm from 'object-path-immutable';
import pluralize from 'pluralize';
import equal from 'deep-equal';
import { hasOwnProperties } from './utils';
export const makeUpdateReverseRelationship = (
resource,
relationship,
newRelation = {
type: resource.type,
id: resource.id
}
) => {
return (foreignResources) => {
const idx = foreignResources.findIndex((item) => (
item.id === relationship.data.id
));
if (idx === -1) {
return foreignResources;
}
const [singular, plural] = [1, 2].map((i) => pluralize(resource.type, i));
const relCase = [singular, plural]
.find((r) => (
hasOwnProperties(foreignResources[idx], ['relationships', r])
));
if (!relCase) {
return foreignResources;
}
const relPath = ['relationships', relCase, 'data'];
const idxRelPath = [idx].concat(relPath);
const immutableForeingResources = imm.wrap(foreignResources);
if (!hasOwnProperties(foreignResources[idx], relPath)) {
return immutableForeingResources
.push(idxRelPath, newRelation)
.value();
}
const foreignResourceRel = foreignResources[idx].relationships[relCase].data;
if (
(
newRelation
&& Array.isArray(foreignResourceRel)
&& ~foreignResourceRel.findIndex( // eslint-disable-line
(rel) => rel.id === newRelation.id && rel.type === newRelation.type
)
)
|| (
newRelation
&& foreignResourceRel
&& foreignResourceRel.id === newRelation.id
&& foreignResourceRel.type === newRelation.type
)
) {
return foreignResources;
} if (Array.isArray(foreignResourceRel) && !newRelation) {
const relIdx = foreignResourceRel.findIndex((item) => (
item.id === resource.id
));
if (foreignResourceRel[relIdx]) {
const deletePath = [idx, 'relationships', singular, 'data', relIdx];
return imm.wrap(foreignResources).del(deletePath).value();
}
return foreignResources;
}
if (relCase === singular) {
return immutableForeingResources
.set(idxRelPath, newRelation)
.value();
}
return immutableForeingResources
.push(idxRelPath, newRelation)
.value();
};
};
const stateContainsResource = (state, resource) => {
const updatePath = [resource.type, 'data'];
if (hasOwnProperties(state, updatePath)) {
return state[resource.type].data.findIndex(
(item) => item.id === resource.id
) > -1;
}
return false;
};
const ensureUpdatedReverseRelationships = (state, resource) => {
let newState = state;
const rels = resource.relationships;
if (!rels) {
return newState;
}
Object.keys(rels).forEach((relKey) => {
if (!hasOwnProperties(rels[relKey], ['data', 'type'])) {
return;
}
const entityPath = [rels[relKey].data.type, 'data'];
if (!hasOwnProperties(newState, entityPath)) {
return;
}
const updateReverseRelationship = makeUpdateReverseRelationship(resource, rels[relKey]);
newState = imm.set(
newState,
entityPath,
updateReverseRelationship(newState[rels[relKey].data.type].data)
);
});
return newState;
};
export const addLinksToState = (state, links, options) => {
if (options === undefined || options.indexLinks === undefined) {
return state;
}
const indexLinkName = options.indexLinks;
return imm.set(state, `links.${indexLinkName}`, links);
};
export const updateRelationship = (state, resourceIdentifier, relationshipName, relationship) => {
if (typeof resourceIdentifier !== 'object') {
return state;
}
if (!stateContainsResource(state, resourceIdentifier)) {
return state;
}
let newState = state;
const updatePath = [resourceIdentifier.type, 'data'];
const resources = state[resourceIdentifier.type].data;
const idx = resources.findIndex((item) => item.id === resourceIdentifier.id);
newState = imm.set(newState, updatePath.concat(...[idx, 'relationships', relationshipName]), relationship);
return ensureUpdatedReverseRelationships(newState, resources[idx]);
};
export const updateOrInsertResource = (state, resource) => {
if (typeof resource !== 'object') {
return state;
}
let newState = state;
const updatePath = [resource.type, 'data'];
if (stateContainsResource(state, resource)) {
const resources = state[resource.type].data;
const idx = resources.findIndex((item) => item.id === resource.id);
const relationships = {};
if (resources[idx].hasOwnProperty('relationships')) {
Object.keys(resources[idx].relationships)
.forEach((relationship) => {
if (!hasOwnProperties(resource, ['relationships', relationship, 'data'])) {
relationships[relationship] = resources[idx].relationships[relationship];
}
});
}
if (!resource.hasOwnProperty('relationships')) {
Object.assign(resource, { relationships });
} else {
Object.assign(resource.relationships, relationships);
}
if (!equal(resources[idx], resource)) {
newState = imm.set(newState, updatePath.concat(idx), resource);
}
} else {
newState = imm.push(newState, updatePath, resource);
}
return ensureUpdatedReverseRelationships(newState, resource);
};
export const removeResourceFromState = (state, resource) => {
const index = state[resource.type].data.findIndex((e) => e.id === resource.id);
const path = [resource.type, 'data', index];
const entityRelationships = resource.relationships || {};
return Object.keys(entityRelationships).reduce((newState, key) => {
if (!resource.relationships[key].data) {
return newState;
}
const entityPath = [resource.relationships[key].data.type, 'data'];
if (hasOwnProperties(state, entityPath)) {
const updateReverseRelationship = makeUpdateReverseRelationship(
resource,
resource.relationships[key],
null
);
return newState.set(
entityPath,
updateReverseRelationship(
state[resource.relationships[key].data.type].data
)
);
}
return newState;
}, imm.wrap(state).del(path));
};
export const updateOrInsertResourcesIntoState = (state, resources) => (
resources.reduce(updateOrInsertResource, state)
);
export const setIsInvalidatingForExistingResource = (state, { type, id }, value = null) => {
const idx = state[type].data.findIndex((e) => e.id === id && e.type === type);
const updatePath = [type, 'data', idx, 'isInvalidating'];
return value === null
? imm.wrap(state).del(updatePath)
: imm.wrap(state).set(updatePath, value);
};
export const setIsInvalidatingForExistingRelationship = (
state,
{ type, id },
relationship,
value = null
) => {
const idx = state[type].data.findIndex((e) => e.id === id && e.type === type);
const updatePath = [type, 'data', idx, 'relationships', relationship, 'isInvalidating'];
return value === null ? imm.wrap(state)
.del(updatePath) : imm.wrap(state)
.set(updatePath, value);
};
export const ensureResourceTypeInState = (state, type) => {
const path = [type, 'data'];
return hasOwnProperties(state, [type])
? state
: imm.wrap(state).set(path, []).value();
};
export const ensureRelationshipInState = (state, { type, id }, relationship) => {
const idx = state[type].data.findIndex((e) => e.id === id && e.type === type);
const path = [type, 'data', idx, 'relationships', relationship];
return hasOwnProperties(state, path)
? state
: imm.wrap(state)
.set(path, null)
.value();
};