@monorise/react 1.0.11 → 3.0.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/auth.action.d.ts +4 -1
- package/dist/actions/auth.action.d.ts.map +1 -1
- package/dist/actions/auth.action.js +7 -6
- package/dist/actions/auth.action.js.map +1 -1
- package/dist/actions/core.action.d.ts +163 -12
- package/dist/actions/core.action.d.ts.map +1 -1
- package/dist/actions/core.action.js +592 -168
- package/dist/actions/core.action.js.map +1 -1
- package/dist/index.d.ts +456 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/api.d.ts +15 -5
- package/dist/lib/api.d.ts.map +1 -1
- package/dist/lib/api.js +12 -6
- package/dist/lib/api.js.map +1 -1
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +24 -0
- package/dist/lib/utils.js.map +1 -1
- package/dist/services/core.service.d.ts +9 -1
- package/dist/services/core.service.d.ts.map +1 -1
- package/dist/services/core.service.js +32 -20
- package/dist/services/core.service.js.map +1 -1
- package/dist/services/filestore.service.d.ts +4 -1
- package/dist/services/filestore.service.d.ts.map +1 -1
- package/dist/services/filestore.service.js +49 -4
- package/dist/services/filestore.service.js.map +1 -1
- package/dist/store/monorise.store.d.ts +6 -0
- package/dist/store/monorise.store.d.ts.map +1 -1
- package/dist/store/monorise.store.js +1 -0
- package/dist/store/monorise.store.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/api.type.d.ts +2 -0
- package/dist/types/api.type.d.ts.map +1 -1
- package/dist/types/api.type.js.map +1 -1
- package/package.json +20 -10
- package/dist/index.esm.js +0 -1601
- package/dist/index.jsx +0 -48
- package/dist/index.jsx.map +0 -1
- package/dist/lib/api.jsx +0 -70
- package/dist/lib/api.jsx.map +0 -1
- package/dist/lib/config.d.ts +0 -5
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js +0 -24
- package/dist/lib/config.js.map +0 -1
- package/index.ts +0 -194
- package/tsconfig.json +0 -118
|
@@ -1,170 +1,390 @@
|
|
|
1
1
|
import { produce } from 'immer';
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
3
|
import { byEntityId, constructLocal, constructMutual, flipMutual, } from '../lib/entity';
|
|
4
|
-
import { convertToMap } from '../lib/utils';
|
|
4
|
+
import { convertToMap, getEntityRequestKey, getMutualRequestKey, getMutualStateKey, getTagRequestKey, getTagStateKey, getUniqueFieldRequestKey, getUniqueFieldStateKey, } from '../lib/utils';
|
|
5
5
|
// ===== Important tips ======
|
|
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 = {}) => {
|
|
19
|
+
var _a, _b;
|
|
13
20
|
const store = monoriseStore.getState();
|
|
14
|
-
const entityState = store.entity[entityType];
|
|
21
|
+
const entityState = (_a = store.entity[entityType]) !== null && _a !== void 0 ? _a : {};
|
|
15
22
|
const { isFirstFetched } = entityState;
|
|
16
23
|
const entityService = makeEntityService(entityType);
|
|
17
24
|
const { skRange } = params;
|
|
18
|
-
const requestKey =
|
|
25
|
+
const requestKey = getEntityRequestKey('list', entityType);
|
|
19
26
|
const isLoading = checkIsLoading(requestKey);
|
|
20
27
|
const error = getError(requestKey);
|
|
21
|
-
|
|
28
|
+
const onError = (_b = opts.onError) !== null && _b !== void 0 ? _b : defaultOnError;
|
|
29
|
+
if (error) {
|
|
30
|
+
onError(error);
|
|
22
31
|
return;
|
|
23
32
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
if (!opts.forceFetch &&
|
|
34
|
+
((isFirstFetched && !params.skRange) || isLoading)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
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);
|
|
39
|
+
const newEntityMap = convertToMap(result.data, 'entityId');
|
|
40
|
+
const mergedMap = new Map([
|
|
41
|
+
...newEntityMap,
|
|
42
|
+
...store.entity[entityType].dataMap,
|
|
43
|
+
]);
|
|
44
|
+
monoriseStore.setState(produce((state) => {
|
|
45
|
+
state.entity[entityType] = {
|
|
46
|
+
dataMap: params.skRange ? newEntityMap : mergedMap,
|
|
47
|
+
isFirstFetched: true,
|
|
48
|
+
lastKey: result.lastKey,
|
|
49
|
+
};
|
|
50
|
+
}), undefined, `mr/entity/list/${entityType}`);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
54
|
+
return { error };
|
|
55
|
+
}
|
|
37
56
|
};
|
|
38
57
|
const listMoreEntities = async (entityType, opts = {}) => {
|
|
58
|
+
var _a;
|
|
59
|
+
const requestKey = getEntityRequestKey('list', entityType);
|
|
39
60
|
const store = monoriseStore.getState();
|
|
40
61
|
const entityState = store.entity[entityType];
|
|
41
62
|
const { dataMap, lastKey } = entityState;
|
|
42
63
|
const entityService = makeEntityService(entityType);
|
|
64
|
+
const error = getError(requestKey);
|
|
65
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
43
66
|
if (!lastKey) {
|
|
44
67
|
return;
|
|
45
68
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
69
|
+
if (error) {
|
|
70
|
+
onError(error);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const { data: result } = await entityService.listEntities({
|
|
75
|
+
limit: 20,
|
|
76
|
+
lastKey,
|
|
77
|
+
}, opts);
|
|
78
|
+
monoriseStore.setState(produce((state) => {
|
|
79
|
+
state.entity[entityType].isFirstFetched = true;
|
|
80
|
+
state.entity[entityType].lastKey = result.lastKey;
|
|
81
|
+
for (const i in result.data) {
|
|
82
|
+
state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
|
|
83
|
+
}
|
|
84
|
+
}), undefined, `mr/entity/list-more/${entityType}`);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
88
|
+
return { error };
|
|
89
|
+
}
|
|
57
90
|
};
|
|
58
91
|
const searchEntities = async (entityType, query, opts = {}) => {
|
|
92
|
+
var _a;
|
|
93
|
+
const requestKey = getEntityRequestKey('search', entityType);
|
|
59
94
|
const entityService = makeEntityService(entityType);
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
95
|
+
const error = getError(requestKey);
|
|
96
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
97
|
+
if (error) {
|
|
98
|
+
onError(error);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
const { data: result } = await entityService.searchEntities(query, opts);
|
|
103
|
+
monoriseStore.setState(produce((state) => {
|
|
104
|
+
state.entity[entityType].searchResults = result.data;
|
|
105
|
+
for (const i in result.data) {
|
|
106
|
+
state.entity[entityType].dataMap.set(result.data[i].entityId, result.data[i]);
|
|
107
|
+
}
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
112
|
+
return { error };
|
|
113
|
+
}
|
|
67
114
|
};
|
|
68
115
|
const listEntitiesByTag = async (entityType, tagName, opts = {}) => {
|
|
69
116
|
var _a;
|
|
70
|
-
const tagKey =
|
|
71
|
-
const
|
|
72
|
-
const tagState =
|
|
117
|
+
const tagKey = getTagStateKey(entityType, tagName, opts.params);
|
|
118
|
+
const state = monoriseStore.getState();
|
|
119
|
+
const tagState = state.tag[tagKey] || {};
|
|
73
120
|
const { isFirstFetched, dataMap } = tagState;
|
|
74
121
|
const entityService = makeEntityService(entityType);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
122
|
+
const { forceFetch } = opts;
|
|
123
|
+
const requestKey = getTagRequestKey('list', entityType, tagName, opts.params);
|
|
124
|
+
const isLoading = checkIsLoading(requestKey);
|
|
125
|
+
const error = getError(requestKey);
|
|
126
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
127
|
+
if (!forceFetch) {
|
|
128
|
+
if (error) {
|
|
129
|
+
onError(error);
|
|
130
|
+
return { error };
|
|
83
131
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
132
|
+
if (isFirstFetched || isLoading) {
|
|
133
|
+
return {
|
|
134
|
+
data: {
|
|
135
|
+
entities: Array.from((dataMap === null || dataMap === void 0 ? void 0 : dataMap.values()) || []),
|
|
136
|
+
lastKey: null,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
const { data } = await entityService.listEntitiesByTag(tagName, Object.assign(Object.assign({}, opts), { requestKey }));
|
|
143
|
+
const { entities, lastKey } = data;
|
|
144
|
+
monoriseStore.setState(produce((state) => {
|
|
145
|
+
for (const entity of entities) {
|
|
146
|
+
state.entity[entityType].dataMap.set(entity.entityId, entity);
|
|
147
|
+
}
|
|
148
|
+
}));
|
|
149
|
+
monoriseStore.setState(produce((state) => {
|
|
150
|
+
state.tag[tagKey] = {
|
|
151
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
152
|
+
isFirstFetched: true,
|
|
153
|
+
lastKey,
|
|
154
|
+
};
|
|
155
|
+
}));
|
|
156
|
+
return { data };
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
160
|
+
return { error };
|
|
161
|
+
}
|
|
92
162
|
};
|
|
93
163
|
const getEntity = async (entityType, id, opts = {}) => {
|
|
164
|
+
var _a;
|
|
94
165
|
const store = monoriseStore.getState();
|
|
95
166
|
const entityState = store.entity[entityType];
|
|
96
167
|
const { dataMap } = entityState;
|
|
97
168
|
const entityService = makeEntityService(entityType);
|
|
98
169
|
let entity = dataMap.get(id);
|
|
99
|
-
const requestKey =
|
|
170
|
+
const requestKey = getEntityRequestKey('get', entityType, id);
|
|
100
171
|
const isLoading = checkIsLoading(requestKey);
|
|
101
172
|
const error = getError(requestKey);
|
|
102
|
-
|
|
103
|
-
|
|
173
|
+
const { forceFetch } = opts;
|
|
174
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
175
|
+
if (!forceFetch) {
|
|
176
|
+
if (error) {
|
|
177
|
+
// If there's an existing error, handle it and return the error.
|
|
178
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
179
|
+
return { error };
|
|
180
|
+
}
|
|
181
|
+
if (entity) {
|
|
182
|
+
// If entity already exists and no force fetch, return it.
|
|
183
|
+
return { data: entity };
|
|
184
|
+
}
|
|
185
|
+
if (isLoading) {
|
|
186
|
+
// If already loading, return undefined data and error to indicate ongoing operation.
|
|
187
|
+
return {};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
({ data: entity } = await entityService.getEntity(id, opts));
|
|
192
|
+
monoriseStore.setState(produce((state) => {
|
|
193
|
+
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
194
|
+
}), undefined, `mr/entity/get/${entityType}/${id}`);
|
|
195
|
+
return { data: entity };
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
199
|
+
return { error };
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
const getEntityByUniqueField = async (entityType, fieldName, value, opts = {}) => {
|
|
203
|
+
var _a;
|
|
204
|
+
const store = monoriseStore.getState();
|
|
205
|
+
const entityState = store.entity[entityType];
|
|
206
|
+
const { dataMap } = entityState;
|
|
207
|
+
const entityService = makeEntityService(entityType);
|
|
208
|
+
const stateKey = getUniqueFieldStateKey(fieldName, value);
|
|
209
|
+
let entity = dataMap.get(stateKey);
|
|
210
|
+
const requestKey = getUniqueFieldRequestKey(entityType, fieldName, value);
|
|
211
|
+
const isLoading = checkIsLoading(requestKey);
|
|
212
|
+
const error = getError(requestKey);
|
|
213
|
+
const { forceFetch } = opts;
|
|
214
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
215
|
+
if (!forceFetch) {
|
|
216
|
+
if (error) {
|
|
217
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
218
|
+
return { error };
|
|
219
|
+
}
|
|
220
|
+
if (entity) {
|
|
221
|
+
return { data: entity };
|
|
222
|
+
}
|
|
223
|
+
if (isLoading) {
|
|
224
|
+
return {};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
({ data: entity } = await entityService.getEntityByUniqueField(fieldName, value, opts));
|
|
229
|
+
monoriseStore.setState(produce((state) => {
|
|
230
|
+
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
231
|
+
state.entity[entityType].dataMap.set(`${stateKey}`, entity);
|
|
232
|
+
}), undefined, `mr/entity/unique/${entityType}/${stateKey}`);
|
|
233
|
+
return { data: entity };
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
237
|
+
return { error };
|
|
104
238
|
}
|
|
105
|
-
({ data: entity } = await entityService.getEntity(id, opts));
|
|
106
|
-
monoriseStore.setState(produce((state) => {
|
|
107
|
-
state.entity[entityType].dataMap.set(entity === null || entity === void 0 ? void 0 : entity.entityId, entity);
|
|
108
|
-
}), undefined, `mr/entity/get/${entityType}/${id}`);
|
|
109
239
|
};
|
|
110
240
|
const createEntity = async (entityType, entity, opts = {}) => {
|
|
241
|
+
var _a;
|
|
111
242
|
const entityService = makeEntityService(entityType);
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
243
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
244
|
+
try {
|
|
245
|
+
const { data } = await entityService.createEntity(entity, opts);
|
|
246
|
+
monoriseStore.setState(produce((state) => {
|
|
247
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
248
|
+
}), undefined, `mr/entity/create/${entityType}`);
|
|
249
|
+
return { data };
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
253
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
254
|
+
return { error };
|
|
255
|
+
}
|
|
117
256
|
};
|
|
118
257
|
const upsertEntity = async (entityType, id, entity, opts = {}) => {
|
|
258
|
+
var _a;
|
|
119
259
|
const entityService = makeEntityService(entityType);
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
260
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
261
|
+
try {
|
|
262
|
+
const { data } = await entityService.upsertEntity(id, entity, opts);
|
|
263
|
+
monoriseStore.setState(produce((state) => {
|
|
264
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
265
|
+
}), undefined, `mr/entity/upsert/${entityType}/${id}`);
|
|
266
|
+
return { data };
|
|
267
|
+
}
|
|
268
|
+
catch (err) {
|
|
269
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
270
|
+
onError(error);
|
|
271
|
+
return { error };
|
|
272
|
+
}
|
|
124
273
|
};
|
|
125
274
|
const editEntity = async (entityType, id, entity, opts = {}) => {
|
|
275
|
+
var _a;
|
|
126
276
|
const entityService = makeEntityService(entityType);
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
277
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
278
|
+
try {
|
|
279
|
+
const { data } = await entityService.editEntity(id, entity, opts);
|
|
280
|
+
monoriseStore.setState(produce((state) => {
|
|
281
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
282
|
+
// update mutual's entity data
|
|
283
|
+
for (const key of Object.keys(state.mutual)) {
|
|
284
|
+
const [_byEntity, _byId, _entityType] = key.split('/');
|
|
285
|
+
if (_entityType === entityType) {
|
|
286
|
+
const mutual = state.mutual[key].dataMap.get(id);
|
|
287
|
+
state.mutual[key].dataMap = new Map(state.mutual[key].dataMap).set(id, Object.assign(Object.assign({}, mutual), { data: data.data }));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}), undefined, `mr/entity/edit/${entityType}/${id}`);
|
|
291
|
+
return { data };
|
|
292
|
+
}
|
|
293
|
+
catch (err) {
|
|
294
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
295
|
+
onError(error);
|
|
296
|
+
return { error };
|
|
297
|
+
}
|
|
131
298
|
};
|
|
132
299
|
const deleteEntity = async (entityType, id, opts = {}) => {
|
|
300
|
+
var _a;
|
|
133
301
|
const entityService = makeEntityService(entityType);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
302
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
303
|
+
try {
|
|
304
|
+
await entityService.deleteEntity(id, opts);
|
|
305
|
+
deleteLocalMutualsByEntity(entityType, id);
|
|
306
|
+
monoriseStore.setState(produce((state) => {
|
|
307
|
+
state.entity[entityType].dataMap.delete(id);
|
|
308
|
+
// delete mutual's entity data
|
|
309
|
+
for (const key of Object.keys(state.mutual)) {
|
|
310
|
+
const [_byEntity, _byId, _entityType] = key.split('/');
|
|
311
|
+
if (_entityType === entityType) {
|
|
312
|
+
state.mutual[key].dataMap.delete(id);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}), undefined, `mr/entity/delete/${entityType}/${id}`);
|
|
316
|
+
return { data: { entityId: id } }; // Indicate success with the deleted ID
|
|
317
|
+
}
|
|
318
|
+
catch (err) {
|
|
319
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
320
|
+
onError(error);
|
|
321
|
+
return { error };
|
|
322
|
+
}
|
|
139
323
|
};
|
|
140
324
|
const listEntitiesByEntity = async (byEntityType, entityType, id, opts = {}, chainEntityQuery) => {
|
|
141
|
-
|
|
325
|
+
var _a, _b, _c;
|
|
326
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
327
|
+
const selfKey = (_b = opts.stateKey) !== null && _b !== void 0 ? _b : getMutualStateKey(byEntityType, id, entityType, undefined, chainEntityQuery);
|
|
142
328
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
143
329
|
const store = monoriseStore.getState();
|
|
144
330
|
const mutualState = store.mutual[selfKey] || {};
|
|
145
331
|
const { isFirstFetched } = mutualState;
|
|
146
|
-
const requestKey =
|
|
332
|
+
const requestKey = getMutualRequestKey('list', byEntityType, entityType, id, undefined, chainEntityQuery);
|
|
147
333
|
const isLoading = checkIsLoading(requestKey);
|
|
148
334
|
const error = getError(requestKey);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
335
|
+
const { forceFetch } = opts;
|
|
336
|
+
if (!forceFetch) {
|
|
337
|
+
if (error) {
|
|
338
|
+
onError(error);
|
|
339
|
+
return { error };
|
|
340
|
+
}
|
|
341
|
+
if (isFirstFetched || isLoading) {
|
|
342
|
+
return {
|
|
343
|
+
data: {
|
|
344
|
+
entities: Array.from(((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.values()) || []),
|
|
345
|
+
lastKey: mutualState.lastKey,
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
}
|
|
157
349
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
350
|
+
try {
|
|
351
|
+
const { data } = await mutualService.listEntitiesByEntity(id, opts, chainEntityQuery);
|
|
352
|
+
const { entities, lastKey } = data;
|
|
353
|
+
const newEntityDataMap = new Map();
|
|
354
|
+
for (const i in entities) {
|
|
355
|
+
newEntityDataMap.set(entities[i].entityId, entities[i]);
|
|
161
356
|
}
|
|
162
|
-
state
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
357
|
+
monoriseStore.setState(produce((state) => {
|
|
358
|
+
var _a;
|
|
359
|
+
for (const [key, value] of newEntityDataMap) {
|
|
360
|
+
(_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap.set(key, value);
|
|
361
|
+
}
|
|
362
|
+
// prepare or extend the mutual entry
|
|
363
|
+
const existing = state.mutual[selfKey];
|
|
364
|
+
if (existing) {
|
|
365
|
+
// extend the existing map
|
|
366
|
+
const incoming = convertToMap(entities, 'entityId');
|
|
367
|
+
for (const [k, v] of incoming) {
|
|
368
|
+
existing.dataMap.set(k, v);
|
|
369
|
+
}
|
|
370
|
+
existing.isFirstFetched = true;
|
|
371
|
+
existing.lastKey = lastKey;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
// first time: create it
|
|
375
|
+
state.mutual[selfKey] = {
|
|
376
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
377
|
+
isFirstFetched: true,
|
|
378
|
+
lastKey,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}), undefined, `mr/mutual/list/${selfKey}`);
|
|
382
|
+
return { data };
|
|
383
|
+
}
|
|
384
|
+
catch (err) {
|
|
385
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
386
|
+
return { error };
|
|
387
|
+
}
|
|
168
388
|
};
|
|
169
389
|
// todo: list more mutuals by entity
|
|
170
390
|
// const listMoremutualsByEntity = async (
|
|
@@ -173,19 +393,28 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
173
393
|
// id: string,
|
|
174
394
|
// ) =>
|
|
175
395
|
const getMutual = async (byEntityType, entityType, byEntityId, entityId, opts = {}) => {
|
|
176
|
-
var _a, _b;
|
|
177
|
-
const selfKey =
|
|
396
|
+
var _a, _b, _c, _d;
|
|
397
|
+
const selfKey = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
178
398
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
179
399
|
const store = monoriseStore.getState();
|
|
180
400
|
const mutualState = store.mutual[selfKey] || {};
|
|
181
|
-
const requestKey =
|
|
401
|
+
const requestKey = getMutualRequestKey('get', byEntityType, entityType, byEntityId, entityId);
|
|
182
402
|
const isLoading = checkIsLoading(requestKey);
|
|
183
403
|
const error = getError(requestKey);
|
|
184
|
-
|
|
185
|
-
|
|
404
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
405
|
+
if (error) {
|
|
406
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
407
|
+
return { error };
|
|
408
|
+
}
|
|
409
|
+
if (!byEntityId || isLoading) {
|
|
410
|
+
return {
|
|
411
|
+
data: (_b = mutualState.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId),
|
|
412
|
+
};
|
|
186
413
|
}
|
|
187
|
-
if ((
|
|
188
|
-
return
|
|
414
|
+
if ((_c = mutualState.dataMap) === null || _c === void 0 ? void 0 : _c.get(entityId)) {
|
|
415
|
+
return {
|
|
416
|
+
data: mutualState.dataMap.get(entityId),
|
|
417
|
+
};
|
|
189
418
|
}
|
|
190
419
|
let mutual;
|
|
191
420
|
let hasRequestFailed = false;
|
|
@@ -194,7 +423,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
194
423
|
}
|
|
195
424
|
catch (err) {
|
|
196
425
|
if (!opts.defaultMutualData) {
|
|
197
|
-
|
|
426
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
427
|
+
return { error };
|
|
198
428
|
}
|
|
199
429
|
hasRequestFailed = true;
|
|
200
430
|
mutual = {
|
|
@@ -209,7 +439,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
209
439
|
mutualData: opts.defaultMutualData,
|
|
210
440
|
};
|
|
211
441
|
}
|
|
212
|
-
const newMutualDataMap = new Map((
|
|
442
|
+
const newMutualDataMap = new Map((_d = store.mutual[selfKey]) === null || _d === void 0 ? void 0 : _d.dataMap);
|
|
213
443
|
newMutualDataMap.set(mutual.entityId, mutual);
|
|
214
444
|
if (!hasRequestFailed) {
|
|
215
445
|
const entityState = store.entity[entityType];
|
|
@@ -219,16 +449,47 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
219
449
|
monoriseStore.setState(produce((state) => {
|
|
220
450
|
state.mutual[selfKey] = Object.assign(Object.assign({}, (state.mutual[selfKey] || {})), { dataMap: newMutualDataMap });
|
|
221
451
|
state.entity[entityType].dataMap = newEntityDataMap;
|
|
222
|
-
}), undefined, `mr/mutual/get/${byEntityType
|
|
452
|
+
}), undefined, `mr/mutual/get/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
223
453
|
}
|
|
454
|
+
return { data: mutual };
|
|
224
455
|
};
|
|
225
456
|
const createMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
|
|
457
|
+
var _a;
|
|
226
458
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
227
|
-
const
|
|
459
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
460
|
+
try {
|
|
461
|
+
const { data: mutual } = await mutualService.createMutual(byEntityId, entityId, payload, opts);
|
|
462
|
+
monoriseStore.setState(produce((state) => {
|
|
463
|
+
var _a, _b;
|
|
464
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
465
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
466
|
+
if (!state.mutual[bySide]) {
|
|
467
|
+
state.mutual[bySide] = {
|
|
468
|
+
dataMap: new Map(),
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
|
|
472
|
+
if (!state.mutual[side]) {
|
|
473
|
+
state.mutual[side] = {
|
|
474
|
+
dataMap: new Map(),
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
|
|
478
|
+
}), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
479
|
+
return { data: mutual };
|
|
480
|
+
}
|
|
481
|
+
catch (err) {
|
|
482
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
483
|
+
onError(error);
|
|
484
|
+
return { error };
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
const createLocalMutual = async (byEntityType, entityType, byEntityId, entityId, mutualData, data) => {
|
|
488
|
+
const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
|
|
228
489
|
monoriseStore.setState(produce((state) => {
|
|
229
490
|
var _a, _b;
|
|
230
|
-
const bySide =
|
|
231
|
-
const side =
|
|
491
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
492
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
232
493
|
if (!state.mutual[bySide]) {
|
|
233
494
|
state.mutual[bySide] = {
|
|
234
495
|
dataMap: new Map(),
|
|
@@ -241,7 +502,7 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
241
502
|
};
|
|
242
503
|
}
|
|
243
504
|
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
|
|
244
|
-
}), undefined, `mr/mutual/create/${byEntityType
|
|
505
|
+
}), undefined, `mr/mutual/create/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
245
506
|
};
|
|
246
507
|
const updateLocalEntity = async (entityType, entityId, data = {}) => {
|
|
247
508
|
const createdEntity = constructLocal(entityType, entityId, data);
|
|
@@ -253,8 +514,8 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
253
514
|
const mutual = constructMutual(byEntityType, byEntityId, entityType, entityId, mutualData, data);
|
|
254
515
|
monoriseStore.setState(produce((state) => {
|
|
255
516
|
var _a, _b;
|
|
256
|
-
const bySide =
|
|
257
|
-
const side =
|
|
517
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
518
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
258
519
|
if (!state.mutual[bySide]) {
|
|
259
520
|
state.mutual[bySide] = {
|
|
260
521
|
dataMap: new Map(),
|
|
@@ -267,57 +528,81 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
267
528
|
};
|
|
268
529
|
}
|
|
269
530
|
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(byEntityId, flipMutual(mutual));
|
|
270
|
-
}), undefined, `mr/mutual/local-update/${byEntityType
|
|
531
|
+
}), undefined, `mr/mutual/local-update/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
271
532
|
};
|
|
272
533
|
const editMutual = async (byEntityType, entityType, byEntityId, entityId, payload = {}, opts = {}) => {
|
|
534
|
+
var _a;
|
|
273
535
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
state.mutual[
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
536
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
537
|
+
try {
|
|
538
|
+
const { data: mutual } = await mutualService.editMutual(byEntityId, entityId, payload, opts);
|
|
539
|
+
monoriseStore.setState(produce((state) => {
|
|
540
|
+
var _a, _b;
|
|
541
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
542
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
543
|
+
if (!state.mutual[bySide]) {
|
|
544
|
+
state.mutual[bySide] = {
|
|
545
|
+
dataMap: new Map(),
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
state.mutual[bySide].dataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap).set(mutual.entityId, mutual);
|
|
549
|
+
if (!state.mutual[side]) {
|
|
550
|
+
state.mutual[side] = {
|
|
551
|
+
dataMap: new Map(),
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
state.mutual[side].dataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap).set(mutual.byEntityId, flipMutual(mutual));
|
|
555
|
+
}), undefined, `mr/mutual/edit/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
556
|
+
return { data: mutual };
|
|
557
|
+
}
|
|
558
|
+
catch (err) {
|
|
559
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
560
|
+
onError(error);
|
|
561
|
+
return { error };
|
|
562
|
+
}
|
|
292
563
|
};
|
|
293
564
|
const deleteMutual = async (byEntityType, entityType, byEntityId, entityId = '', opts = {}) => {
|
|
565
|
+
var _a;
|
|
294
566
|
const mutualService = makeMutualService(byEntityType, entityType);
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
state.mutual[
|
|
302
|
-
|
|
303
|
-
|
|
567
|
+
const onError = (_a = opts.onError) !== null && _a !== void 0 ? _a : defaultOnError;
|
|
568
|
+
try {
|
|
569
|
+
const { data } = await mutualService.deleteMutual(byEntityId, entityId, opts);
|
|
570
|
+
monoriseStore.setState(produce((state) => {
|
|
571
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
572
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
573
|
+
state.mutual[bySide].dataMap.delete(data.entityId);
|
|
574
|
+
if (state.mutual[side]) {
|
|
575
|
+
state.mutual[side].dataMap.delete(data.byEntityId);
|
|
576
|
+
}
|
|
577
|
+
}), undefined, `mr/mutual/delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
578
|
+
return { data };
|
|
579
|
+
}
|
|
580
|
+
catch (err) {
|
|
581
|
+
const error = err instanceof Error ? err : new Error('Unknown error occurred');
|
|
582
|
+
onError(error);
|
|
583
|
+
return { error };
|
|
584
|
+
}
|
|
304
585
|
};
|
|
305
586
|
const deleteLocalMutual = (byEntityType, entityType, byEntityId, entityId) => {
|
|
306
587
|
monoriseStore.setState(produce((state) => {
|
|
307
588
|
var _a, _b;
|
|
308
|
-
const bySide =
|
|
309
|
-
const side =
|
|
310
|
-
const bySideDataMap = new Map((_a = state[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
|
|
311
|
-
const sideDataMap = new Map((_b = state[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
|
|
589
|
+
const bySide = getMutualStateKey(byEntityType, byEntityId, entityType);
|
|
590
|
+
const side = getMutualStateKey(entityType, entityId, byEntityType);
|
|
591
|
+
const bySideDataMap = new Map((_a = state.mutual[bySide]) === null || _a === void 0 ? void 0 : _a.dataMap);
|
|
592
|
+
const sideDataMap = new Map((_b = state.mutual[side]) === null || _b === void 0 ? void 0 : _b.dataMap);
|
|
312
593
|
bySideDataMap.delete(entityId);
|
|
313
594
|
sideDataMap.delete(byEntityId);
|
|
314
|
-
state.mutual[bySide]
|
|
315
|
-
|
|
316
|
-
|
|
595
|
+
if (state.mutual[bySide]) {
|
|
596
|
+
state.mutual[bySide].dataMap.delete(entityId);
|
|
597
|
+
}
|
|
598
|
+
if (state.mutual[side]) {
|
|
599
|
+
state.mutual[side].dataMap.delete(byEntityId);
|
|
600
|
+
}
|
|
601
|
+
}), undefined, `mr/mutual/local-delete/${getMutualStateKey(byEntityType, byEntityId, entityType, entityId)}`);
|
|
317
602
|
};
|
|
318
603
|
const deleteLocalMutualsByEntity = (entityType, id) => {
|
|
319
604
|
const store = monoriseStore.getState();
|
|
320
|
-
const entityConfig = store.config;
|
|
605
|
+
const entityConfig = store.config; // Assuming store.config holds mutual related configurations
|
|
321
606
|
let mutuals = [];
|
|
322
607
|
for (const i of Object.keys(entityConfig)) {
|
|
323
608
|
const mutualState = store.mutual[`${entityType}/${id}/${i}`];
|
|
@@ -327,19 +612,26 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
327
612
|
];
|
|
328
613
|
}
|
|
329
614
|
const updatedState = mutuals.reduce((acc, mutual) => {
|
|
330
|
-
|
|
331
|
-
const
|
|
615
|
+
var _a;
|
|
616
|
+
const side = getMutualStateKey(mutual.entityType, mutual.entityId, mutual.byEntityType);
|
|
617
|
+
const dataMap = new Map((_a = store.mutual[side]) === null || _a === void 0 ? void 0 : _a.dataMap); // Use optional chaining
|
|
332
618
|
dataMap.delete(id);
|
|
333
619
|
acc[side] = Object.assign(Object.assign({}, store.mutual[side]), { dataMap });
|
|
334
620
|
return acc;
|
|
335
621
|
}, {});
|
|
336
622
|
monoriseStore.setState(produce((state) => {
|
|
337
|
-
|
|
623
|
+
// This is a full replacement, might not be desired for partial updates.
|
|
624
|
+
// If state.mutual can contain other keys, this might clear them.
|
|
625
|
+
// Consider a more granular update if needed.
|
|
626
|
+
for (const key in updatedState) {
|
|
627
|
+
state.mutual[key] = updatedState[key];
|
|
628
|
+
}
|
|
338
629
|
}), undefined, `mr/mutual/local-delete-by-entity/${entityType}/${id}`);
|
|
339
630
|
};
|
|
340
631
|
const useEntity = (entityType, id, opts = {}) => {
|
|
341
632
|
const dataMap = monoriseStore((state) => { var _a; return ((_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap) || new Map(); });
|
|
342
|
-
const
|
|
633
|
+
const isFirstFetched = monoriseStore((state) => { var _a; return (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.isFirstFetched; });
|
|
634
|
+
const requestKey = getEntityRequestKey('get', entityType, id);
|
|
343
635
|
const isLoading = useLoadStore(requestKey);
|
|
344
636
|
const error = useErrorStore(requestKey);
|
|
345
637
|
useEffect(() => {
|
|
@@ -352,14 +644,53 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
352
644
|
isLoading,
|
|
353
645
|
error,
|
|
354
646
|
requestKey,
|
|
647
|
+
isFirstFetched,
|
|
648
|
+
refetch: async () => {
|
|
649
|
+
if (id) {
|
|
650
|
+
const { data, error: fetchError } = await getEntity(entityType, id, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
651
|
+
if (fetchError) {
|
|
652
|
+
// Handle refetch error if necessary, e.g., throw or set local state
|
|
653
|
+
}
|
|
654
|
+
return data;
|
|
655
|
+
}
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
const useEntityByUniqueField = (entityType, fieldName, value, opts = {}) => {
|
|
660
|
+
const dataMap = monoriseStore((state) => { var _a; return ((_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.dataMap) || new Map(); });
|
|
661
|
+
const isFirstFetched = monoriseStore((state) => { var _a; return (_a = state.entity[entityType]) === null || _a === void 0 ? void 0 : _a.isFirstFetched; });
|
|
662
|
+
const stateKey = getUniqueFieldStateKey(fieldName, value || '');
|
|
663
|
+
const requestKey = getUniqueFieldRequestKey(entityType, fieldName, value || '');
|
|
664
|
+
const isLoading = useLoadStore(requestKey);
|
|
665
|
+
const error = useErrorStore(requestKey);
|
|
666
|
+
useEffect(() => {
|
|
667
|
+
if (value) {
|
|
668
|
+
getEntityByUniqueField(entityType, fieldName, value, opts);
|
|
669
|
+
}
|
|
670
|
+
}, [fieldName, value, entityType, opts]);
|
|
671
|
+
return {
|
|
672
|
+
entity: value ? dataMap.get(`${stateKey}`) : undefined,
|
|
673
|
+
isLoading,
|
|
674
|
+
error,
|
|
675
|
+
requestKey,
|
|
676
|
+
isFirstFetched,
|
|
677
|
+
refetch: async () => {
|
|
678
|
+
if (value) {
|
|
679
|
+
const { data, error: fetchError } = await getEntityByUniqueField(entityType, fieldName, value, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
680
|
+
if (fetchError) {
|
|
681
|
+
// Handle refetch error if necessary
|
|
682
|
+
}
|
|
683
|
+
return data;
|
|
684
|
+
}
|
|
685
|
+
},
|
|
355
686
|
};
|
|
356
687
|
};
|
|
357
688
|
const useEntities = (entityType, params = {}, opts = {}) => {
|
|
358
|
-
const requestKey =
|
|
689
|
+
const requestKey = getEntityRequestKey('list', entityType);
|
|
359
690
|
const isListing = useLoadStore(requestKey);
|
|
360
691
|
const error = useErrorStore(requestKey);
|
|
361
692
|
const state = monoriseStore((state) => state.entity[entityType]);
|
|
362
|
-
const { dataMap, searchResults, isFirstFetched } = state !== null && state !== void 0 ? state : {
|
|
693
|
+
const { dataMap, searchResults, isFirstFetched, lastKey } = state !== null && state !== void 0 ? state : {
|
|
363
694
|
dataMap: new Map(),
|
|
364
695
|
};
|
|
365
696
|
const [entities, setEntities] = useState();
|
|
@@ -386,13 +717,14 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
386
717
|
}
|
|
387
718
|
}, [all, entityType, skRange, opts, isFirstFetched]);
|
|
388
719
|
useEffect(() => {
|
|
720
|
+
var _a;
|
|
389
721
|
let queryTimeout;
|
|
390
722
|
if (query === null || query === void 0 ? void 0 : query.length) {
|
|
391
723
|
setIsSearching(true);
|
|
392
724
|
queryTimeout = setTimeout(async () => {
|
|
393
725
|
await searchEntities(entityType, query);
|
|
394
726
|
setIsSearching(false);
|
|
395
|
-
}, 700);
|
|
727
|
+
}, (_a = opts.searchInterval) !== null && _a !== void 0 ? _a : 700);
|
|
396
728
|
}
|
|
397
729
|
return () => queryTimeout && clearTimeout(queryTimeout);
|
|
398
730
|
}, [entityType, query]);
|
|
@@ -426,11 +758,26 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
426
758
|
isLoading,
|
|
427
759
|
error,
|
|
428
760
|
requestKey,
|
|
761
|
+
isFirstFetched,
|
|
762
|
+
lastKey,
|
|
763
|
+
refetch: async () => {
|
|
764
|
+
if (entityType && query) {
|
|
765
|
+
await searchEntities(entityType, query);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
await listEntities(entityType, { skRange, all }, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
769
|
+
},
|
|
770
|
+
listMore: async () => {
|
|
771
|
+
if (!lastKey)
|
|
772
|
+
return;
|
|
773
|
+
await listMoreEntities(entityType, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
774
|
+
},
|
|
429
775
|
};
|
|
430
776
|
};
|
|
431
777
|
const useMutual = (byEntityType, entityType, byId, id, opts = {}) => {
|
|
432
|
-
const
|
|
433
|
-
const
|
|
778
|
+
const stateKey = getMutualStateKey(byEntityType, byId, entityType);
|
|
779
|
+
const state = monoriseStore((state) => state.mutual[stateKey]);
|
|
780
|
+
const requestKey = getMutualRequestKey('get', byEntityType, entityType, byId, id);
|
|
434
781
|
const isLoading = useLoadStore(requestKey);
|
|
435
782
|
const error = useErrorStore(requestKey);
|
|
436
783
|
const { dataMap } = state || {
|
|
@@ -449,17 +796,18 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
449
796
|
};
|
|
450
797
|
};
|
|
451
798
|
const useMutuals = (byEntityType, entityType, byId, opts = {}, chainEntityQuery) => {
|
|
452
|
-
const
|
|
453
|
-
const
|
|
799
|
+
const stateKey = getMutualStateKey(byEntityType, byId || '', entityType, undefined, chainEntityQuery);
|
|
800
|
+
const state = monoriseStore((state) => state.mutual[stateKey]);
|
|
801
|
+
const { dataMap, isFirstFetched, lastKey } = state || {
|
|
454
802
|
dataMap: new Map(),
|
|
455
803
|
};
|
|
456
804
|
const [mutuals, setMutuals] = useState([]);
|
|
457
|
-
const requestKey =
|
|
805
|
+
const requestKey = getMutualRequestKey('list', byEntityType, entityType, byId || '', undefined, chainEntityQuery);
|
|
458
806
|
const isLoading = useLoadStore(requestKey);
|
|
459
807
|
const error = useErrorStore(requestKey);
|
|
460
808
|
useEffect(() => {
|
|
461
809
|
if (!isFirstFetched && byEntityType && entityType && byId) {
|
|
462
|
-
listEntitiesByEntity(byEntityType, entityType, byId, opts, chainEntityQuery);
|
|
810
|
+
listEntitiesByEntity(byEntityType, entityType, byId, Object.assign(Object.assign({}, opts), { stateKey }), chainEntityQuery);
|
|
463
811
|
}
|
|
464
812
|
}, [
|
|
465
813
|
isFirstFetched,
|
|
@@ -468,42 +816,118 @@ const initCoreActions = (monoriseStore, appActions, coreService) => {
|
|
|
468
816
|
entityType,
|
|
469
817
|
opts,
|
|
470
818
|
chainEntityQuery,
|
|
819
|
+
stateKey,
|
|
820
|
+
opts === null || opts === void 0 ? void 0 : opts.forceFetch,
|
|
821
|
+
opts === null || opts === void 0 ? void 0 : opts.noData,
|
|
471
822
|
]);
|
|
472
823
|
useEffect(() => {
|
|
473
|
-
|
|
474
|
-
|
|
824
|
+
const dataMapArray = Array.from(dataMap.values());
|
|
825
|
+
if (dataMap.size !== (mutuals === null || mutuals === void 0 ? void 0 : mutuals.length) ||
|
|
826
|
+
dataMapArray.some((item, index) => JSON.stringify(item) !== JSON.stringify(mutuals[index]))) {
|
|
827
|
+
setMutuals(dataMapArray);
|
|
475
828
|
}
|
|
476
829
|
}, [dataMap, dataMap.size, mutuals === null || mutuals === void 0 ? void 0 : mutuals.length]);
|
|
830
|
+
return Object.assign({ mutuals, mutualsMap: dataMap, isLoading,
|
|
831
|
+
requestKey,
|
|
832
|
+
error,
|
|
833
|
+
isFirstFetched,
|
|
834
|
+
lastKey }, (lastKey && {
|
|
835
|
+
listMore: () => {
|
|
836
|
+
if (byEntityType && entityType && byId) {
|
|
837
|
+
listEntitiesByEntity(byEntityType, entityType, byId, Object.assign(Object.assign({}, opts), { forceFetch: true, params: Object.assign(Object.assign({}, opts.params), { lastKey }), stateKey }), chainEntityQuery);
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
}));
|
|
841
|
+
};
|
|
842
|
+
const useTaggedEntities = (entityType, tagName, opts = {}) => {
|
|
843
|
+
const { params } = opts || {};
|
|
844
|
+
const stateKey = getTagStateKey(entityType, tagName, params);
|
|
845
|
+
const state = monoriseStore((state) => state.tag[stateKey]);
|
|
846
|
+
const { dataMap, isFirstFetched, lastKey } = state || {
|
|
847
|
+
dataMap: new Map(),
|
|
848
|
+
};
|
|
849
|
+
const [entities, setEntities] = useState([]);
|
|
850
|
+
const requestKey = getTagRequestKey('list', entityType, tagName, params);
|
|
851
|
+
const isLoading = useLoadStore(requestKey);
|
|
852
|
+
const error = useErrorStore(requestKey);
|
|
853
|
+
useEffect(() => {
|
|
854
|
+
if (entityType && tagName && Object.keys(params).length > 0) {
|
|
855
|
+
listEntitiesByTag(entityType, tagName, opts);
|
|
856
|
+
}
|
|
857
|
+
}, [entityType, opts, tagName, params, opts === null || opts === void 0 ? void 0 : opts.forceFetch]);
|
|
858
|
+
useEffect(() => {
|
|
859
|
+
const dataMapArray = Array.from(dataMap.values());
|
|
860
|
+
if (dataMap.size !== (entities === null || entities === void 0 ? void 0 : entities.length) ||
|
|
861
|
+
dataMapArray.some((item, index) => JSON.stringify(item) !== JSON.stringify(entities[index]))) {
|
|
862
|
+
setEntities(dataMapArray);
|
|
863
|
+
}
|
|
864
|
+
}, [dataMap, dataMap.size, entities === null || entities === void 0 ? void 0 : entities.length]);
|
|
477
865
|
return {
|
|
478
|
-
|
|
479
|
-
|
|
866
|
+
entities,
|
|
867
|
+
entitiesMap: dataMap,
|
|
480
868
|
isLoading,
|
|
481
869
|
requestKey,
|
|
482
870
|
error,
|
|
871
|
+
isFirstFetched,
|
|
872
|
+
lastKey,
|
|
873
|
+
refetch: async () => {
|
|
874
|
+
if (entityType && tagName && (params === null || params === void 0 ? void 0 : params.group)) {
|
|
875
|
+
return await listEntitiesByTag(entityType, tagName, Object.assign(Object.assign({}, opts), { forceFetch: true }));
|
|
876
|
+
}
|
|
877
|
+
},
|
|
878
|
+
listMore: async () => {
|
|
879
|
+
if (entityType && tagName && (params === null || params === void 0 ? void 0 : params.group)) {
|
|
880
|
+
return await listEntitiesByTag(entityType, tagName, Object.assign(Object.assign({}, opts), { forceFetch: true, params: Object.assign(Object.assign({}, params), { lastKey }) }));
|
|
881
|
+
}
|
|
882
|
+
},
|
|
483
883
|
};
|
|
484
884
|
};
|
|
485
885
|
const useEntityState = (entityType) => {
|
|
486
886
|
return monoriseStore((state) => state.entity[entityType]);
|
|
487
887
|
};
|
|
888
|
+
const updateLocalTaggedEntity = (entityType, entityId, tagName, data = {}, params) => {
|
|
889
|
+
const tagKey = getTagStateKey(entityType, tagName, params);
|
|
890
|
+
monoriseStore.setState(produce((state) => {
|
|
891
|
+
var _a, _b;
|
|
892
|
+
const entity = (_b = (_a = state.tag[tagKey]) === null || _a === void 0 ? void 0 : _a.dataMap) === null || _b === void 0 ? void 0 : _b.get(entityId);
|
|
893
|
+
if (entity) {
|
|
894
|
+
state.tag[tagKey].dataMap.set(entityId, Object.assign(Object.assign({}, entity), { data: Object.assign(Object.assign({}, entity.data), data) }));
|
|
895
|
+
}
|
|
896
|
+
}), undefined, `mr/tag/local-update/${entityType}/${entityId}`);
|
|
897
|
+
};
|
|
898
|
+
const deleteLocalTaggedEntity = (entityType, entityId, tagName, params) => {
|
|
899
|
+
const tagKey = getTagStateKey(entityType, tagName, params);
|
|
900
|
+
monoriseStore.setState(produce((state) => {
|
|
901
|
+
var _a, _b;
|
|
902
|
+
if ((_b = (_a = state.tag[tagKey]) === null || _a === void 0 ? void 0 : _a.dataMap) === null || _b === void 0 ? void 0 : _b.has(entityId)) {
|
|
903
|
+
state.tag[tagKey].dataMap.delete(entityId);
|
|
904
|
+
}
|
|
905
|
+
}), undefined, `mr/tag/local-delete/${entityType}/${entityId}`);
|
|
906
|
+
};
|
|
488
907
|
return {
|
|
489
908
|
listMoreEntities,
|
|
490
909
|
createEntity,
|
|
491
910
|
upsertEntity,
|
|
911
|
+
getEntity,
|
|
492
912
|
editEntity,
|
|
493
913
|
deleteEntity,
|
|
494
914
|
getMutual,
|
|
495
915
|
updateLocalEntity,
|
|
496
916
|
createMutual,
|
|
917
|
+
createLocalMutual,
|
|
497
918
|
upsertLocalMutual,
|
|
498
919
|
editMutual,
|
|
499
920
|
deleteMutual,
|
|
500
921
|
deleteLocalMutual,
|
|
501
922
|
useEntity,
|
|
923
|
+
useEntityByUniqueField,
|
|
502
924
|
useEntities,
|
|
503
925
|
useMutual,
|
|
504
926
|
useMutuals,
|
|
505
|
-
|
|
927
|
+
useTaggedEntities,
|
|
506
928
|
useEntityState,
|
|
929
|
+
updateLocalTaggedEntity,
|
|
930
|
+
deleteLocalTaggedEntity,
|
|
507
931
|
};
|
|
508
932
|
};
|
|
509
933
|
export { initCoreActions };
|