@monorise/react 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/core.action.d.ts +126 -14
- package/dist/actions/core.action.d.ts.map +1 -1
- package/dist/actions/core.action.js +392 -177
- package/dist/actions/core.action.js.map +1 -1
- package/dist/index.d.ts +245 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/lib/api.d.ts.map +1 -1
- package/dist/lib/api.js +11 -3
- package/dist/lib/api.js.map +1 -1
- package/dist/services/core.service.d.ts +2 -1
- package/dist/services/core.service.d.ts.map +1 -1
- package/dist/services/core.service.js.map +1 -1
- package/dist/types/api.type.d.ts +1 -0
- package/dist/types/api.type.d.ts.map +1 -1
- package/dist/types/api.type.js.map +1 -1
- package/package.json +2 -2
|
@@ -6,11 +6,17 @@ import { convertToMap, getEntityRequestKey, getMutualRequestKey, getMutualStateK
|
|
|
6
6
|
// Should we use store.getState() or store()?
|
|
7
7
|
// USE store.getState() WHEN using within an action such as getEntity, getProfile
|
|
8
8
|
// USE store() WHEN using within a react hook so that it gets the benefit of reactivity, such as useProfile, useEntity
|
|
9
|
+
const defaultOnError = (error) => {
|
|
10
|
+
if (!(error instanceof Error) && error.status >= 500) {
|
|
11
|
+
// we only want server error to be captured by error tracking service
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
9
15
|
const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
10
16
|
const { makeEntityService, makeMutualService } = coreService;
|
|
11
17
|
const { checkIsLoading, getError, useLoadStore, useErrorStore } = appActions;
|
|
12
18
|
const listEntities = async (entityType, params = {}, opts = {}) => {
|
|
13
|
-
var _a;
|
|
19
|
+
var _a, _b;
|
|
14
20
|
const store = monoriseStore.getState();
|
|
15
21
|
const entityState = (_a = store.entity[entityType]) !== null && _a !== void 0 ? _a : {};
|
|
16
22
|
const { isFirstFetched } = entityState;
|
|
@@ -19,54 +25,94 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
19
25
|
const requestKey = getEntityRequestKey('list', entityType);
|
|
20
26
|
const isLoading = checkIsLoading(requestKey);
|
|
21
27
|
const error = getError(requestKey);
|
|
22
|
-
|
|
28
|
+
const onError = (_b = opts.onError) !== null && _b !== void 0 ? _b : defaultOnError;
|
|
29
|
+
if (error) {
|
|
30
|
+
onError(error);
|
|
23
31
|
return;
|
|
24
32
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
dataMap
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
if ((isFirstFetched && !params.skRange) || isLoading) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const { data: result } = await entityService.listEntities(Object.assign(Object.assign({}, ((params === null || params === void 0 ? void 0 : params.all) ? {} : { limit: 20 })), { start: skRange === null || skRange === void 0 ? void 0 : skRange.start, end: skRange === null || skRange === void 0 ? void 0 : skRange.end }), opts);
|
|
38
|
+
const newEntityMap = convertToMap(result.data, 'entityId');
|
|
39
|
+
const mergedMap = new Map([
|
|
40
|
+
...newEntityMap,
|
|
41
|
+
...store.entity[entityType].dataMap,
|
|
42
|
+
]);
|
|
43
|
+
monoriseStore.setState(produce((state) => {
|
|
44
|
+
state.entity[entityType] = {
|
|
45
|
+
dataMap: params.skRange ? newEntityMap : mergedMap,
|
|
46
|
+
isFirstFetched: true,
|
|
47
|
+
lastKey: result.lastKey,
|
|
48
|
+
};
|
|
49
|
+
}), undefined, `mr/entity/list/${entityType}`);
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
53
|
+
return { error };
|
|
54
|
+
}
|
|
38
55
|
};
|
|
39
56
|
const listMoreEntities = async (entityType, opts = {}) => {
|
|
57
|
+
var _a;
|
|
58
|
+
const requestKey = getEntityRequestKey('list', entityType);
|
|
40
59
|
const store = monoriseStore.getState();
|
|
41
60
|
const entityState = store.entity[entityType];
|
|
42
61
|
const { dataMap, lastKey } = entityState;
|
|
43
62
|
const entityService = makeEntityService(entityType);
|
|
63
|
+
const error = getError(requestKey);
|
|
64
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
44
65
|
if (!lastKey) {
|
|
45
66
|
return;
|
|
46
67
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
68
|
+
if (error) {
|
|
69
|
+
onError(error);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const { data: result } = await entityService.listEntities({
|
|
74
|
+
limit: 20,
|
|
75
|
+
lastKey,
|
|
76
|
+
}, opts);
|
|
77
|
+
monoriseStore.setState(produce((state) => {
|
|
78
|
+
state.entity[entityType].isFirstFetched = true;
|
|
79
|
+
state.entity[entityType].lastKey = result.lastKey;
|
|
80
|
+
for (const i in result.data) {
|
|
81
|
+
state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
|
|
82
|
+
}
|
|
83
|
+
}), undefined, `mr/entity/list-more/${entityType}`);
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
87
|
+
return { error };
|
|
88
|
+
}
|
|
58
89
|
};
|
|
59
90
|
const searchEntities = async (entityType, query, opts = {}) => {
|
|
91
|
+
var _a;
|
|
92
|
+
const requestKey = getEntityRequestKey('search', entityType);
|
|
60
93
|
const entityService = makeEntityService(entityType);
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
94
|
+
const error = getError(requestKey);
|
|
95
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
96
|
+
if (error) {
|
|
97
|
+
onError(error);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const { data: result } = await entityService.searchEntities(query, opts);
|
|
102
|
+
monoriseStore.setState(produce((state) => {
|
|
103
|
+
state.entity[entityType].searchResults = result.data;
|
|
104
|
+
for (const i in result.data) {
|
|
105
|
+
state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
|
|
106
|
+
}
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
111
|
+
return { error };
|
|
112
|
+
}
|
|
68
113
|
};
|
|
69
114
|
const listEntitiesByTag = async (entityType, tagName, opts = {}) => {
|
|
115
|
+
var _a;
|
|
70
116
|
const tagKey = getTagStateKey(entityType, tagName, opts.params);
|
|
71
117
|
const state = monoriseStore.getState();
|
|
72
118
|
const tagState = state.tag[tagKey] || {};
|
|
@@ -76,26 +122,42 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
76
122
|
const requestKey = getTagRequestKey('list', entityType, tagName, opts.params);
|
|
77
123
|
const isLoading = checkIsLoading(requestKey);
|
|
78
124
|
const error = getError(requestKey);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
monoriseStore.setState(produce((state) => {
|
|
85
|
-
for (const entity of entities) {
|
|
86
|
-
state.entity[entityType].dataMap.set(entity.entityId, entity);
|
|
125
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
126
|
+
if (!forceFetch) {
|
|
127
|
+
if (error) {
|
|
128
|
+
onError(error);
|
|
129
|
+
return { error };
|
|
87
130
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
131
|
+
if (isFirstFetched || isLoading) {
|
|
132
|
+
return {
|
|
133
|
+
data: { entities: Array.from(dataMap.values()), lastKey: null },
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const { data } = await entityService.listEntitiesByTag(tagName, Object.assign(Object.assign({}, opts), { requestKey }));
|
|
139
|
+
const { entities, lastKey } = data;
|
|
140
|
+
monoriseStore.setState(produce((state) => {
|
|
141
|
+
for (const entity of entities) {
|
|
142
|
+
state.entity[entityType].dataMap.set(entity.entityId, entity);
|
|
143
|
+
}
|
|
144
|
+
}));
|
|
145
|
+
monoriseStore.setState(produce((state) => {
|
|
146
|
+
state.tag[tagKey] = {
|
|
147
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
148
|
+
isFirstFetched: true,
|
|
149
|
+
lastKey,
|
|
150
|
+
};
|
|
151
|
+
}));
|
|
152
|
+
return { data };
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
156
|
+
return { error };
|
|
157
|
+
}
|
|
97
158
|
};
|
|
98
159
|
const getEntity = async (entityType, id, opts = {}) => {
|
|
160
|
+
var _a;
|
|
99
161
|
const store = monoriseStore.getState();
|
|
100
162
|
const entityState = store.entity[entityType];
|
|
101
163
|
const { dataMap } = entityState;
|
|
@@ -105,16 +167,36 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
105
167
|
const isLoading = checkIsLoading(requestKey);
|
|
106
168
|
const error = getError(requestKey);
|
|
107
169
|
const { forceFetch } = opts;
|
|
108
|
-
|
|
109
|
-
|
|
170
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
171
|
+
if (!forceFetch) {
|
|
172
|
+
if (error) {
|
|
173
|
+
// If there's an existing error, handle it and return the error.
|
|
174
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
175
|
+
return { error };
|
|
176
|
+
}
|
|
177
|
+
if (entity) {
|
|
178
|
+
// If entity already exists and no force fetch, return it.
|
|
179
|
+
return { data: entity };
|
|
180
|
+
}
|
|
181
|
+
if (isLoading) {
|
|
182
|
+
// If already loading, return undefined data and error to indicate ongoing operation.
|
|
183
|
+
return {};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
({ data: entity } = await entityService.getEntity(id, opts));
|
|
188
|
+
monoriseStore.setState(produce((state) => {
|
|
189
|
+
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
190
|
+
}), undefined, `mr/entity/get/${entityType}/${id}`);
|
|
191
|
+
return { data: entity };
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
195
|
+
return { error };
|
|
110
196
|
}
|
|
111
|
-
({ data: entity } = await entityService.getEntity(id, opts));
|
|
112
|
-
monoriseStore.setState(produce((state) => {
|
|
113
|
-
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
114
|
-
}), undefined, `mr/entity/get/${entityType}/${id}`);
|
|
115
|
-
return entity;
|
|
116
197
|
};
|
|
117
198
|
const getEntityByUniqueField = async (entityType, fieldName, value, opts = {}) => {
|
|
199
|
+
var _a;
|
|
118
200
|
const store = monoriseStore.getState();
|
|
119
201
|
const entityState = store.entity[entityType];
|
|
120
202
|
const { dataMap } = entityState;
|
|
@@ -125,64 +207,120 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
125
207
|
const isLoading = checkIsLoading(requestKey);
|
|
126
208
|
const error = getError(requestKey);
|
|
127
209
|
const { forceFetch } = opts;
|
|
128
|
-
|
|
129
|
-
|
|
210
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
211
|
+
if (!forceFetch) {
|
|
212
|
+
if (error) {
|
|
213
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
214
|
+
return { error };
|
|
215
|
+
}
|
|
216
|
+
if (entity) {
|
|
217
|
+
return { data: entity };
|
|
218
|
+
}
|
|
219
|
+
if (isLoading) {
|
|
220
|
+
return {};
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
|
|
225
|
+
monoriseStore.setState(produce((state) => {
|
|
226
|
+
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
227
|
+
state.entity[entityType].dataMap.set(`${stateKey}`, entity);
|
|
228
|
+
}), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
|
|
229
|
+
return { data: entity };
|
|
230
|
+
}
|
|
231
|
+
catch (err) {
|
|
232
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
233
|
+
return { error };
|
|
130
234
|
}
|
|
131
|
-
({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
|
|
132
|
-
monoriseStore.setState(produce((state) => {
|
|
133
|
-
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
134
|
-
state.entity[entityType].dataMap.set(`${stateKey}`, entity);
|
|
135
|
-
}), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
|
|
136
|
-
return entity;
|
|
137
235
|
};
|
|
138
236
|
const createEntity = async (entityType, entity, opts = {}) => {
|
|
237
|
+
var _a;
|
|
139
238
|
const entityService = makeEntityService(entityType);
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
239
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
240
|
+
try {
|
|
241
|
+
const { data } = await entityService.createEntity(entity, opts);
|
|
242
|
+
monoriseStore.setState(produce((state) => {
|
|
243
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
244
|
+
}), undefined, `mr/entity/create/${entityType}`);
|
|
245
|
+
return { data };
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
249
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
250
|
+
return { error };
|
|
251
|
+
}
|
|
145
252
|
};
|
|
146
253
|
const upsertEntity = async (entityType, id, entity, opts = {}) => {
|
|
254
|
+
var _a;
|
|
147
255
|
const entityService = makeEntityService(entityType);
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
256
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
257
|
+
try {
|
|
258
|
+
const { data } = await entityService.upsertEntity(id, entity, opts);
|
|
259
|
+
monoriseStore.setState(produce((state) => {
|
|
260
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
261
|
+
}), undefined, `mr/entity/upsert/${entityType}/${id}`);
|
|
262
|
+
return { data };
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
266
|
+
onError(error);
|
|
267
|
+
return { error };
|
|
268
|
+
}
|
|
152
269
|
};
|
|
153
270
|
const editEntity = async (entityType, id, entity, opts = {}) => {
|
|
271
|
+
var _a;
|
|
154
272
|
const entityService = makeEntityService(entityType);
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
|
|
273
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
274
|
+
try {
|
|
275
|
+
const { data } = await entityService.editEntity(id, entity, opts);
|
|
276
|
+
monoriseStore.setState(produce((state) => {
|
|
277
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
278
|
+
// update mutual's entity data
|
|
279
|
+
for (const key of Object.keys(state.mutual)) {
|
|
280
|
+
const [_byEntity, _byId, _entityType] = key.split('/');
|
|
281
|
+
if (_entityType === entityType) {
|
|
282
|
+
const mutual = state.mutual[key].dataMap.get(id);
|
|
283
|
+
state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(id, Object.assign(Object.assign({}, mutual), { data: data.data }));
|
|
284
|
+
}
|
|
164
285
|
}
|
|
165
|
-
}
|
|
166
|
-
|
|
286
|
+
}), undefined, `mr/entity/edit/${entityType}/${id}`);
|
|
287
|
+
return { data };
|
|
288
|
+
}
|
|
289
|
+
catch (err) {
|
|
290
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
291
|
+
onError(error);
|
|
292
|
+
return { error };
|
|
293
|
+
}
|
|
167
294
|
};
|
|
168
295
|
const deleteEntity = async (entityType, id, opts = {}) => {
|
|
296
|
+
var _a;
|
|
169
297
|
const entityService = makeEntityService(entityType);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
298
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
299
|
+
try {
|
|
300
|
+
await entityService.deleteEntity(id, opts);
|
|
301
|
+
deleteLocalMutualsByEntity(entityType, id);
|
|
302
|
+
monoriseStore.setState(produce((state) => {
|
|
303
|
+
state.entity[entityType].dataMap.delete(id);
|
|
304
|
+
// delete mutual's entity data
|
|
305
|
+
for (const key of Object.keys(state.mutual)) {
|
|
306
|
+
const [_byEntity, _byId, _entityType] = key.split('/');
|
|
307
|
+
if (_entityType === entityType) {
|
|
308
|
+
state.mutual[key].dataMap.delete(id);
|
|
309
|
+
}
|
|
179
310
|
}
|
|
180
|
-
}
|
|
181
|
-
|
|
311
|
+
}), undefined, `mr/entity/delete/${entityType}/${id}`);
|
|
312
|
+
return { data: { entityId: id } }; // Indicate success with the deleted ID
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
316
|
+
onError(error);
|
|
317
|
+
return { error };
|
|
318
|
+
}
|
|
182
319
|
};
|
|
183
320
|
const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
|
|
184
|
-
var _a;
|
|
185
|
-
const
|
|
321
|
+
var _a, _b, _c;
|
|
322
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
323
|
+
const selfKey = (_b = opts.stateKey) !== null && _b !== void 0 ? _b : getMutualStateKey(byEntityType, id, entityType, undefined, chainEntityQuery);
|
|
186
324
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
187
325
|
const store = monoriseStore.getState();
|
|
188
326
|
const mutualState = store.mutual[selfKey] || {};
|
|
@@ -191,26 +329,44 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
191
329
|
const isLoading = checkIsLoading(requestKey);
|
|
192
330
|
const error = getError(requestKey);
|
|
193
331
|
const { forceFetch } = opts;
|
|
194
|
-
if (!forceFetch
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
332
|
+
if (!forceFetch) {
|
|
333
|
+
if (error) {
|
|
334
|
+
onError(error);
|
|
335
|
+
return { error };
|
|
336
|
+
}
|
|
337
|
+
if (isFirstFetched || isLoading) {
|
|
338
|
+
return {
|
|
339
|
+
data: {
|
|
340
|
+
entities: Array.from(((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.values()) || []),
|
|
341
|
+
lastKey: mutualState.lastKey,
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
}
|
|
202
345
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
346
|
+
try {
|
|
347
|
+
const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
|
|
348
|
+
const { entities, lastKey } = data;
|
|
349
|
+
const newEntityDataMap = new Map();
|
|
350
|
+
for (const i in entities) {
|
|
351
|
+
newEntityDataMap.set(entities[i].entityId, entities[i]);
|
|
207
352
|
}
|
|
208
|
-
state
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
353
|
+
monoriseStore.setState(produce((state) => {
|
|
354
|
+
var _a;
|
|
355
|
+
for (const [key, value] of newEntityDataMap) {
|
|
356
|
+
(_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap.set(key, value);
|
|
357
|
+
}
|
|
358
|
+
state.mutual[selfKey] = {
|
|
359
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
360
|
+
isFirstFetched: true,
|
|
361
|
+
lastKey,
|
|
362
|
+
};
|
|
363
|
+
}), undefined, `mr/mutual/list/${selfKey}`);
|
|
364
|
+
return { data };
|
|
365
|
+
}
|
|
366
|
+
catch (err) {
|
|
367
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
368
|
+
return { error };
|
|
369
|
+
}
|
|
214
370
|
};
|
|
215
371
|
// todo: list more mutuals by entity
|
|
216
372
|
// const listMoremutualsByEntity = async (
|
|
@@ -219,7 +375,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
219
375
|
// id: string,
|
|
220
376
|
// ) =>
|
|
221
377
|
const getMutual = async (byEntityType, entityType, byEntityId, entityId, opts = {}) => {
|
|
222
|
-
var _a, _b;
|
|
378
|
+
var _a, _b, _c, _d;
|
|
223
379
|
const selfKey = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
224
380
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
225
381
|
const store = monoriseStore.getState();
|
|
@@ -227,11 +383,20 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
227
383
|
const requestKey = getMutualRequestKey('get', byEntityType, entityType, byEntityId, entityId);
|
|
228
384
|
const isLoading = checkIsLoading(requestKey);
|
|
229
385
|
const error = getError(requestKey);
|
|
230
|
-
|
|
231
|
-
|
|
386
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
387
|
+
if (error) {
|
|
388
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
389
|
+
return { error };
|
|
390
|
+
}
|
|
391
|
+
if (!byEntityId || isLoading) {
|
|
392
|
+
return {
|
|
393
|
+
data: (_b = mutualState.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId),
|
|
394
|
+
};
|
|
232
395
|
}
|
|
233
|
-
if ((
|
|
234
|
-
return
|
|
396
|
+
if ((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.get(entityId)) {
|
|
397
|
+
return {
|
|
398
|
+
data: mutualState.dataMap.get(entityId),
|
|
399
|
+
};
|
|
235
400
|
}
|
|
236
401
|
let mutual;
|
|
237
402
|
let hasRequestFailed = false;
|
|
@@ -240,7 +405,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
240
405
|
}
|
|
241
406
|
catch (err) {
|
|
242
407
|
if (!opts.defaultMutualData) {
|
|
243
|
-
|
|
408
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
409
|
+
return { error };
|
|
244
410
|
}
|
|
245
411
|
hasRequestFailed = true;
|
|
246
412
|
mutual = {
|
|
@@ -255,7 +421,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
255
421
|
mutualData: opts.defaultMutualData,
|
|
256
422
|
};
|
|
257
423
|
}
|
|
258
|
-
const newMutualDataMap = new Map((
|
|
424
|
+
const newMutualDataMap = new Map((_d = store.mutual[selfKey]) === null || _d === void 0 ? void 0 : _d.dataMap);
|
|
259
425
|
newMutualDataMap.set(mutual.entityId, mutual);
|
|
260
426
|
if (!hasRequestFailed) {
|
|
261
427
|
const entityState = store.entity[entityType];
|
|
@@ -267,27 +433,38 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
267
433
|
state.entity[entityType].dataMap = newEntityDataMap;
|
|
268
434
|
}), undefined, `mr/mutual/get/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
269
435
|
}
|
|
436
|
+
return { data: mutual };
|
|
270
437
|
};
|
|
271
438
|
const createMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
|
|
439
|
+
var _a;
|
|
272
440
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
state.mutual[
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
441
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
442
|
+
try {
|
|
443
|
+
const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
|
|
444
|
+
monoriseStore.setState(produce((state) => {
|
|
445
|
+
var _a, _b;
|
|
446
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
447
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
448
|
+
if (!state.mutual[bySide]) {
|
|
449
|
+
state.mutual[bySide] = {
|
|
450
|
+
dataMap: new Map(),
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
|
|
454
|
+
if (!state.mutual[side]) {
|
|
455
|
+
state.mutual[side] = {
|
|
456
|
+
dataMap: new Map(),
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
|
|
460
|
+
}), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
461
|
+
return { data: mutual };
|
|
462
|
+
}
|
|
463
|
+
catch (err) {
|
|
464
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
465
|
+
onError(error);
|
|
466
|
+
return { error };
|
|
467
|
+
}
|
|
291
468
|
};
|
|
292
469
|
const createLocalMutual = async (byEntityType, entityType, byEntityId, entityId, mutualData, data) => {
|
|
293
470
|
const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
|
|
@@ -336,54 +513,78 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
336
513
|
}), undefined, `mr/mutual/local-update/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
337
514
|
};
|
|
338
515
|
const editMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
|
|
516
|
+
var _a;
|
|
339
517
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
state.mutual[
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
518
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
519
|
+
try {
|
|
520
|
+
const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
|
|
521
|
+
monoriseStore.setState(produce((state) => {
|
|
522
|
+
var _a, _b;
|
|
523
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
524
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
525
|
+
if (!state.mutual[bySide]) {
|
|
526
|
+
state.mutual[bySide] = {
|
|
527
|
+
dataMap: new Map(),
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
|
|
531
|
+
if (!state.mutual[side]) {
|
|
532
|
+
state.mutual[side] = {
|
|
533
|
+
dataMap: new Map(),
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
|
|
537
|
+
}), undefined, `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
538
|
+
return { data: mutual };
|
|
539
|
+
}
|
|
540
|
+
catch (err) {
|
|
541
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
542
|
+
onError(error);
|
|
543
|
+
return { error };
|
|
544
|
+
}
|
|
358
545
|
};
|
|
359
546
|
const deleteMutual = async (byEntityType, entityType, byEntityId, entityId = '', opts = {}) => {
|
|
547
|
+
var _a;
|
|
360
548
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
state.mutual[
|
|
368
|
-
|
|
369
|
-
|
|
549
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
550
|
+
try {
|
|
551
|
+
const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
|
|
552
|
+
monoriseStore.setState(produce((state) => {
|
|
553
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
554
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
555
|
+
state.mutual[bySide].dataMap.delete(data.entityId);
|
|
556
|
+
if (state.mutual[side]) {
|
|
557
|
+
state.mutual[side].dataMap.delete(data.byEntityId);
|
|
558
|
+
}
|
|
559
|
+
}), undefined, `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
560
|
+
return { data };
|
|
561
|
+
}
|
|
562
|
+
catch (err) {
|
|
563
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
564
|
+
onError(error);
|
|
565
|
+
return { error };
|
|
566
|
+
}
|
|
370
567
|
};
|
|
371
568
|
const deleteLocalMutual = (byEntityType, entityType, byEntityId, entityId) => {
|
|
372
569
|
monoriseStore.setState(produce((state) => {
|
|
373
570
|
var _a, _b;
|
|
374
571
|
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
375
572
|
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
376
|
-
const bySideDataMap = new Map((_a = state[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
|
|
377
|
-
const sideDataMap = new Map((_b = state[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
|
|
573
|
+
const bySideDataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
|
|
574
|
+
const sideDataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
|
|
378
575
|
bySideDataMap.delete(entityId);
|
|
379
576
|
sideDataMap.delete(byEntityId);
|
|
380
|
-
state.mutual[bySide]
|
|
381
|
-
|
|
577
|
+
if (state.mutual[bySide]) {
|
|
578
|
+
state.mutual[bySide].dataMap.delete(entityId);
|
|
579
|
+
}
|
|
580
|
+
if (state.mutual[side]) {
|
|
581
|
+
state.mutual[side].dataMap.delete(byEntityId);
|
|
582
|
+
}
|
|
382
583
|
}), undefined, `mr/mutual/local-delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
383
584
|
};
|
|
384
585
|
const deleteLocalMutualsByEntity = (entityType, id) => {
|
|
385
586
|
const store = monoriseStore.getState();
|
|
386
|
-
const entityConfig = store.config;
|
|
587
|
+
const entityConfig = store.config; // Assuming store.config holds mutual related configurations
|
|
387
588
|
let mutuals = [];
|
|
388
589
|
for (const i of Object.keys(entityConfig)) {
|
|
389
590
|
const mutualState = store.mutual[`${entityType}/${id}/${i}`];
|
|
@@ -393,14 +594,20 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
393
594
|
];
|
|
394
595
|
}
|
|
395
596
|
const updatedState = mutuals.reduce((acc, mutual) => {
|
|
597
|
+
var _a;
|
|
396
598
|
const side = getMutualStateKey(mutual.entityType, mutual.entityId, mutual.byEntityType);
|
|
397
|
-
const dataMap = new Map(store.mutual[side].dataMap);
|
|
599
|
+
const dataMap = new Map((_a = store.mutual[side]) === null || _a === void 0 ? void 0 : _a.dataMap); // Use optional chaining
|
|
398
600
|
dataMap.delete(id);
|
|
399
601
|
acc[side] = Object.assign(Object.assign({}, store.mutual[side]), { dataMap });
|
|
400
602
|
return acc;
|
|
401
603
|
}, {});
|
|
402
604
|
monoriseStore.setState(produce((state) => {
|
|
403
|
-
|
|
605
|
+
// This is a full replacement, might not be desired for partial updates.
|
|
606
|
+
// If state.mutual can contain other keys, this might clear them.
|
|
607
|
+
// Consider a more granular update if needed.
|
|
608
|
+
for (const key in updatedState) {
|
|
609
|
+
state.mutual[key] = updatedState[key];
|
|
610
|
+
}
|
|
404
611
|
}), undefined, `mr/mutual/local-delete-by-entity/${entityType}/${id}`);
|
|
405
612
|
};
|
|
406
613
|
const useEntity = (entityType, id, opts = {}) => {
|
|
@@ -422,7 +629,11 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
422
629
|
isFirstFetched,
|
|
423
630
|
refetch: async () => {
|
|
424
631
|
if (id) {
|
|
425
|
-
|
|
632
|
+
const { data, error: fetchError } = await getEntity(entityType, id, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
633
|
+
if (fetchError) {
|
|
634
|
+
// Handle refetch error if necessary, e.g., throw or set local state
|
|
635
|
+
}
|
|
636
|
+
return data;
|
|
426
637
|
}
|
|
427
638
|
},
|
|
428
639
|
};
|
|
@@ -447,7 +658,11 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
447
658
|
isFirstFetched,
|
|
448
659
|
refetch: async () => {
|
|
449
660
|
if (value) {
|
|
450
|
-
|
|
661
|
+
const { data, error: fetchError } = await getEntityByUniqueField(entityType, fieldName, value, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
662
|
+
if (fetchError) {
|
|
663
|
+
// Handle refetch error if necessary
|
|
664
|
+
}
|
|
665
|
+
return data;
|
|
451
666
|
}
|
|
452
667
|
},
|
|
453
668
|
};
|