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 | 1x
1x
1x
3x
3x
3x
3x
3x
1x
1x
1x
13x
13x
13x
20x
10x
10x
13x
3x
3x
3x
4x
1x
3x
16x
16x
16x
16x
16x
| import shortid from 'shortid';
import { compose } from 'redux';
import persistState from 'redux-localstorage';
export function addToObject(state, arrKey, obj) {
const newObject = Object.assign({}, state[arrKey]);
const copiedObject = Object.assign({}, obj);
Iif (!copiedObject.id) {
copiedObject.id = shortid.generate();
}
newObject[copiedObject.id] = copiedObject;
return Object.assign({}, state, { [arrKey]: newObject });
}
export function alterInObject(state, arrKey, obj, alterations) {
const newObject = Object.assign({}, state[arrKey]);
newObject[obj.id] = Object.assign({}, newObject[obj.id], alterations);
return Object.assign({}, state, { [arrKey]: newObject });
}
export function alterInArr(state, arrKey, obj, alterations) {
// Finds an item in an array in the state and replaces it with a
// new object with an altered property
const idKey = 'id';
const newArr = [];
state[arrKey].forEach((arrItem) => {
if (obj[idKey] === arrItem[idKey]) {
newArr.push(Object.assign({}, arrItem, alterations));
} else {
newArr.push(arrItem);
}
});
return Object.assign({}, state, { [arrKey]: newArr });
}
export function removeFromArr(state, arrKey, obj, idKey = 'id') {
const newArr = [];
state[arrKey].forEach((arrItem) => {
if (!(obj[idKey] === arrItem[idKey])) {
newArr.push(arrItem);
}
});
return Object.assign({}, state, { [arrKey]: newArr });
}
export function getFromArr(arr, id) {
let obj;
arr.forEach((o) => {
if (o.id === id) {
obj = o;
}
});
return obj;
}
export function addToArr(state, arrKey, obj) {
const newObj = Object.assign({}, obj);
Iif (!newObj.id) {
newObj.id = shortid.generate();
}
const newState = {};
newState[arrKey] = [...state[arrKey], newObj];
return Object.assign({}, state, newState);
}
export function enhancer() {
let enhancerWithPersistState = compose(persistState());
if (process.env.NODE_ENV === 'dev') {
/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
/* eslint-enable */
enhancerWithPersistState = composeEnhancers(persistState());
}
return enhancerWithPersistState;
}
export function areArraysShallowEqual(arr1, arr2) {
// returns whether 2 arrays are shallow equal
// used in shouldComponentUpdate when denormalizing arrays
// where the array object is different every time, but the content might
// be the same
if (!arr1 || !arr2) {
return false;
}
if (arr1.length !== arr2.length) {
return false;
}
const length = arr1.length;
for (let i = 0; i < length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}
export function areObjectsEqual(obj1, obj2) {
if (!obj1 || !obj2) {
return false;
}
if (! Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (const id in obj1) {
if (!obj2.hasOwnProperty(id)) {
return false;
}
if (obj1[id] !== obj2[id]) {
return false;
}
}
return true;
}
|