-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharedReducer.js
137 lines (110 loc) · 3.76 KB
/
sharedReducer.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
import { isFetchAction } from '../defineFetch';
import { isSuccessAction } from '../createDataService';
import { isClearAction } from '../clearFetch';
const initialState = {
data: {},
parents: {},
merges: {},
};
// for each fetch factory, save their normalized merge object into an easy-to-look-up place
// on success, find the merge functions relevant to the current merge
export default function sharedReducer(state = initialState, action) {
if (isFetchAction(action)) {
const { meta } = action;
const { displayName, fetchFactoryId, key, share } = meta;
// only run this reducer if this action is `share`d
if (!share) return state;
const { namespace, mergeObj } = share;
const mergeState = { ...state?.merges };
// (eslint bug)
// eslint-disable-next-line no-unused-vars
for (const [targetNamespace, mergeFn] of Object.entries(mergeObj)) {
const merges = { ...mergeState?.[targetNamespace] };
merges[namespace] = mergeFn;
mergeState[targetNamespace] = merges;
}
const parentsState = { ...state?.parents };
// (eslint bug)
// eslint-disable-next-line no-unused-vars
const parents = { ...parentsState?.[namespace] };
parents[`${displayName} | ${key} | ${fetchFactoryId}`] = {
fetchFactoryId,
key,
displayName,
};
parentsState[namespace] = parents;
return {
...state,
merges: mergeState,
parents: parentsState,
};
}
if (isSuccessAction(action)) {
const { meta, payload } = action;
const { key, share } = meta;
// only run this reducer if this action is `share`d
if (!share) return state;
const { namespace } = share;
const merges = state?.merges?.[namespace] || {};
const nextData = { ...state?.data };
// (eslint bug)
// eslint-disable-next-line no-unused-vars
for (const [targetNamespace, mergeFn] of Object.entries(merges)) {
// if the target namespace is the same from the action's namespace
// we should only apply the merge function over the current key
if (targetNamespace === namespace) {
nextData[targetNamespace] = {
...state.data?.[targetNamespace],
[key]: mergeFn(state.data?.[targetNamespace]?.[key], payload),
};
continue;
}
const mergedData = Object.entries(state?.data?.[targetNamespace] || {}).reduce(
(acc, [key, value]) => {
acc[key] = mergeFn(value, payload);
return acc;
},
{},
);
// otherwise we should apply the merge function over all the keys
nextData[targetNamespace] = mergedData;
}
return {
...state,
data: nextData,
};
}
if (isClearAction(action)) {
const { meta } = action;
const { displayName, fetchFactoryId, key, share } = meta;
// only run this reducer if this action is `share`d
if (!share) return state;
const { namespace, mergeObj } = share;
const mergeState = { ...state?.merges };
// (eslint bug)
// eslint-disable-next-line no-unused-vars
for (const targetNamespace of Object.keys(mergeObj)) {
const merges = { ...mergeState?.[targetNamespace] };
delete merges[namespace];
mergeState[targetNamespace] = merges;
}
const parentsState = { ...state?.parents };
const parentSet = { ...parentsState?.[namespace] };
const parentKey = `${displayName} | ${key} | ${fetchFactoryId}`;
delete parentSet[parentKey];
if (Object.keys(parentSet || {}).length <= 0) {
delete parentsState[namespace];
} else {
parentsState[namespace] = parentSet;
}
const dataState = { ...state?.data };
delete dataState[namespace];
return {
...state,
data: dataState,
merges: mergeState,
parents: parentsState,
};
}
return state;
}