@monorise/react 0.0.2-dev.1 → 1.0.1
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/actions/app.action.ts +218 -0
- package/actions/auth.action.ts +71 -0
- package/actions/core.action.ts +878 -0
- package/dist/actions/auth.action.d.ts +0 -1
- package/dist/actions/auth.action.d.ts.map +1 -1
- package/dist/actions/auth.action.js +6 -23
- package/dist/actions/auth.action.js.map +1 -1
- package/dist/actions/core.action.d.ts +1 -3
- package/dist/actions/core.action.d.ts.map +1 -1
- package/dist/actions/core.action.js +5 -38
- package/dist/actions/core.action.js.map +1 -1
- package/dist/index.d.ts +57 -430
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -16
- package/dist/index.js.map +1 -1
- package/dist/lib/api.d.ts +1 -6
- package/dist/lib/api.d.ts.map +1 -1
- package/dist/lib/{api.js → api.jsx} +20 -26
- package/dist/lib/api.jsx.map +1 -0
- package/dist/lib/config.d.ts +5 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +24 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/entity.d.ts +1 -2
- package/dist/lib/entity.d.ts.map +1 -1
- package/dist/lib/entity.js.map +1 -1
- package/dist/services/auth.service.d.ts +0 -1
- package/dist/services/auth.service.d.ts.map +1 -1
- package/dist/services/auth.service.js +0 -4
- package/dist/services/auth.service.js.map +1 -1
- package/dist/services/core.service.d.ts +2 -4
- package/dist/services/core.service.d.ts.map +1 -1
- package/dist/services/core.service.js +10 -9
- package/dist/services/core.service.js.map +1 -1
- package/dist/store/monorise.store.d.ts +3 -10
- package/dist/store/monorise.store.d.ts.map +1 -1
- package/dist/store/monorise.store.js +11 -3
- package/dist/store/monorise.store.js.map +1 -1
- package/index.ts +146 -0
- package/lib/api.tsx +132 -0
- package/lib/config.ts +36 -0
- package/lib/constant.ts +1 -0
- package/lib/entity.ts +81 -0
- package/lib/utils.ts +12 -0
- package/package.json +7 -11
- package/services/auth.service.ts +59 -0
- package/services/core.service.ts +416 -0
- package/services/filestore.service.ts +136 -0
- package/store/monorise.store.ts +111 -0
- package/tsconfig.json +114 -0
- package/types/api.type.ts +57 -0
- package/types/axios.type.ts +14 -0
- package/types/monorise.type.ts +8 -0
- package/dist/actions/config.action.d.ts +0 -10
- package/dist/actions/config.action.d.ts.map +0 -1
- package/dist/actions/config.action.js +0 -31
- package/dist/actions/config.action.js.map +0 -1
- package/dist/lib/api.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/mutual.type.d.ts +0 -21
- package/dist/types/mutual.type.d.ts.map +0 -1
- package/dist/types/mutual.type.js +0 -2
- package/dist/types/mutual.type.js.map +0 -1
|
@@ -0,0 +1,878 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreatedEntity,
|
|
3
|
+
DraftEntity,
|
|
4
|
+
Entity,
|
|
5
|
+
EntitySchemaMap,
|
|
6
|
+
Mutual,
|
|
7
|
+
MutualData,
|
|
8
|
+
} from '@monorise/base';
|
|
9
|
+
import { produce } from 'immer';
|
|
10
|
+
import { useEffect, useState } from 'react';
|
|
11
|
+
import { getEntityConfig } from '../lib/config';
|
|
12
|
+
import {
|
|
13
|
+
byEntityId,
|
|
14
|
+
constructLocal,
|
|
15
|
+
constructMutual,
|
|
16
|
+
flipMutual,
|
|
17
|
+
} from '../lib/entity';
|
|
18
|
+
import { convertToMap } from '../lib/utils';
|
|
19
|
+
import type {
|
|
20
|
+
CommonOptions,
|
|
21
|
+
CoreService,
|
|
22
|
+
ListEntitiesByTagParams,
|
|
23
|
+
} from '../services/core.service';
|
|
24
|
+
import type { MonoriseStore } from '../store/monorise.store';
|
|
25
|
+
import type { ApplicationRequestError } from '../types/api.type';
|
|
26
|
+
import type { CommonStore } from '../types/monorise.type';
|
|
27
|
+
import type { AppActions } from './app.action';
|
|
28
|
+
|
|
29
|
+
// ===== Important tips ======
|
|
30
|
+
// Should we use store.getState() or store()?
|
|
31
|
+
// USE store.getState() WHEN using within an action such as getEntity, getProfile
|
|
32
|
+
// USE store() WHEN using within a react hook so that it gets the benefit of reactivity, such as useProfile, useEntity
|
|
33
|
+
|
|
34
|
+
const initCoreActions = (
|
|
35
|
+
monoriseStore: MonoriseStore,
|
|
36
|
+
appActions: AppActions,
|
|
37
|
+
coreService: CoreService,
|
|
38
|
+
) => {
|
|
39
|
+
const { makeEntityService, makeMutualService } = coreService;
|
|
40
|
+
const { checkIsLoading, getError, useLoadStore, useErrorStore } = appActions;
|
|
41
|
+
|
|
42
|
+
const listEntities = async <T extends Entity>(
|
|
43
|
+
entityType: T,
|
|
44
|
+
params: {
|
|
45
|
+
skRange?: { start: string; end: string };
|
|
46
|
+
all?: boolean;
|
|
47
|
+
} = {},
|
|
48
|
+
opts: CommonOptions = {},
|
|
49
|
+
) => {
|
|
50
|
+
const store = monoriseStore.getState();
|
|
51
|
+
const entityState = store.entity[entityType];
|
|
52
|
+
const { isFirstFetched } = entityState;
|
|
53
|
+
const entityService = makeEntityService(entityType);
|
|
54
|
+
const { skRange } = params;
|
|
55
|
+
const requestKey = `entity/${entityType}/list`;
|
|
56
|
+
const isLoading = checkIsLoading(requestKey);
|
|
57
|
+
const error = getError(requestKey);
|
|
58
|
+
|
|
59
|
+
if ((isFirstFetched && !params.skRange) || isLoading || error) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { data: result } = await entityService.listEntities(
|
|
64
|
+
{
|
|
65
|
+
...(params?.all ? {} : { limit: 20 }),
|
|
66
|
+
start: skRange?.start,
|
|
67
|
+
end: skRange?.end,
|
|
68
|
+
},
|
|
69
|
+
opts,
|
|
70
|
+
);
|
|
71
|
+
const newEntityMap = convertToMap<CreatedEntity<T>>(
|
|
72
|
+
result.data,
|
|
73
|
+
'entityId',
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const mergedMap = new Map([
|
|
77
|
+
...newEntityMap,
|
|
78
|
+
...store.entity[entityType].dataMap,
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
monoriseStore.setState(
|
|
82
|
+
produce((state) => {
|
|
83
|
+
state.entity[entityType] = {
|
|
84
|
+
dataMap: params.skRange ? newEntityMap : mergedMap,
|
|
85
|
+
isFirstFetched: true,
|
|
86
|
+
lastKey: result.lastKey,
|
|
87
|
+
};
|
|
88
|
+
}),
|
|
89
|
+
undefined,
|
|
90
|
+
`mr/entity/list/${entityType}`,
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const listMoreEntities = async <T extends Entity>(
|
|
95
|
+
entityType: T,
|
|
96
|
+
opts: CommonOptions = {},
|
|
97
|
+
) => {
|
|
98
|
+
const store = monoriseStore.getState();
|
|
99
|
+
const entityState = store.entity[entityType];
|
|
100
|
+
const { dataMap, lastKey } = entityState;
|
|
101
|
+
const entityService = makeEntityService(entityType);
|
|
102
|
+
|
|
103
|
+
if (!lastKey) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const { data: result } = await entityService.listEntities(
|
|
108
|
+
{
|
|
109
|
+
limit: 20,
|
|
110
|
+
lastKey,
|
|
111
|
+
},
|
|
112
|
+
opts,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
monoriseStore.setState(
|
|
116
|
+
produce((state) => {
|
|
117
|
+
state.entity[entityType].isFirstFetched = true;
|
|
118
|
+
state.entity[entityType].lastKey = result.lastKey;
|
|
119
|
+
for (const i in result.data) {
|
|
120
|
+
state.entity[entityType].dataMap.set(
|
|
121
|
+
result.data[i].entityId,
|
|
122
|
+
result.data[i],
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}),
|
|
126
|
+
undefined,
|
|
127
|
+
`mr/entity/list-more/${entityType}`,
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const searchEntities = async <T extends Entity>(
|
|
132
|
+
entityType: T,
|
|
133
|
+
query: string,
|
|
134
|
+
opts: CommonOptions = {},
|
|
135
|
+
) => {
|
|
136
|
+
const entityService = makeEntityService(entityType);
|
|
137
|
+
const { data: result } = await entityService.searchEntities(query, opts);
|
|
138
|
+
|
|
139
|
+
monoriseStore.setState(
|
|
140
|
+
produce((state) => {
|
|
141
|
+
state.entity[entityType].searchResults = result.data;
|
|
142
|
+
for (const i in result.data) {
|
|
143
|
+
state.entity[entityType].dataMap.set(
|
|
144
|
+
result.data[i].entityId,
|
|
145
|
+
result.data[i],
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const listEntitiesByTag = async <T extends Entity>(
|
|
153
|
+
entityType: T,
|
|
154
|
+
tagName: string,
|
|
155
|
+
opts: CommonOptions & { params?: ListEntitiesByTagParams } = {},
|
|
156
|
+
) => {
|
|
157
|
+
const tagKey = `${entityType}/${tagName}/${opts.params?.group || ''}`;
|
|
158
|
+
|
|
159
|
+
const store = monoriseStore.getState();
|
|
160
|
+
const tagState = store.tag[tagKey] || {};
|
|
161
|
+
const { isFirstFetched, dataMap } = tagState;
|
|
162
|
+
const entityService = makeEntityService(entityType);
|
|
163
|
+
|
|
164
|
+
if (isFirstFetched) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { data } = await entityService.listEntitiesByTag(tagName, opts);
|
|
169
|
+
const { entities, lastKey } = data;
|
|
170
|
+
|
|
171
|
+
monoriseStore.setState(
|
|
172
|
+
produce((state) => {
|
|
173
|
+
for (const entity of entities) {
|
|
174
|
+
state.entity[entityType].dataMap.set(entity.entityId, entity);
|
|
175
|
+
}
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
monoriseStore.setState(
|
|
180
|
+
produce((state) => {
|
|
181
|
+
state.tag[tagKey] = {
|
|
182
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
183
|
+
isFirstFetched: true,
|
|
184
|
+
lastKey,
|
|
185
|
+
};
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
const getEntity = async <T extends Entity>(
|
|
190
|
+
entityType: T,
|
|
191
|
+
id: string,
|
|
192
|
+
opts: CommonOptions = {},
|
|
193
|
+
) => {
|
|
194
|
+
const store = monoriseStore.getState();
|
|
195
|
+
const entityState = store.entity[entityType];
|
|
196
|
+
const { dataMap } = entityState;
|
|
197
|
+
const entityService = makeEntityService(entityType);
|
|
198
|
+
let entity = dataMap.get(id);
|
|
199
|
+
const requestKey = `entity/${entityType}/get/${id}`;
|
|
200
|
+
const isLoading = checkIsLoading(requestKey);
|
|
201
|
+
const error = getError(requestKey);
|
|
202
|
+
|
|
203
|
+
if (entity || isLoading || error) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
({ data: entity } = await entityService.getEntity(id, opts));
|
|
208
|
+
|
|
209
|
+
monoriseStore.setState(
|
|
210
|
+
produce((state) => {
|
|
211
|
+
state.entity[entityType].dataMap.set(entity?.entityId, entity);
|
|
212
|
+
}),
|
|
213
|
+
undefined,
|
|
214
|
+
`mr/entity/get/${entityType}/${id}`,
|
|
215
|
+
);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const createEntity = async <T extends Entity>(
|
|
219
|
+
entityType: T,
|
|
220
|
+
entity: DraftEntity<T>,
|
|
221
|
+
opts: CommonOptions = {},
|
|
222
|
+
) => {
|
|
223
|
+
const entityService = makeEntityService(entityType);
|
|
224
|
+
const { data } = await entityService.createEntity(entity, opts);
|
|
225
|
+
|
|
226
|
+
monoriseStore.setState(
|
|
227
|
+
produce((state) => {
|
|
228
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
229
|
+
}),
|
|
230
|
+
undefined,
|
|
231
|
+
`mr/entity/create/${entityType}`,
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return data;
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const upsertEntity = async <T extends Entity>(
|
|
238
|
+
entityType: T,
|
|
239
|
+
id: string,
|
|
240
|
+
entity: DraftEntity<T>,
|
|
241
|
+
opts: CommonOptions = {},
|
|
242
|
+
) => {
|
|
243
|
+
const entityService = makeEntityService(entityType);
|
|
244
|
+
const { data } = await entityService.upsertEntity(id, entity, opts);
|
|
245
|
+
|
|
246
|
+
monoriseStore.setState(
|
|
247
|
+
produce((state) => {
|
|
248
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
249
|
+
}),
|
|
250
|
+
undefined,
|
|
251
|
+
`mr/entity/upser/${entityType}/${id}`,
|
|
252
|
+
);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const editEntity = async <T extends Entity>(
|
|
256
|
+
entityType: T,
|
|
257
|
+
id: string,
|
|
258
|
+
entity: Partial<DraftEntity<T>>,
|
|
259
|
+
opts: CommonOptions = {},
|
|
260
|
+
) => {
|
|
261
|
+
const entityService = makeEntityService(entityType);
|
|
262
|
+
const { data } = await entityService.editEntity(id, entity, opts);
|
|
263
|
+
|
|
264
|
+
monoriseStore.setState(
|
|
265
|
+
produce((state) => {
|
|
266
|
+
state.entity[entityType].dataMap.set(data.entityId, data);
|
|
267
|
+
}),
|
|
268
|
+
undefined,
|
|
269
|
+
`mr/entity/edit/${entityType}/${id}`,
|
|
270
|
+
);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const deleteEntity = async <T extends Entity>(
|
|
274
|
+
entityType: T,
|
|
275
|
+
id: string,
|
|
276
|
+
opts: CommonOptions = {},
|
|
277
|
+
) => {
|
|
278
|
+
const entityService = makeEntityService(entityType);
|
|
279
|
+
await entityService.deleteEntity(id, opts);
|
|
280
|
+
deleteLocalMutualsByEntity(entityType, id);
|
|
281
|
+
|
|
282
|
+
monoriseStore.setState(
|
|
283
|
+
produce((state) => {
|
|
284
|
+
state.entity[entityType].dataMap.delete(id);
|
|
285
|
+
}),
|
|
286
|
+
undefined,
|
|
287
|
+
`mr/entity/delete/${entityType}/${id}`,
|
|
288
|
+
);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const listEntitiesByEntity = async <B extends Entity, T extends Entity>(
|
|
292
|
+
byEntityType: B,
|
|
293
|
+
entityType: T,
|
|
294
|
+
id: string,
|
|
295
|
+
opts: CommonOptions = {},
|
|
296
|
+
chainEntityQuery?: string,
|
|
297
|
+
) => {
|
|
298
|
+
const selfKey = `${byEntityType}/${id}/${entityType}`;
|
|
299
|
+
const mutualService = makeMutualService(byEntityType, entityType);
|
|
300
|
+
const store = monoriseStore.getState();
|
|
301
|
+
const mutualState = store.mutual[selfKey] || {};
|
|
302
|
+
const { isFirstFetched } = mutualState;
|
|
303
|
+
const requestKey = `mutual/${selfKey}/list`;
|
|
304
|
+
const isLoading = checkIsLoading(requestKey);
|
|
305
|
+
const error = getError(requestKey);
|
|
306
|
+
|
|
307
|
+
if (isFirstFetched || isLoading || error) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const { data } = await mutualService.listEntitiesByEntity(
|
|
312
|
+
id,
|
|
313
|
+
opts,
|
|
314
|
+
chainEntityQuery,
|
|
315
|
+
);
|
|
316
|
+
const { entities, lastKey } = data;
|
|
317
|
+
|
|
318
|
+
const newEntityDataMap = new Map();
|
|
319
|
+
|
|
320
|
+
for (const i in entities) {
|
|
321
|
+
newEntityDataMap.set(entities[i].entityId, entities[i]);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
monoriseStore.setState(
|
|
325
|
+
produce((state) => {
|
|
326
|
+
for (const [key, value] of newEntityDataMap) {
|
|
327
|
+
state.entity[entityType].dataMap.set(key, value);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
state.mutual[selfKey] = {
|
|
331
|
+
dataMap: convertToMap(entities, 'entityId'),
|
|
332
|
+
isFirstFetched: true,
|
|
333
|
+
lastKey,
|
|
334
|
+
};
|
|
335
|
+
}),
|
|
336
|
+
undefined,
|
|
337
|
+
`mr/mutual/list/${byEntityType}/${id}/${entityType}`,
|
|
338
|
+
);
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// todo: list more mutuals by entity
|
|
342
|
+
// const listMoremutualsByEntity = async (
|
|
343
|
+
// entityType: Entity,
|
|
344
|
+
// otherEntityType: Entity,
|
|
345
|
+
// id: string,
|
|
346
|
+
// ) =>
|
|
347
|
+
|
|
348
|
+
const getMutual = async <B extends Entity, T extends Entity>(
|
|
349
|
+
byEntityType: B,
|
|
350
|
+
entityType: T,
|
|
351
|
+
byEntityId: string | null,
|
|
352
|
+
entityId: string,
|
|
353
|
+
opts: CommonOptions & {
|
|
354
|
+
// if failed to retrieve, would still set a default mutual data
|
|
355
|
+
defaultMutualData?: Record<string, any>;
|
|
356
|
+
} = {},
|
|
357
|
+
) => {
|
|
358
|
+
const selfKey = `${byEntityType}/${byEntityId}/${entityType}`;
|
|
359
|
+
const mutualService = makeMutualService(byEntityType, entityType);
|
|
360
|
+
const store = monoriseStore.getState();
|
|
361
|
+
const mutualState = store.mutual[selfKey] || {};
|
|
362
|
+
const requestKey = `mutual/${byEntityType}/${byEntityId}/${entityType}/${entityId}/get`;
|
|
363
|
+
const isLoading = checkIsLoading(requestKey);
|
|
364
|
+
const error = getError(requestKey);
|
|
365
|
+
|
|
366
|
+
if (!byEntityId || isLoading || error) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (mutualState.dataMap?.get(entityId)) {
|
|
371
|
+
return mutualState.dataMap.get(entityId) as Mutual<B, T>;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
let mutual: Omit<Mutual<B, T>, 'data'>;
|
|
375
|
+
let hasRequestFailed = false;
|
|
376
|
+
|
|
377
|
+
try {
|
|
378
|
+
({ data: mutual } = await mutualService.getMutual(
|
|
379
|
+
byEntityId,
|
|
380
|
+
entityId,
|
|
381
|
+
opts,
|
|
382
|
+
));
|
|
383
|
+
} catch (err) {
|
|
384
|
+
if (!opts.defaultMutualData) {
|
|
385
|
+
throw err;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
hasRequestFailed = true;
|
|
389
|
+
mutual = {
|
|
390
|
+
createdAt: new Date().toISOString(),
|
|
391
|
+
updatedAt: new Date().toISOString(),
|
|
392
|
+
mutualId: `${byEntityId}-${entityId}`,
|
|
393
|
+
mutualUpdatedAt: new Date().toISOString(),
|
|
394
|
+
byEntityType,
|
|
395
|
+
byEntityId,
|
|
396
|
+
entityType,
|
|
397
|
+
entityId,
|
|
398
|
+
mutualData: opts.defaultMutualData,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const newMutualDataMap = new Map(store.mutual[selfKey]?.dataMap);
|
|
403
|
+
newMutualDataMap.set(mutual.entityId, mutual as Mutual<B, T>);
|
|
404
|
+
|
|
405
|
+
if (!hasRequestFailed) {
|
|
406
|
+
const entityState = store.entity[entityType];
|
|
407
|
+
const { dataMap: entityDataMap } = entityState;
|
|
408
|
+
|
|
409
|
+
const newEntityDataMap = new Map(entityDataMap);
|
|
410
|
+
newEntityDataMap.set(mutual.entityId, mutual as any);
|
|
411
|
+
|
|
412
|
+
monoriseStore.setState(
|
|
413
|
+
produce((state) => {
|
|
414
|
+
state.mutual[selfKey] = {
|
|
415
|
+
...(state.mutual[selfKey] || {}),
|
|
416
|
+
dataMap: newMutualDataMap,
|
|
417
|
+
};
|
|
418
|
+
state.entity[entityType].dataMap = newEntityDataMap;
|
|
419
|
+
}),
|
|
420
|
+
undefined,
|
|
421
|
+
`mr/mutual/get/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const createMutual = async <B extends Entity, T extends Entity>(
|
|
427
|
+
byEntityType: B,
|
|
428
|
+
entityType: T,
|
|
429
|
+
byEntityId: string,
|
|
430
|
+
entityId: string,
|
|
431
|
+
payload: MutualData<B, T> | Record<string, any> = {},
|
|
432
|
+
opts: CommonOptions = {},
|
|
433
|
+
) => {
|
|
434
|
+
const mutualService = makeMutualService(byEntityType, entityType);
|
|
435
|
+
const { data: mutual } = await mutualService.createMutual(
|
|
436
|
+
byEntityId,
|
|
437
|
+
entityId,
|
|
438
|
+
payload,
|
|
439
|
+
opts,
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
monoriseStore.setState(
|
|
443
|
+
produce((state) => {
|
|
444
|
+
const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
|
|
445
|
+
const side = `${entityType}/${entityId}/${byEntityType}`;
|
|
446
|
+
|
|
447
|
+
state.mutual[bySide].dataMap = new Map(
|
|
448
|
+
state.mutual[bySide]?.dataMap,
|
|
449
|
+
).set(mutual.entityId, mutual);
|
|
450
|
+
|
|
451
|
+
if (!state.mutual[side]) {
|
|
452
|
+
state.mutual[side] = {
|
|
453
|
+
dataMap: new Map(),
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
state.mutual[side].dataMap = new Map(state.mutual[side]?.dataMap).set(
|
|
458
|
+
mutual.byEntityId,
|
|
459
|
+
flipMutual(mutual),
|
|
460
|
+
);
|
|
461
|
+
}),
|
|
462
|
+
undefined,
|
|
463
|
+
`mr/mutual/create/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
464
|
+
);
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const updateLocalEntity = async <T extends Entity>(
|
|
468
|
+
entityType: Entity,
|
|
469
|
+
entityId: string,
|
|
470
|
+
data: Partial<DraftEntity<T>> = {},
|
|
471
|
+
) => {
|
|
472
|
+
const createdEntity = constructLocal(entityType, entityId, data);
|
|
473
|
+
|
|
474
|
+
monoriseStore.setState(
|
|
475
|
+
produce((state) => {
|
|
476
|
+
state.entity[entityType].dataMap.set(entityId, createdEntity);
|
|
477
|
+
}),
|
|
478
|
+
undefined,
|
|
479
|
+
`mr/entity/local-update/${entityType}/${entityId}`,
|
|
480
|
+
);
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const upsertLocalMutual = async <B extends Entity, T extends Entity>(
|
|
484
|
+
byEntityType: B,
|
|
485
|
+
entityType: T,
|
|
486
|
+
byEntityId: string,
|
|
487
|
+
entityId: string,
|
|
488
|
+
mutualData: MutualData<B, T>,
|
|
489
|
+
data: EntitySchemaMap[T] | Record<string, any> = {},
|
|
490
|
+
) => {
|
|
491
|
+
const mutual = constructMutual(
|
|
492
|
+
byEntityType,
|
|
493
|
+
byEntityId,
|
|
494
|
+
entityType,
|
|
495
|
+
entityId,
|
|
496
|
+
mutualData,
|
|
497
|
+
data as EntitySchemaMap[T],
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
monoriseStore.setState(
|
|
501
|
+
produce((state) => {
|
|
502
|
+
const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
|
|
503
|
+
const side = `${entityType}/${entityId}/${byEntityType}`;
|
|
504
|
+
|
|
505
|
+
state.mutual[bySide].dataMap = new Map(
|
|
506
|
+
state.mutual[bySide]?.dataMap,
|
|
507
|
+
).set(entityId, mutual);
|
|
508
|
+
|
|
509
|
+
if (!state.mutual[side]) {
|
|
510
|
+
state.mutual[side] = {
|
|
511
|
+
dataMap: new Map(),
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
state.mutual[side].dataMap = new Map(state.mutual[side]?.dataMap).set(
|
|
516
|
+
byEntityId,
|
|
517
|
+
flipMutual(mutual),
|
|
518
|
+
);
|
|
519
|
+
}),
|
|
520
|
+
undefined,
|
|
521
|
+
`mr/mutual/local-update/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
522
|
+
);
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const deleteMutual = async <B extends Entity, T extends Entity>(
|
|
526
|
+
byEntityType: B,
|
|
527
|
+
entityType: T,
|
|
528
|
+
byEntityId: string,
|
|
529
|
+
entityId = '',
|
|
530
|
+
opts: CommonOptions = {},
|
|
531
|
+
) => {
|
|
532
|
+
const mutualService = makeMutualService(byEntityType, entityType);
|
|
533
|
+
const { data } = await mutualService.deleteMutual(
|
|
534
|
+
byEntityId,
|
|
535
|
+
entityId,
|
|
536
|
+
opts,
|
|
537
|
+
);
|
|
538
|
+
|
|
539
|
+
monoriseStore.setState(
|
|
540
|
+
produce((state) => {
|
|
541
|
+
const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
|
|
542
|
+
const side = `${entityType}/${entityId}/${byEntityType}`;
|
|
543
|
+
|
|
544
|
+
state.mutual[bySide].dataMap.delete(data.entityId);
|
|
545
|
+
|
|
546
|
+
if (state.mutual[side]) {
|
|
547
|
+
state.mutual[side].dataMap.delete(data.byEntityId);
|
|
548
|
+
}
|
|
549
|
+
}),
|
|
550
|
+
undefined,
|
|
551
|
+
`mr/mutual/delete/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
552
|
+
);
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
const deleteLocalMutual = <B extends Entity, T extends Entity>(
|
|
556
|
+
byEntityType: B,
|
|
557
|
+
entityType: T,
|
|
558
|
+
byEntityId: string,
|
|
559
|
+
entityId: string,
|
|
560
|
+
) => {
|
|
561
|
+
monoriseStore.setState(
|
|
562
|
+
produce((state) => {
|
|
563
|
+
const bySide = `${byEntityType}/${byEntityId}/${entityType}`;
|
|
564
|
+
const side = `${entityType}/${entityId}/${byEntityType}`;
|
|
565
|
+
const bySideDataMap = new Map(state[bySide]?.dataMap);
|
|
566
|
+
const sideDataMap = new Map(state[side]?.dataMap);
|
|
567
|
+
bySideDataMap.delete(entityId);
|
|
568
|
+
sideDataMap.delete(byEntityId);
|
|
569
|
+
|
|
570
|
+
state.mutual[bySide].dataMap.delete(entityId);
|
|
571
|
+
state.mutual[side].dataMap.delete(byEntityId);
|
|
572
|
+
}),
|
|
573
|
+
undefined,
|
|
574
|
+
`mr/mutual/local-delete/${byEntityType}/${byEntityId}/${entityType}/${entityId}`,
|
|
575
|
+
);
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
const deleteLocalMutualsByEntity = async <T extends Entity>(
|
|
579
|
+
entityType: T,
|
|
580
|
+
id: string,
|
|
581
|
+
) => {
|
|
582
|
+
const entityConfig = (await getEntityConfig()) as Entity[];
|
|
583
|
+
const store = monoriseStore.getState();
|
|
584
|
+
let mutuals: Mutual[] = [];
|
|
585
|
+
for (const i of Object.keys(entityConfig)) {
|
|
586
|
+
const mutualState = store.mutual[`${entityType}/${id}/${i}`];
|
|
587
|
+
mutuals = [
|
|
588
|
+
...mutuals,
|
|
589
|
+
...Array.from(mutualState?.dataMap.values() || []),
|
|
590
|
+
];
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const updatedState = mutuals.reduce(
|
|
594
|
+
(acc, mutual) => {
|
|
595
|
+
const side = `${mutual.entityType}/${mutual.entityId}/${mutual.byEntityType}`;
|
|
596
|
+
const dataMap = new Map(store.mutual[side].dataMap);
|
|
597
|
+
dataMap.delete(id);
|
|
598
|
+
acc[side] = {
|
|
599
|
+
...store.mutual[side],
|
|
600
|
+
dataMap,
|
|
601
|
+
};
|
|
602
|
+
return acc;
|
|
603
|
+
},
|
|
604
|
+
{} as Record<string, CommonStore<Mutual>>,
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
monoriseStore.setState(
|
|
608
|
+
produce((state) => {
|
|
609
|
+
state.mutual = updatedState;
|
|
610
|
+
}),
|
|
611
|
+
undefined,
|
|
612
|
+
`mr/mutual/local-delete-by-entity/${entityType}/${id}`,
|
|
613
|
+
);
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
const useEntity = <T extends Entity>(
|
|
617
|
+
entityType: T,
|
|
618
|
+
id?: string,
|
|
619
|
+
opts: CommonOptions = {},
|
|
620
|
+
): {
|
|
621
|
+
entity: CreatedEntity<T> | undefined;
|
|
622
|
+
isLoading: boolean;
|
|
623
|
+
error?: ApplicationRequestError;
|
|
624
|
+
requestKey: string;
|
|
625
|
+
} => {
|
|
626
|
+
const dataMap = monoriseStore((state) => state.entity[entityType].dataMap);
|
|
627
|
+
const requestKey = `entity/${entityType}/get/${id}`;
|
|
628
|
+
const isLoading = useLoadStore(requestKey);
|
|
629
|
+
const error = useErrorStore(requestKey);
|
|
630
|
+
|
|
631
|
+
useEffect(() => {
|
|
632
|
+
if (id) {
|
|
633
|
+
getEntity(entityType, id, opts);
|
|
634
|
+
}
|
|
635
|
+
}, [id, entityType, opts]);
|
|
636
|
+
|
|
637
|
+
return {
|
|
638
|
+
entity: id ? dataMap.get(id) : undefined,
|
|
639
|
+
isLoading,
|
|
640
|
+
error,
|
|
641
|
+
requestKey,
|
|
642
|
+
};
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
const useEntities = <T extends Entity>(
|
|
646
|
+
entityType: T,
|
|
647
|
+
params: {
|
|
648
|
+
skRange?: {
|
|
649
|
+
start: string;
|
|
650
|
+
end: string;
|
|
651
|
+
};
|
|
652
|
+
all?: boolean;
|
|
653
|
+
} = {},
|
|
654
|
+
opts: CommonOptions = {},
|
|
655
|
+
): {
|
|
656
|
+
isLoading: boolean;
|
|
657
|
+
entities?: CreatedEntity<T>[];
|
|
658
|
+
entitiesMap: Map<string, CreatedEntity<T>>;
|
|
659
|
+
error?: ApplicationRequestError;
|
|
660
|
+
requestKey: string;
|
|
661
|
+
searchField: {
|
|
662
|
+
value: string;
|
|
663
|
+
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
|
664
|
+
};
|
|
665
|
+
} => {
|
|
666
|
+
const requestKey = `entity/${entityType}/list`;
|
|
667
|
+
const isListing = useLoadStore(requestKey);
|
|
668
|
+
const error = useErrorStore(requestKey);
|
|
669
|
+
const state = monoriseStore((state) => state.entity[entityType]);
|
|
670
|
+
const { dataMap, searchResults, isFirstFetched } = state;
|
|
671
|
+
const [entities, setEntities] = useState<CreatedEntity<T>[]>();
|
|
672
|
+
const [query, setQuery] = useState<string>('');
|
|
673
|
+
const [skRange, setBetween] = useState(params.skRange);
|
|
674
|
+
const [all, setAll] = useState(params.all);
|
|
675
|
+
const [isSearching, setIsSearching] = useState(false);
|
|
676
|
+
const isLoading = isListing || isSearching;
|
|
677
|
+
|
|
678
|
+
useEffect(() => {
|
|
679
|
+
if (
|
|
680
|
+
params?.skRange &&
|
|
681
|
+
skRange?.start !== params.skRange.start &&
|
|
682
|
+
skRange?.end !== params.skRange.end
|
|
683
|
+
) {
|
|
684
|
+
setBetween(params.skRange);
|
|
685
|
+
}
|
|
686
|
+
}, [skRange?.end, skRange?.start, params.skRange]);
|
|
687
|
+
|
|
688
|
+
useEffect(() => {
|
|
689
|
+
if (params?.all !== all) {
|
|
690
|
+
setAll(params.all);
|
|
691
|
+
}
|
|
692
|
+
}, [all, params.all]);
|
|
693
|
+
|
|
694
|
+
useEffect(() => {
|
|
695
|
+
if (!isFirstFetched) {
|
|
696
|
+
listEntities(entityType, { skRange, all }, opts);
|
|
697
|
+
}
|
|
698
|
+
}, [all, entityType, skRange, opts, isFirstFetched]);
|
|
699
|
+
|
|
700
|
+
useEffect(() => {
|
|
701
|
+
let queryTimeout: NodeJS.Timeout;
|
|
702
|
+
|
|
703
|
+
if (query?.length) {
|
|
704
|
+
setIsSearching(true);
|
|
705
|
+
queryTimeout = setTimeout(async () => {
|
|
706
|
+
await searchEntities(entityType, query);
|
|
707
|
+
setIsSearching(false);
|
|
708
|
+
}, 700);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
return () => queryTimeout && clearTimeout(queryTimeout);
|
|
712
|
+
}, [entityType, query]);
|
|
713
|
+
|
|
714
|
+
useEffect(() => {
|
|
715
|
+
if (!query && dataMap.size !== entities?.length) {
|
|
716
|
+
setIsSearching(false);
|
|
717
|
+
setEntities(
|
|
718
|
+
Array.from(dataMap.values()).sort(byEntityId) as CreatedEntity<T>[],
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
if (query) {
|
|
723
|
+
setEntities(searchResults as CreatedEntity<T>[]);
|
|
724
|
+
}
|
|
725
|
+
}, [
|
|
726
|
+
dataMap,
|
|
727
|
+
dataMap.size,
|
|
728
|
+
entities?.length,
|
|
729
|
+
query,
|
|
730
|
+
searchResults,
|
|
731
|
+
searchResults?.length,
|
|
732
|
+
]);
|
|
733
|
+
|
|
734
|
+
const handleQueryChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
735
|
+
setQuery(event.target.value);
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
const searchField = {
|
|
739
|
+
value: query,
|
|
740
|
+
onChange: handleQueryChange,
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
entities,
|
|
745
|
+
entitiesMap: dataMap,
|
|
746
|
+
searchField,
|
|
747
|
+
isLoading,
|
|
748
|
+
error,
|
|
749
|
+
requestKey,
|
|
750
|
+
};
|
|
751
|
+
};
|
|
752
|
+
|
|
753
|
+
const useMutual = <B extends Entity, T extends Entity>(
|
|
754
|
+
byEntityType: B,
|
|
755
|
+
entityType: T,
|
|
756
|
+
byId: string | null,
|
|
757
|
+
id: string,
|
|
758
|
+
opts: CommonOptions & {
|
|
759
|
+
// if failed to retrieve, would still set a default mutual data
|
|
760
|
+
defaultMutualData?: Record<string, any>;
|
|
761
|
+
} = {},
|
|
762
|
+
): {
|
|
763
|
+
mutual?: Mutual<B, T>;
|
|
764
|
+
isLoading: boolean;
|
|
765
|
+
error?: ApplicationRequestError;
|
|
766
|
+
requestKey: string;
|
|
767
|
+
} => {
|
|
768
|
+
const state = monoriseStore(
|
|
769
|
+
(state) => state.mutual[`${byEntityType}/${byId}/${entityType}`],
|
|
770
|
+
);
|
|
771
|
+
const requestKey = `mutual/${byEntityType}/${byId}/${entityType}/${id}/get`;
|
|
772
|
+
const isLoading = useLoadStore(requestKey);
|
|
773
|
+
const error = useErrorStore(requestKey);
|
|
774
|
+
|
|
775
|
+
const { dataMap } = state || {
|
|
776
|
+
dataMap: new Map(),
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
useEffect(() => {
|
|
780
|
+
if (!dataMap.get(id)) {
|
|
781
|
+
getMutual(byEntityType, entityType, byId, id, opts);
|
|
782
|
+
}
|
|
783
|
+
}, [byEntityType, byId, entityType, id, opts, dataMap]);
|
|
784
|
+
|
|
785
|
+
return {
|
|
786
|
+
mutual: dataMap.get(id) as Mutual<B, T>,
|
|
787
|
+
isLoading,
|
|
788
|
+
error,
|
|
789
|
+
requestKey,
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const useMutuals = <B extends Entity, T extends Entity>(
|
|
794
|
+
byEntityType: B,
|
|
795
|
+
entityType: T,
|
|
796
|
+
byId?: string,
|
|
797
|
+
opts: CommonOptions = {},
|
|
798
|
+
chainEntityQuery?: string, // chain entity query, refer to list-entities-by-entity.controller.ts
|
|
799
|
+
): {
|
|
800
|
+
mutuals: Mutual<B, T>[];
|
|
801
|
+
mutualsMap: Map<string, Mutual<B, T>>;
|
|
802
|
+
isLoading: boolean;
|
|
803
|
+
requestKey: string;
|
|
804
|
+
error?: ApplicationRequestError;
|
|
805
|
+
} => {
|
|
806
|
+
const state = monoriseStore(
|
|
807
|
+
(state) => state.mutual[`${byEntityType}/${byId}/${entityType}`],
|
|
808
|
+
);
|
|
809
|
+
const { dataMap, isFirstFetched } = state || {
|
|
810
|
+
dataMap: new Map(),
|
|
811
|
+
};
|
|
812
|
+
const [mutuals, setMutuals] = useState<Mutual<B, T>[]>([]);
|
|
813
|
+
const requestKey = `mutual/${byEntityType}/${entityType}/${byId}/list${chainEntityQuery ? `?${chainEntityQuery}` : ''}`;
|
|
814
|
+
const isLoading = useLoadStore(requestKey);
|
|
815
|
+
const error = useErrorStore(requestKey);
|
|
816
|
+
|
|
817
|
+
useEffect(() => {
|
|
818
|
+
if (!isFirstFetched && byEntityType && entityType && byId) {
|
|
819
|
+
listEntitiesByEntity(
|
|
820
|
+
byEntityType,
|
|
821
|
+
entityType,
|
|
822
|
+
byId,
|
|
823
|
+
opts,
|
|
824
|
+
chainEntityQuery,
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
}, [
|
|
828
|
+
isFirstFetched,
|
|
829
|
+
byEntityType,
|
|
830
|
+
byId,
|
|
831
|
+
entityType,
|
|
832
|
+
opts,
|
|
833
|
+
chainEntityQuery,
|
|
834
|
+
]);
|
|
835
|
+
|
|
836
|
+
useEffect(() => {
|
|
837
|
+
if (dataMap.size !== mutuals?.length) {
|
|
838
|
+
setMutuals(Array.from(dataMap.values()) as Mutual<B, T>[]);
|
|
839
|
+
}
|
|
840
|
+
}, [dataMap, dataMap.size, mutuals?.length]);
|
|
841
|
+
|
|
842
|
+
return {
|
|
843
|
+
mutuals,
|
|
844
|
+
mutualsMap: dataMap as Map<string, Mutual<B, T>>,
|
|
845
|
+
isLoading,
|
|
846
|
+
requestKey,
|
|
847
|
+
error,
|
|
848
|
+
};
|
|
849
|
+
};
|
|
850
|
+
|
|
851
|
+
const useEntityState = <T extends Entity>(entityType: T) => {
|
|
852
|
+
return monoriseStore((state) => state.entity[entityType]);
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
return {
|
|
856
|
+
listMoreEntities,
|
|
857
|
+
createEntity,
|
|
858
|
+
upsertEntity,
|
|
859
|
+
editEntity,
|
|
860
|
+
deleteEntity,
|
|
861
|
+
getMutual,
|
|
862
|
+
updateLocalEntity,
|
|
863
|
+
createMutual,
|
|
864
|
+
upsertLocalMutual,
|
|
865
|
+
deleteMutual,
|
|
866
|
+
deleteLocalMutual,
|
|
867
|
+
useEntity,
|
|
868
|
+
useEntities,
|
|
869
|
+
useMutual,
|
|
870
|
+
useMutuals,
|
|
871
|
+
listEntitiesByTag,
|
|
872
|
+
useEntityState,
|
|
873
|
+
};
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
export { initCoreActions };
|
|
877
|
+
|
|
878
|
+
export type CoreActions = ReturnType<typeof initCoreActions>;
|