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