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