@flowerforce/flower-core 3.1.2-beta.0 → 3.1.2-beta.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/index.cjs.js +748 -851
- package/dist/index.esm.js +748 -851
- package/dist/src/interfaces/ReducerInterface.d.ts +5 -0
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
@@ -27,254 +27,246 @@ const EMPTY_STRING_REGEXP = /^\s*$/;
|
|
27
27
|
* Defines a utility object named rulesMatcherUtils, which contains various helper functions used for processing rules and data in a rule-matching context.
|
28
28
|
*/
|
29
29
|
const rulesMatcherUtils = {
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
// functions are non empty
|
145
|
-
if (rulesMatcherUtils.isFunction(value)) {
|
146
|
-
return false;
|
147
|
-
}
|
148
|
-
/* if (isBool(value)) {
|
149
|
-
return false;
|
150
|
-
}
|
151
|
-
*/
|
152
|
-
// Whitespace only strings are empty
|
153
|
-
if (rulesMatcherUtils.isString(value)) {
|
154
|
-
return EMPTY_STRING_REGEXP.test(value);
|
155
|
-
}
|
156
|
-
// For arrays we use the length property
|
157
|
-
if (Array.isArray(value)) {
|
158
|
-
return value.length === 0;
|
159
|
-
}
|
160
|
-
// Dates have no attributes but aren't empty
|
161
|
-
if (rulesMatcherUtils.isDate(value)) {
|
162
|
-
return false;
|
163
|
-
}
|
164
|
-
// If we find at least one property we consider it non empty
|
165
|
-
let attr;
|
166
|
-
if (rulesMatcherUtils.isObject(value)) {
|
167
|
-
for (attr in value) {
|
30
|
+
isNumber: (el) => {
|
31
|
+
const num = String(el);
|
32
|
+
return !!num.match(/(^-?|^\d+\.)\d+$/);
|
33
|
+
},
|
34
|
+
rule: (val, data, options) => {
|
35
|
+
const { prefix } = options || {};
|
36
|
+
const path = Object.keys(val)[0];
|
37
|
+
const valueBlock = val;
|
38
|
+
const pathWithPrefix = rulesMatcherUtils.getPath(path, prefix);
|
39
|
+
const valueForKey = _get(data, pathWithPrefix, undefined);
|
40
|
+
const { name } = _get(valueBlock, [path], {}) || {};
|
41
|
+
const { op, value, opt } = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
42
|
+
const valueRef = value && String(value).indexOf('$ref:') === 0
|
43
|
+
? _get(data, rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix), undefined)
|
44
|
+
: value;
|
45
|
+
if (!operators[op]) {
|
46
|
+
throw new Error(`Error missing operator:${op}`);
|
47
|
+
}
|
48
|
+
const valid = operators[op] && operators[op](valueForKey, valueRef, opt, data);
|
49
|
+
return { valid, name: `${pathWithPrefix}___${name || op}` };
|
50
|
+
},
|
51
|
+
getKey: (block, keys, options) => {
|
52
|
+
if (Object.prototype.hasOwnProperty.call(block, '$and')) {
|
53
|
+
return block.$and.map((item) => rulesMatcherUtils.getKey(item, keys, options));
|
54
|
+
}
|
55
|
+
if (Object.prototype.hasOwnProperty.call(block, '$or')) {
|
56
|
+
return block.$or.map((item) => rulesMatcherUtils.getKey(item, keys, options));
|
57
|
+
}
|
58
|
+
const { prefix } = options || {};
|
59
|
+
const path = Object.keys(block)[0];
|
60
|
+
const valueBlock = block;
|
61
|
+
const res = rulesMatcherUtils.getPath(path, prefix);
|
62
|
+
const { value } = rulesMatcherUtils.getDefaultRule(valueBlock[path]);
|
63
|
+
if (value && String(value).indexOf('$ref:') === 0) {
|
64
|
+
keys[rulesMatcherUtils.getPath(value.replace('$ref:', ''), prefix)] = true;
|
65
|
+
}
|
66
|
+
return (keys[res] = true);
|
67
|
+
},
|
68
|
+
isDate: (v) => v instanceof Date,
|
69
|
+
isDefined: (v) => v !== null && v !== undefined,
|
70
|
+
isObject: (v) => v === Object(v),
|
71
|
+
isFunction: (v) => typeof v === 'function',
|
72
|
+
isString: (v) => typeof v === 'string',
|
73
|
+
getDefaultStringValue: (value) => {
|
74
|
+
switch (value) {
|
75
|
+
case '$required':
|
76
|
+
return { op: '$exists', value: true };
|
77
|
+
case '$exists':
|
78
|
+
return { op: '$exists', value: true };
|
79
|
+
default:
|
80
|
+
return { op: '$eq', value };
|
81
|
+
}
|
82
|
+
},
|
83
|
+
getTypeOf: (value) => Array.isArray(value)
|
84
|
+
? 'array'
|
85
|
+
: rulesMatcherUtils.isNumber(value)
|
86
|
+
? 'number'
|
87
|
+
: value === null
|
88
|
+
? null
|
89
|
+
: typeof value,
|
90
|
+
getDefaultRule: (value) => {
|
91
|
+
const valueType = rulesMatcherUtils.getTypeOf(value);
|
92
|
+
switch (valueType) {
|
93
|
+
case 'number':
|
94
|
+
return { op: '$eq', value };
|
95
|
+
case 'string':
|
96
|
+
return rulesMatcherUtils.getDefaultStringValue(value);
|
97
|
+
case 'boolean':
|
98
|
+
return { op: '$exists', value };
|
99
|
+
case 'array':
|
100
|
+
return { op: '$in', value };
|
101
|
+
case 'object':
|
102
|
+
return {
|
103
|
+
...value,
|
104
|
+
op: value.op || Object.keys(value)[0],
|
105
|
+
value: value.value || value[Object.keys(value)[0]]
|
106
|
+
};
|
107
|
+
default:
|
108
|
+
return { op: '$eq', value };
|
109
|
+
}
|
110
|
+
},
|
111
|
+
isEmpty: (value) => {
|
112
|
+
// Null and undefined are empty
|
113
|
+
if (!rulesMatcherUtils.isDefined(value)) {
|
114
|
+
return true;
|
115
|
+
}
|
116
|
+
// functions are non empty
|
117
|
+
if (rulesMatcherUtils.isFunction(value)) {
|
118
|
+
return false;
|
119
|
+
}
|
120
|
+
/* if (isBool(value)) {
|
121
|
+
return false;
|
122
|
+
}
|
123
|
+
*/
|
124
|
+
// Whitespace only strings are empty
|
125
|
+
if (rulesMatcherUtils.isString(value)) {
|
126
|
+
return EMPTY_STRING_REGEXP.test(value);
|
127
|
+
}
|
128
|
+
// For arrays we use the length property
|
129
|
+
if (Array.isArray(value)) {
|
130
|
+
return value.length === 0;
|
131
|
+
}
|
132
|
+
// Dates have no attributes but aren't empty
|
133
|
+
if (rulesMatcherUtils.isDate(value)) {
|
134
|
+
return false;
|
135
|
+
}
|
136
|
+
// If we find at least one property we consider it non empty
|
137
|
+
let attr;
|
138
|
+
if (rulesMatcherUtils.isObject(value)) {
|
139
|
+
for (attr in value) {
|
140
|
+
return false;
|
141
|
+
}
|
142
|
+
return true;
|
143
|
+
}
|
168
144
|
return false;
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
145
|
+
},
|
146
|
+
forceArray: (a) => (Array.isArray(a) ? a : [a]),
|
147
|
+
getPath: (path, prefix) => {
|
148
|
+
if (path.indexOf('^') === 0) {
|
149
|
+
return _trimStart(path, '^');
|
150
|
+
}
|
151
|
+
// da verificare se è ancora utilizzato
|
152
|
+
if (path.indexOf('$') === 0) {
|
153
|
+
return path;
|
154
|
+
}
|
155
|
+
return prefix ? `${prefix}.${path}` : path;
|
156
|
+
},
|
157
|
+
// TODO BUG NUMERI CON LETTERE 1asdas o solo
|
158
|
+
forceNumber: (el) => {
|
159
|
+
if (Array.isArray(el)) {
|
160
|
+
return el.length;
|
161
|
+
}
|
162
|
+
if (rulesMatcherUtils.isNumber(String(el))) {
|
163
|
+
return parseFloat(String(el));
|
164
|
+
}
|
165
|
+
// fix perchè un valore false < 1 è true, quindi sbagliato, mentre un valore undefined < 1 è false
|
166
|
+
return 0;
|
167
|
+
},
|
168
|
+
checkRule: (block, data, options) => {
|
169
|
+
if (!Array.isArray(block) &&
|
170
|
+
block &&
|
171
|
+
Object.prototype.hasOwnProperty.call(block, '$and')) {
|
172
|
+
if (block && block['$and'] && !block['$and'].length)
|
173
|
+
return true;
|
174
|
+
return block['$and'].every((item) => rulesMatcherUtils.checkRule(item, data, options));
|
175
|
+
}
|
176
|
+
if (!Array.isArray(block) &&
|
177
|
+
block &&
|
178
|
+
Object.prototype.hasOwnProperty.call(block, '$or')) {
|
179
|
+
if (block && block['$or'] && !block['$or'].length)
|
180
|
+
return true;
|
181
|
+
return block['$or'].some((item) => rulesMatcherUtils.checkRule(item, data, options));
|
182
|
+
}
|
183
|
+
const res = rulesMatcherUtils.rule(block, data, options);
|
184
|
+
return res.valid;
|
185
|
+
},
|
186
|
+
getKeys: (rules, options) => {
|
187
|
+
if (!rules)
|
188
|
+
return null;
|
189
|
+
if (typeof rules == 'function')
|
190
|
+
return [];
|
191
|
+
if (!rulesMatcherUtils
|
192
|
+
.forceArray(rules)
|
193
|
+
.every((r) => rulesMatcherUtils.isObject(r)))
|
194
|
+
return null;
|
195
|
+
const keys = {};
|
196
|
+
const conditions = Array.isArray(rules) ? { $and: rules } : rules;
|
197
|
+
rulesMatcherUtils.getKey(conditions, keys, options ?? {});
|
198
|
+
return Object.keys(keys);
|
204
199
|
}
|
205
|
-
const res = rulesMatcherUtils.rule(block, data, options);
|
206
|
-
return res.valid;
|
207
|
-
},
|
208
|
-
getKeys: (rules, options) => {
|
209
|
-
if (!rules) return null;
|
210
|
-
if (typeof rules == 'function') return [];
|
211
|
-
if (!rulesMatcherUtils.forceArray(rules).every(r => rulesMatcherUtils.isObject(r))) return null;
|
212
|
-
const keys = {};
|
213
|
-
const conditions = Array.isArray(rules) ? {
|
214
|
-
$and: rules
|
215
|
-
} : rules;
|
216
|
-
rulesMatcherUtils.getKey(conditions, keys, options ?? {});
|
217
|
-
return Object.keys(keys);
|
218
|
-
}
|
219
200
|
};
|
220
201
|
/**
|
221
202
|
* Defines a set of comparison operators used for matching rules against user input.
|
222
203
|
*/
|
223
204
|
const operators = {
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
205
|
+
$exists: (a, b) => !rulesMatcherUtils.isEmpty(a) === b,
|
206
|
+
$eq: (a, b) => a === b,
|
207
|
+
$ne: (a, b) => a !== b,
|
208
|
+
$gt: (a, b) => rulesMatcherUtils.forceNumber(a) > parseFloat(b),
|
209
|
+
$gte: (a, b) => rulesMatcherUtils.forceNumber(a) >= parseFloat(b),
|
210
|
+
$lt: (a, b) => rulesMatcherUtils.forceNumber(a) < parseFloat(b),
|
211
|
+
$lte: (a, b) => rulesMatcherUtils.forceNumber(a) <= parseFloat(b),
|
212
|
+
$strGt: (a, b) => String(a || '').length > parseFloat(b),
|
213
|
+
$strGte: (a, b) => String(a || '').length >= parseFloat(b),
|
214
|
+
$strLt: (a, b) => String(a || '').length < parseFloat(b),
|
215
|
+
$strLte: (a, b) => String(a || '').length <= parseFloat(b),
|
216
|
+
$in: (a, b) => rulesMatcherUtils
|
217
|
+
.forceArray(b)
|
218
|
+
.some((c) => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
219
|
+
$nin: (a, b) => !rulesMatcherUtils
|
220
|
+
.forceArray(b)
|
221
|
+
.some((c) => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
222
|
+
$all: (a, b) => rulesMatcherUtils
|
223
|
+
.forceArray(b)
|
224
|
+
.every((c) => _intersection(rulesMatcherUtils.forceArray(a), rulesMatcherUtils.forceArray(c)).length),
|
225
|
+
$regex: (a, b, opt) => rulesMatcherUtils
|
226
|
+
.forceArray(b)
|
227
|
+
.some((c) => c instanceof RegExp ? c.test(a) : new RegExp(c, opt).test(a))
|
239
228
|
};
|
240
229
|
|
241
230
|
const rulesMatcher = (rules, formValue = {}, apply = true, options) => {
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
231
|
+
if (!rules)
|
232
|
+
return [apply];
|
233
|
+
// if (typeof rules !== 'object' && !Array.isArray(rules)) {
|
234
|
+
// throw new Error('Rules accept only array or object');
|
235
|
+
// }
|
236
|
+
if (typeof rules === 'function') {
|
237
|
+
return [rules(formValue) === apply];
|
238
|
+
}
|
239
|
+
const conditions = Array.isArray(rules)
|
240
|
+
? { $and: rules }
|
241
|
+
: rules;
|
242
|
+
const valid = rulesMatcherUtils.checkRule(conditions, formValue, options ?? {});
|
243
|
+
return [valid === apply];
|
254
244
|
};
|
255
245
|
const MatchRules = {
|
256
|
-
|
257
|
-
|
246
|
+
rulesMatcher,
|
247
|
+
utils: rulesMatcherUtils
|
258
248
|
};
|
259
249
|
|
260
250
|
/* eslint-disable no-useless-escape */
|
261
251
|
// TODO align this set of functions to selectors and reducers functions
|
262
|
-
const flattenRules = ob => {
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
252
|
+
const flattenRules = (ob) => {
|
253
|
+
const result = {};
|
254
|
+
for (const i in ob) {
|
255
|
+
if (ob[i] === null) {
|
256
|
+
result[i] = null;
|
257
|
+
}
|
258
|
+
if ((typeof ob[i] === 'object' && !Array.isArray(ob[i])) ||
|
259
|
+
(Array.isArray(ob[i]) && typeof ob[i][0] === 'object')) {
|
260
|
+
const temp = flattenRules(ob[i]);
|
261
|
+
for (const j in temp) {
|
262
|
+
result[i + '*' + j] = temp[j];
|
263
|
+
}
|
264
|
+
}
|
265
|
+
else {
|
266
|
+
result[i] = ob[i];
|
267
|
+
}
|
275
268
|
}
|
276
|
-
|
277
|
-
return result;
|
269
|
+
return result;
|
278
270
|
};
|
279
271
|
// export const searchEmptyKeyRecursively = <T extends object>(
|
280
272
|
// _key: "$and" | "$or",
|
@@ -297,661 +289,566 @@ const flattenRules = ob => {
|
|
297
289
|
// .map((key) => searchEmptyKeyRecursively(_key, obj[key]))
|
298
290
|
// .every((v) => v === true);
|
299
291
|
// };
|
300
|
-
const getRulesExists = rules => {
|
301
|
-
|
292
|
+
const getRulesExists = (rules) => {
|
293
|
+
return Object.keys(rules).length ? CoreUtils.mapEdge(rules) : undefined;
|
302
294
|
};
|
303
295
|
/**
|
304
296
|
* Defines a collection of utility functions for processing rules, nodes and graph-like structures
|
305
297
|
*/
|
306
298
|
const CoreUtils = {
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
299
|
+
generateRulesName: (nextRules) => {
|
300
|
+
const a = nextRules.reduce((acc, inc) => {
|
301
|
+
const n = typeof inc.rules === 'string'
|
302
|
+
? inc.rules || '__ERROR_NAME__'
|
303
|
+
: inc.rules?.name || '__ERROR_NAME__';
|
304
|
+
return {
|
305
|
+
...acc,
|
306
|
+
[n]: inc.nodeId
|
307
|
+
};
|
308
|
+
}, {});
|
309
|
+
return a;
|
310
|
+
},
|
311
|
+
mapKeysDeepLodash: (obj, cb, isRecursive) => {
|
312
|
+
/* istanbul ignore next */
|
313
|
+
if (!obj && !isRecursive) {
|
314
|
+
return {};
|
315
|
+
}
|
316
|
+
if (!isRecursive) {
|
317
|
+
/* istanbul ignore next */
|
318
|
+
if (typeof obj === 'string' ||
|
319
|
+
typeof obj === 'number' ||
|
320
|
+
typeof obj === 'boolean') {
|
321
|
+
return {};
|
322
|
+
}
|
323
|
+
}
|
324
|
+
if (Array.isArray(obj)) {
|
325
|
+
return obj.map((item) => CoreUtils.mapKeysDeepLodash(item, cb, true));
|
326
|
+
}
|
327
|
+
if (!isPlainObject(obj)) {
|
328
|
+
return obj;
|
329
|
+
}
|
330
|
+
const result = mapKeys(obj, cb);
|
331
|
+
return mapValues(result, (value) => CoreUtils.mapKeysDeepLodash(value, cb, true));
|
332
|
+
},
|
333
|
+
generateNodes: (nodes) => keyBy(nodes.map((s) => omit(s, 'nextRules')), 'nodeId'),
|
334
|
+
makeObjectRules: (nodes) => nodes.reduce((acc, inc) => ({ ...acc, [inc.nodeId]: inc.nextRules }), {}),
|
335
|
+
hasNode: (state, name, node) => has(state, [name, 'nodes', node]),
|
336
|
+
isEmptyRules: (rules) => {
|
337
|
+
if (isEmpty(rules))
|
338
|
+
return true;
|
339
|
+
if (isEmpty(_get(rules, 'rules')))
|
340
|
+
return true;
|
341
|
+
if (Object.keys(flattenRules(rules)).every((key) => key.endsWith('$and') || key.endsWith('$or'))) {
|
342
|
+
return true;
|
343
|
+
}
|
344
|
+
return false;
|
345
|
+
},
|
346
|
+
mapEdge: (nextNode) => {
|
347
|
+
const res = nextNode.sort((a, b) => {
|
348
|
+
const x = CoreUtils.isEmptyRules(a.rules);
|
349
|
+
const y = CoreUtils.isEmptyRules(b.rules);
|
350
|
+
return Number(x) - Number(y);
|
351
|
+
});
|
352
|
+
return res;
|
353
|
+
},
|
354
|
+
makeRules: (rules) => Object.entries(rules).reduce((acc2, [k, v]) => [...acc2, { nodeId: k, rules: v }], []),
|
355
|
+
generateNodesForFlowerJson: (nodes) => nodes
|
356
|
+
.filter((e) => !!_get(e, 'props.id'))
|
357
|
+
.map((e) => {
|
358
|
+
const rules = CoreUtils.makeRules(e.props.to ?? {});
|
359
|
+
const nextRules = getRulesExists(rules);
|
360
|
+
const children = e.props.data?.children;
|
361
|
+
return {
|
362
|
+
nodeId: e.props.id,
|
363
|
+
nodeType: e.type.displayName || e.props.as || 'FlowerNode',
|
364
|
+
nodeTitle: _get(e.props, 'data.title'),
|
365
|
+
children,
|
366
|
+
nextRules,
|
367
|
+
retain: e.props.retain,
|
368
|
+
disabled: e.props.disabled
|
369
|
+
};
|
370
|
+
}),
|
371
|
+
cleanPath: (name, char = '^') => _trimStart(name, char),
|
372
|
+
getPath: (idValue) => {
|
373
|
+
if (!idValue) {
|
374
|
+
return {
|
375
|
+
path: []
|
376
|
+
};
|
377
|
+
}
|
378
|
+
if (idValue === '*') {
|
379
|
+
return {
|
380
|
+
path: '*'
|
381
|
+
};
|
382
|
+
}
|
383
|
+
if (idValue.indexOf('^') === 0) {
|
384
|
+
const [flowNameFromPath, ...rest] = CoreUtils.cleanPath(idValue).split('.');
|
385
|
+
return {
|
386
|
+
flowNameFromPath,
|
387
|
+
path: rest
|
388
|
+
};
|
389
|
+
}
|
390
|
+
return {
|
391
|
+
path: idValue.split('.')
|
392
|
+
};
|
393
|
+
},
|
394
|
+
allEqual: (arr, arr2) => arr.length === arr2.length && arr.every((v) => arr2.includes(v)),
|
395
|
+
findValidRule: (nextRules, value, prefix) => find(nextRules, (rule) => {
|
396
|
+
// fix per evitare di entrare in un nodo senza regole, ma con un name,
|
397
|
+
// invocando un next() senza paramentri
|
398
|
+
if (typeof rule.rules === 'string') {
|
399
|
+
return false;
|
400
|
+
}
|
401
|
+
if (typeof rule.rules === 'function') {
|
402
|
+
return rule.rules(value);
|
403
|
+
}
|
404
|
+
if (rule.rules === null) {
|
405
|
+
return true;
|
406
|
+
}
|
407
|
+
if (typeof rule.rules?.rules === 'undefined') {
|
408
|
+
return false;
|
409
|
+
}
|
410
|
+
if (typeof rule.rules.rules === 'string') {
|
411
|
+
return false;
|
412
|
+
}
|
413
|
+
const [valid] = MatchRules.rulesMatcher(rule.rules.rules, value, true, {
|
414
|
+
prefix
|
415
|
+
});
|
416
|
+
return valid;
|
417
|
+
})
|
424
418
|
};
|
425
419
|
|
426
420
|
const FlowerStateUtils = {
|
427
|
-
|
428
|
-
|
429
|
-
[
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
421
|
+
getAllData: (state) => state &&
|
422
|
+
Object.entries(state ?? {}).reduce((acc, [k, v]) => ({ ...acc, [k]: v.data }), {}),
|
423
|
+
selectFlowerFormNode: (name, id) => (state) => _get(state, [name, 'form', id]),
|
424
|
+
makeSelectCurrentNextRules: (name) => (state) => {
|
425
|
+
const nextRules = _get(state, [name, 'nextRules']);
|
426
|
+
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(name)(state);
|
427
|
+
return _get(nextRules, [currentNodeId]);
|
428
|
+
},
|
429
|
+
makeSelectCurrentNodeId: (name) => (state) => {
|
430
|
+
const subState = _get(state, [name]);
|
431
|
+
const startId = _get(state, ['startId']);
|
432
|
+
return _get(subState, ['current']) || startId;
|
433
|
+
},
|
434
|
+
makeSelectNodeErrors: (name, currentNodeId) => (state) => {
|
435
|
+
const form = FlowerStateUtils.selectFlowerFormNode(name, currentNodeId)(state);
|
436
|
+
return {
|
437
|
+
touched: form?.touched || false,
|
438
|
+
errors: form?.errors,
|
439
|
+
isValidating: form?.isValidating,
|
440
|
+
isValid: form && form.errors
|
441
|
+
? Object.values(form.errors).flat().length === 0
|
442
|
+
: true
|
443
|
+
};
|
444
|
+
}
|
451
445
|
};
|
452
446
|
|
453
|
-
const {
|
454
|
-
generateNodes,
|
455
|
-
hasNode,
|
456
|
-
makeObjectRules,
|
457
|
-
generateRulesName,
|
458
|
-
findValidRule
|
459
|
-
} = CoreUtils;
|
447
|
+
const { generateNodes, hasNode, makeObjectRules, generateRulesName, findValidRule } = CoreUtils;
|
460
448
|
/**
|
461
449
|
* These functions are Redux reducers used in a Flux architecture for managing state transitions and updates in a Flower application.
|
462
450
|
*/
|
463
451
|
const FlowerCoreReducers = {
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
}
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
}) => {
|
500
|
-
const {
|
501
|
-
name,
|
502
|
-
flowName,
|
503
|
-
history
|
504
|
-
} = payload;
|
505
|
-
const key = name || flowName || '' || '';
|
506
|
-
const resultHistory = history.slice(1, -1);
|
507
|
-
state[key].history.push(...resultHistory);
|
508
|
-
return state;
|
509
|
-
},
|
510
|
-
historyPop: (state, {
|
511
|
-
payload
|
512
|
-
}) => {
|
513
|
-
const container = _get(state, [payload.name]);
|
514
|
-
const startId = _get(container, ['startId']);
|
515
|
-
const history = _get(container, ['history']);
|
516
|
-
const originHistory = [..._get(container, ['history'], [])];
|
517
|
-
if (originHistory.length < 2) {
|
518
|
-
_set(state, [payload.name, 'current'], startId);
|
519
|
-
_set(state, [payload.name, 'history'], [startId]);
|
520
|
-
return state;
|
521
|
-
}
|
522
|
-
// elimino lo node corrente
|
523
|
-
history.pop();
|
524
|
-
const total = originHistory.length - 2;
|
525
|
-
// eslint-disable-next-line no-plusplus
|
526
|
-
for (let index = total; index > 0; index--) {
|
527
|
-
const curr = originHistory[index];
|
528
|
-
const nodeType = _get(container, ['nodes', curr, 'nodeType']);
|
529
|
-
const nodeDisabled = _get(container, ['nodes', curr, 'disabled']);
|
530
|
-
if (nodeDisabled || nodeType === 'FlowerAction' || nodeType === 'FlowerServer' || !nodeType) {
|
531
|
-
history.pop();
|
532
|
-
} else {
|
533
|
-
break;
|
534
|
-
}
|
535
|
-
}
|
536
|
-
const lastId = _last(history);
|
537
|
-
_set(state, [payload.name, 'current'], lastId);
|
538
|
-
return state;
|
539
|
-
},
|
540
|
-
restoreHistory: (state, {
|
541
|
-
payload
|
542
|
-
}) => {
|
543
|
-
const startId = _get(state, [payload.name, 'startId']);
|
544
|
-
_set(state, [payload.name, 'current'], startId);
|
545
|
-
_set(state, [payload.name, 'history'], [startId]);
|
546
|
-
return state;
|
547
|
-
},
|
548
|
-
replaceNode: (state, {
|
549
|
-
payload
|
550
|
-
}) => {
|
551
|
-
const {
|
552
|
-
name,
|
553
|
-
flowName,
|
554
|
-
node
|
555
|
-
} = payload;
|
556
|
-
// non ancora implementanto nell'hook useFlower
|
557
|
-
/* istanbul ignore next */
|
558
|
-
if (hasNode(state, name || flowName || '', node)) {
|
559
|
-
_set(state, [name || flowName || '', 'current'], node);
|
560
|
-
_set(state, [name || flowName || '', 'history'], [node]);
|
561
|
-
}
|
562
|
-
/* istanbul ignore next */
|
563
|
-
return state;
|
564
|
-
},
|
565
|
-
/* istanbul ignore next */
|
566
|
-
initializeFromNode: (state, {
|
567
|
-
payload
|
568
|
-
}) => {
|
569
|
-
const {
|
570
|
-
name,
|
571
|
-
flowName,
|
572
|
-
node
|
573
|
-
} = payload;
|
574
|
-
if (hasNode(state, name || flowName || '', node)) {
|
575
|
-
_set(state, [name || flowName || '', 'startId'], node);
|
576
|
-
_set(state, [name || flowName || '', 'current'], node);
|
577
|
-
_set(state, [name || flowName || '', 'history'], [node]);
|
578
|
-
}
|
579
|
-
return state;
|
580
|
-
},
|
581
|
-
/* istanbul ignore next */
|
582
|
-
forceResetHistory: (state, {
|
583
|
-
payload
|
584
|
-
}) => {
|
585
|
-
const {
|
586
|
-
name,
|
587
|
-
flowName
|
588
|
-
} = payload;
|
589
|
-
if (!name && !flowName) return state;
|
590
|
-
_set(state, [name || flowName || '', 'history'], []);
|
591
|
-
return state;
|
592
|
-
},
|
593
|
-
destroy: (state, {
|
594
|
-
payload
|
595
|
-
}) => {
|
596
|
-
_set(state, [payload.name], {});
|
597
|
-
},
|
598
|
-
initNodes: (state, {
|
599
|
-
payload
|
600
|
-
}) => {
|
601
|
-
if (payload.persist && _get(state, [payload.name, 'persist'])) return;
|
602
|
-
const startId = payload.startId || _get(payload, 'nodes.0.nodeId');
|
603
|
-
// TODO non verificato, controllo precendente che non lo permette
|
604
|
-
/* istanbul ignore next */
|
605
|
-
if (!startId) {
|
606
|
-
// eslint-disable-next-line no-console
|
607
|
-
console.warn('Flower is empty');
|
608
|
-
return;
|
609
|
-
}
|
610
|
-
_set(state, payload.name, {
|
611
|
-
persist: payload.persist,
|
612
|
-
startId,
|
613
|
-
current: startId,
|
614
|
-
history: [startId],
|
615
|
-
nodes: generateNodes(payload.nodes),
|
616
|
-
nextRules: makeObjectRules(payload.nodes),
|
617
|
-
data: payload.initialData
|
618
|
-
});
|
619
|
-
},
|
620
|
-
// TODO usato solo da flower su vscode
|
621
|
-
setCurrentNode: /* istanbul ignore next */(state, {
|
622
|
-
payload
|
623
|
-
}) => {
|
452
|
+
historyAdd: (state, { payload }) => {
|
453
|
+
if (hasNode(state, payload.name, payload.node)) {
|
454
|
+
state[payload.name].history.push(payload.node);
|
455
|
+
_set(state, [payload.name, 'current'], payload.node);
|
456
|
+
}
|
457
|
+
return state;
|
458
|
+
},
|
459
|
+
historyPrevToNode: (state, { payload }) => {
|
460
|
+
const history = _get(state[typeof payload === 'string' ? payload : payload.name], ['history'], []);
|
461
|
+
const lastIndex = lastIndexOf(history, typeof payload === 'string' ? payload : payload.node);
|
462
|
+
// se passo un nodo che non esiste
|
463
|
+
if (lastIndex >= 0) {
|
464
|
+
const newHistory = _slice(history, 0, lastIndex + 1);
|
465
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'history'], newHistory);
|
466
|
+
_set(state, [typeof payload === 'string' ? payload : payload.name, 'current'], typeof payload === 'string' ? payload : payload.node);
|
467
|
+
}
|
468
|
+
return state;
|
469
|
+
},
|
470
|
+
setFormTouched: (state, { payload }) => {
|
471
|
+
if (!_get(state, [
|
472
|
+
typeof payload === 'string' ? payload : payload.flowName,
|
473
|
+
'nodes',
|
474
|
+
typeof payload === 'string' ? payload : payload.currentNode
|
475
|
+
])) {
|
476
|
+
return state;
|
477
|
+
}
|
478
|
+
_set(state, [
|
479
|
+
typeof payload === 'string' ? payload : payload.flowName,
|
480
|
+
'form',
|
481
|
+
typeof payload === 'string' ? payload : payload.currentNode,
|
482
|
+
'touched'
|
483
|
+
], true);
|
484
|
+
return state;
|
485
|
+
},
|
486
|
+
// TODO check internal logic and use case
|
624
487
|
/* istanbul ignore next */
|
625
|
-
|
626
|
-
|
627
|
-
|
488
|
+
forceAddHistory: (state, { payload }) => {
|
489
|
+
const { name, flowName, history } = payload;
|
490
|
+
const key = name || flowName || '' || '';
|
491
|
+
const resultHistory = history.slice(1, -1);
|
492
|
+
state[key].history.push(...resultHistory);
|
493
|
+
return state;
|
494
|
+
},
|
495
|
+
historyPop: (state, { payload }) => {
|
496
|
+
const container = _get(state, [payload.name]);
|
497
|
+
const startId = _get(container, ['startId']);
|
498
|
+
const history = _get(container, ['history']);
|
499
|
+
const originHistory = [..._get(container, ['history'], [])];
|
500
|
+
if (originHistory.length < 2) {
|
501
|
+
_set(state, [payload.name, 'current'], startId);
|
502
|
+
_set(state, [payload.name, 'history'], [startId]);
|
503
|
+
return state;
|
504
|
+
}
|
505
|
+
// elimino lo node corrente
|
506
|
+
history.pop();
|
507
|
+
const total = originHistory.length - 2;
|
508
|
+
// eslint-disable-next-line no-plusplus
|
509
|
+
for (let index = total; index > 0; index--) {
|
510
|
+
const curr = originHistory[index];
|
511
|
+
const nodeType = _get(container, ['nodes', curr, 'nodeType']);
|
512
|
+
const nodeDisabled = _get(container, ['nodes', curr, 'disabled']);
|
513
|
+
if (nodeDisabled ||
|
514
|
+
nodeType === 'FlowerAction' ||
|
515
|
+
nodeType === 'FlowerServer' ||
|
516
|
+
!nodeType) {
|
517
|
+
history.pop();
|
518
|
+
}
|
519
|
+
else {
|
520
|
+
break;
|
521
|
+
}
|
522
|
+
}
|
523
|
+
const lastId = _last(history);
|
524
|
+
_set(state, [payload.name, 'current'], lastId);
|
525
|
+
return state;
|
526
|
+
},
|
527
|
+
restoreHistory: (state, { payload }) => {
|
528
|
+
const startId = _get(state, [payload.name, 'startId']);
|
628
529
|
_set(state, [payload.name, 'current'], startId);
|
629
530
|
_set(state, [payload.name, 'history'], [startId]);
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
_unset(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id]);
|
644
|
-
_unset(state, [payload.name, 'form', payload.currentNode, 'isValidating']);
|
645
|
-
},
|
646
|
-
addData: (state, {
|
647
|
-
payload
|
648
|
-
}) => {
|
649
|
-
const prevData = _get(state, [payload.flowName, 'data']);
|
650
|
-
_set(state, [payload.flowName, 'data'], {
|
651
|
-
...prevData,
|
652
|
-
...payload.value
|
653
|
-
});
|
654
|
-
},
|
655
|
-
addDataByPath: (state, {
|
656
|
-
payload
|
657
|
-
}) => {
|
658
|
-
if (payload.id && payload.id.length) {
|
659
|
-
_set(state, [payload.flowName, 'data', ...payload.id], payload.value);
|
660
|
-
}
|
661
|
-
},
|
662
|
-
// TODO usato al momento solo il devtool
|
663
|
-
replaceData: /* istanbul ignore next */(state, {
|
664
|
-
payload
|
665
|
-
}) => {
|
531
|
+
return state;
|
532
|
+
},
|
533
|
+
replaceNode: (state, { payload }) => {
|
534
|
+
const { name, flowName, node } = payload;
|
535
|
+
// non ancora implementanto nell'hook useFlower
|
536
|
+
/* istanbul ignore next */
|
537
|
+
if (hasNode(state, name || flowName || '', node)) {
|
538
|
+
_set(state, [name || flowName || '', 'current'], node);
|
539
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
540
|
+
}
|
541
|
+
/* istanbul ignore next */
|
542
|
+
return state;
|
543
|
+
},
|
666
544
|
/* istanbul ignore next */
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
}) => {
|
677
|
-
_set(state, [payload.name, 'form', payload.currentNode, 'isValidating'], payload.isValidating);
|
678
|
-
},
|
679
|
-
node: (state, {
|
680
|
-
payload
|
681
|
-
}) => {
|
682
|
-
const {
|
683
|
-
name,
|
684
|
-
history
|
685
|
-
} = payload;
|
686
|
-
const node = payload.nodeId || payload.node || '';
|
687
|
-
const flowName = name || payload.flowName || '';
|
688
|
-
const startNode = _get(state, [payload.name, 'startId']);
|
689
|
-
const currentNodeId = _get(state, [payload.name, 'current'], startNode);
|
690
|
-
FlowerCoreReducers.setFormTouched(state, {
|
691
|
-
type: 'setFormTouched',
|
692
|
-
payload: {
|
693
|
-
flowName,
|
694
|
-
currentNode: currentNodeId
|
695
|
-
}
|
696
|
-
});
|
545
|
+
initializeFromNode: (state, { payload }) => {
|
546
|
+
const { name, flowName, node } = payload;
|
547
|
+
if (hasNode(state, name || flowName || '', node)) {
|
548
|
+
_set(state, [name || flowName || '', 'startId'], node);
|
549
|
+
_set(state, [name || flowName || '', 'current'], node);
|
550
|
+
_set(state, [name || flowName || '', 'history'], [node]);
|
551
|
+
}
|
552
|
+
return state;
|
553
|
+
},
|
697
554
|
/* istanbul ignore next */
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
}
|
707
|
-
|
708
|
-
}
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
payload
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
}
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
}
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
}
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
555
|
+
forceResetHistory: (state, { payload }) => {
|
556
|
+
const { name, flowName } = payload;
|
557
|
+
if (!name && !flowName)
|
558
|
+
return state;
|
559
|
+
_set(state, [name || flowName || '', 'history'], []);
|
560
|
+
return state;
|
561
|
+
},
|
562
|
+
destroy: (state, { payload }) => {
|
563
|
+
_set(state, [payload.name], {});
|
564
|
+
},
|
565
|
+
initNodes: (state, { payload }) => {
|
566
|
+
if (payload.persist && _get(state, [payload.name, 'persist']))
|
567
|
+
return;
|
568
|
+
const startId = payload.startId ||
|
569
|
+
_get(payload, 'initialState.startId') ||
|
570
|
+
_get(payload, 'nodes.0.nodeId');
|
571
|
+
// TODO non verificato, controllo precendente che non lo permette
|
572
|
+
/* istanbul ignore next */
|
573
|
+
if (!startId) {
|
574
|
+
// eslint-disable-next-line no-console
|
575
|
+
console.warn('Flower is empty');
|
576
|
+
return;
|
577
|
+
}
|
578
|
+
const current = _get(payload, 'initialState.current') || startId;
|
579
|
+
const history = _get(payload, 'initialState.history') || [startId];
|
580
|
+
_set(state, payload.name, {
|
581
|
+
persist: payload.persist,
|
582
|
+
startId,
|
583
|
+
current,
|
584
|
+
history,
|
585
|
+
nodes: generateNodes(payload.nodes),
|
586
|
+
nextRules: makeObjectRules(payload.nodes),
|
587
|
+
data: payload.initialData
|
588
|
+
});
|
589
|
+
},
|
590
|
+
// TODO usato solo da flower su vscode
|
591
|
+
setCurrentNode: /* istanbul ignore next */ (state, { payload }) => {
|
592
|
+
/* istanbul ignore next */
|
593
|
+
if (hasNode(state, payload.name, payload.node)) {
|
594
|
+
const startId = _get(state, [payload.name, 'startId']);
|
595
|
+
if (payload.node === startId) {
|
596
|
+
_set(state, [payload.name, 'current'], startId);
|
597
|
+
_set(state, [payload.name, 'history'], [startId]);
|
598
|
+
}
|
599
|
+
else {
|
600
|
+
_set(state, [payload.name, 'current'], payload.node);
|
601
|
+
}
|
602
|
+
}
|
603
|
+
},
|
604
|
+
formAddErrors: (state, { payload }) => {
|
605
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'errors', payload.id], payload.errors);
|
606
|
+
},
|
607
|
+
formRemoveErrors: (state, { payload }) => {
|
608
|
+
_unset(state, [
|
609
|
+
payload.name,
|
610
|
+
'form',
|
611
|
+
payload.currentNode,
|
612
|
+
'errors',
|
613
|
+
payload.id
|
614
|
+
]);
|
615
|
+
_unset(state, [payload.name, 'form', payload.currentNode, 'isValidating']);
|
616
|
+
},
|
617
|
+
addData: (state, { payload }) => {
|
618
|
+
const prevData = _get(state, [payload.flowName, 'data']);
|
619
|
+
_set(state, [payload.flowName, 'data'], { ...prevData, ...payload.value });
|
620
|
+
},
|
621
|
+
addDataByPath: (state, { payload }) => {
|
622
|
+
if (payload.id && payload.id.length) {
|
623
|
+
_set(state, [payload.flowName, 'data', ...payload.id], payload.value);
|
624
|
+
}
|
625
|
+
},
|
626
|
+
// TODO usato al momento solo il devtool
|
627
|
+
replaceData: /* istanbul ignore next */ (state, { payload }) => {
|
628
|
+
/* istanbul ignore next */
|
629
|
+
_set(state, [payload.flowName, 'data'], payload.value);
|
630
|
+
},
|
631
|
+
unsetData: (state, { payload }) => {
|
632
|
+
_unset(state, [payload.flowName, 'data', ...payload.id]);
|
633
|
+
},
|
634
|
+
setFormIsValidating: (state, { payload }) => {
|
635
|
+
_set(state, [payload.name, 'form', payload.currentNode, 'isValidating'], payload.isValidating);
|
636
|
+
},
|
637
|
+
node: (state, { payload }) => {
|
638
|
+
const { name, history } = payload;
|
639
|
+
const node = payload.nodeId || payload.node || '';
|
640
|
+
const flowName = name || payload.flowName || '';
|
641
|
+
const startNode = _get(state, [payload.name, 'startId']);
|
642
|
+
const currentNodeId = _get(state, [payload.name, 'current'], startNode);
|
643
|
+
FlowerCoreReducers.setFormTouched(state, {
|
644
|
+
type: 'setFormTouched',
|
645
|
+
payload: { flowName, currentNode: currentNodeId }
|
646
|
+
});
|
647
|
+
/* istanbul ignore next */
|
648
|
+
// eslint-disable-next-line no-underscore-dangle
|
649
|
+
if (devtoolState && _get(devtoolState, '__FLOWER_DEVTOOLS__') && history) {
|
650
|
+
FlowerCoreReducers.forceAddHistory(state, {
|
651
|
+
type: 'forceAddHistory',
|
652
|
+
payload: {
|
653
|
+
name,
|
654
|
+
flowName,
|
655
|
+
history
|
656
|
+
}
|
657
|
+
});
|
658
|
+
}
|
659
|
+
FlowerCoreReducers.historyAdd(state, {
|
660
|
+
type: 'historyAdd',
|
661
|
+
payload: { name: name || flowName || '', node }
|
662
|
+
});
|
663
|
+
},
|
664
|
+
prevToNode: (state, { payload }) => {
|
665
|
+
const { node, name, flowName } = payload;
|
666
|
+
FlowerCoreReducers.historyPrevToNode(state, {
|
667
|
+
type: 'historyPrevToNode',
|
668
|
+
payload: { name: name || flowName || '', node }
|
669
|
+
});
|
670
|
+
},
|
671
|
+
next: (state, { payload }) => {
|
672
|
+
const { name, data = {}, route } = payload;
|
673
|
+
const flowName = name || payload.flowName || '';
|
674
|
+
const currentNodeId = FlowerStateUtils.makeSelectCurrentNodeId(flowName)(state);
|
675
|
+
const currentNextRules = FlowerStateUtils.makeSelectCurrentNextRules(flowName)(state);
|
676
|
+
const form = FlowerStateUtils.makeSelectNodeErrors(flowName, currentNodeId)(state);
|
677
|
+
const clonedData = _cloneDeep(FlowerStateUtils.getAllData(state));
|
678
|
+
const stateWithNodeData = {
|
679
|
+
$in: data,
|
680
|
+
$form: form,
|
681
|
+
...clonedData
|
682
|
+
};
|
683
|
+
FlowerCoreReducers.setFormTouched(state, {
|
684
|
+
type: 'setFormTouched',
|
685
|
+
payload: { flowName, currentNode: currentNodeId }
|
686
|
+
});
|
687
|
+
if (!currentNextRules) {
|
688
|
+
return;
|
689
|
+
}
|
690
|
+
if (route) {
|
691
|
+
const rulesByName = generateRulesName(currentNextRules);
|
692
|
+
if (!rulesByName[route]) {
|
693
|
+
return;
|
694
|
+
}
|
695
|
+
FlowerCoreReducers.historyAdd(state, {
|
696
|
+
type: 'historyAdd',
|
697
|
+
payload: { name: flowName, node: rulesByName[route] }
|
698
|
+
});
|
699
|
+
return;
|
700
|
+
}
|
701
|
+
const validRule = findValidRule(currentNextRules, stateWithNodeData, flowName);
|
702
|
+
const nextNumberNode = _get(validRule, 'nodeId');
|
703
|
+
if (!nextNumberNode) {
|
704
|
+
return;
|
705
|
+
}
|
706
|
+
FlowerCoreReducers.historyAdd(state, {
|
707
|
+
type: 'historyAdd',
|
708
|
+
payload: { name: flowName, node: nextNumberNode }
|
709
|
+
});
|
710
|
+
},
|
711
|
+
prev: (state, { payload }) => {
|
712
|
+
const { name, flowName } = payload;
|
713
|
+
FlowerCoreReducers.historyPop(state, {
|
714
|
+
type: 'historyPop',
|
715
|
+
payload: { name: name || flowName || '' }
|
716
|
+
});
|
717
|
+
},
|
718
|
+
restart: (state, { payload }) => {
|
719
|
+
const { name, flowName } = payload;
|
720
|
+
FlowerCoreReducers.restoreHistory(state, {
|
721
|
+
type: 'restoreHistory',
|
722
|
+
payload: { name: name || flowName || '' }
|
723
|
+
});
|
724
|
+
},
|
725
|
+
reset: (state, { payload }) => {
|
726
|
+
const { name, flowName, initialData } = payload;
|
727
|
+
FlowerCoreReducers.restoreHistory(state, {
|
728
|
+
type: 'restoreHistory',
|
729
|
+
payload: { name: name || flowName || '' }
|
730
|
+
});
|
731
|
+
_set(state, [name || flowName || '', 'form'], {});
|
732
|
+
_set(state, [name || flowName || '', 'data'], initialData);
|
779
733
|
}
|
780
|
-
FlowerCoreReducers.historyAdd(state, {
|
781
|
-
type: 'historyAdd',
|
782
|
-
payload: {
|
783
|
-
name: flowName,
|
784
|
-
node: nextNumberNode
|
785
|
-
}
|
786
|
-
});
|
787
|
-
},
|
788
|
-
prev: (state, {
|
789
|
-
payload
|
790
|
-
}) => {
|
791
|
-
const {
|
792
|
-
name,
|
793
|
-
flowName
|
794
|
-
} = payload;
|
795
|
-
FlowerCoreReducers.historyPop(state, {
|
796
|
-
type: 'historyPop',
|
797
|
-
payload: {
|
798
|
-
name: name || flowName || ''
|
799
|
-
}
|
800
|
-
});
|
801
|
-
},
|
802
|
-
restart: (state, {
|
803
|
-
payload
|
804
|
-
}) => {
|
805
|
-
const {
|
806
|
-
name,
|
807
|
-
flowName
|
808
|
-
} = payload;
|
809
|
-
FlowerCoreReducers.restoreHistory(state, {
|
810
|
-
type: 'restoreHistory',
|
811
|
-
payload: {
|
812
|
-
name: name || flowName || ''
|
813
|
-
}
|
814
|
-
});
|
815
|
-
},
|
816
|
-
reset: (state, {
|
817
|
-
payload
|
818
|
-
}) => {
|
819
|
-
const {
|
820
|
-
name,
|
821
|
-
flowName,
|
822
|
-
initialData
|
823
|
-
} = payload;
|
824
|
-
FlowerCoreReducers.restoreHistory(state, {
|
825
|
-
type: 'restoreHistory',
|
826
|
-
payload: {
|
827
|
-
name: name || flowName || ''
|
828
|
-
}
|
829
|
-
});
|
830
|
-
_set(state, [name || flowName || '', 'form'], {});
|
831
|
-
_set(state, [name || flowName || '', 'data'], initialData);
|
832
|
-
}
|
833
734
|
};
|
834
735
|
|
835
736
|
const FlowerCoreStateSelectors = {
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
|
884
|
-
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
}
|
891
|
-
|
892
|
-
|
893
|
-
|
894
|
-
|
895
|
-
|
896
|
-
|
737
|
+
selectGlobal: (state) => state && state.flower,
|
738
|
+
selectFlower: (name) => (state) => _get(state, [name]),
|
739
|
+
selectFlowerFormNode: (id) => (state) => _get(state, ['form', id]),
|
740
|
+
selectFlowerHistory: (flower) => _get(flower, ['history'], []),
|
741
|
+
makeSelectNodesIds: (flower) => _get(flower, ['nodes']),
|
742
|
+
makeSelectStartNodeId: (flower) => _get(flower, ['startId']),
|
743
|
+
getDataByFlow: (flower) => _get(flower, ['data']) ?? {},
|
744
|
+
getDataFromState: (id) => (data) => (id === '*' ? data : _get(data, id)),
|
745
|
+
makeSelectNodeFormTouched: (form) => form && form.touched,
|
746
|
+
makeSelectCurrentNodeId: (flower, startNodeId) => _get(flower, ['current']) || startNodeId,
|
747
|
+
makeSelectCurrentNodeDisabled: (nodes, current) => !!_get(nodes, [current, 'disabled']),
|
748
|
+
makeSelectPrevNodeRetain: (nodes, history, current) => {
|
749
|
+
if (!nodes)
|
750
|
+
return;
|
751
|
+
const prevFlowerNode = [...history].reverse().find((el) => {
|
752
|
+
const { nodeType, retain } = nodes[el];
|
753
|
+
return nodeType === 'FlowerNode' || retain;
|
754
|
+
});
|
755
|
+
// eslint-disable-next-line consistent-return
|
756
|
+
if (nodes[current].nodeType === 'FlowerNode' || nodes[current].retain)
|
757
|
+
return;
|
758
|
+
if (!prevFlowerNode)
|
759
|
+
return;
|
760
|
+
if (nodes[prevFlowerNode] && nodes[prevFlowerNode].disabled)
|
761
|
+
return;
|
762
|
+
// eslint-disable-next-line consistent-return
|
763
|
+
return (nodes[prevFlowerNode] && nodes[prevFlowerNode].retain && prevFlowerNode);
|
764
|
+
},
|
765
|
+
makeSelectNodeErrors: (form) => {
|
766
|
+
return {
|
767
|
+
touched: form?.touched || false,
|
768
|
+
errors: form?.errors,
|
769
|
+
isValidating: form?.isValidating,
|
770
|
+
isValid: form && form.errors
|
771
|
+
? Object.values(form.errors).flat().length === 0
|
772
|
+
: true
|
773
|
+
};
|
774
|
+
},
|
775
|
+
makeSelectFieldError: (name, id, validate) => (data) => {
|
776
|
+
if (!validate || !data)
|
777
|
+
return [];
|
778
|
+
const errors = validate.filter((rule) => {
|
779
|
+
if (!rule)
|
780
|
+
return true;
|
781
|
+
if (!rule.rules)
|
782
|
+
return true;
|
783
|
+
const transformSelf = CoreUtils.mapKeysDeepLodash(rule.rules, (v, key) => key === '$self' ? id : key);
|
784
|
+
const [hasError] = MatchRules.rulesMatcher(transformSelf, data, false, {
|
785
|
+
prefix: name
|
786
|
+
});
|
787
|
+
return hasError;
|
788
|
+
});
|
789
|
+
const result = errors.map((r) => (r && r.message) || 'error');
|
790
|
+
return result.length === 0 ? [] : result;
|
791
|
+
},
|
792
|
+
selectorRulesDisabled: (id, rules, keys, flowName, value) => (data, form) => {
|
793
|
+
const newState = { ...data, ...value, $form: form };
|
794
|
+
const state = Object.assign(newState, id ? { $self: _get(newState, [flowName, ...id.split('.')]) } : {});
|
795
|
+
if (!rules)
|
796
|
+
return false;
|
797
|
+
if (typeof rules === 'function') {
|
798
|
+
return !rules(state);
|
799
|
+
}
|
800
|
+
if (!keys)
|
801
|
+
return false;
|
802
|
+
const res = keys.reduce((acc, inc) => {
|
803
|
+
const k = inc;
|
804
|
+
return Object.assign(acc, { [k]: _get(state, k) });
|
805
|
+
}, {});
|
806
|
+
const [disabled] = MatchRules.rulesMatcher(rules, { ...flat.unflatten(res) }, false, { prefix: flowName });
|
807
|
+
return disabled;
|
897
808
|
}
|
898
|
-
if (!keys) return false;
|
899
|
-
const res = keys.reduce((acc, inc) => {
|
900
|
-
const k = inc;
|
901
|
-
return Object.assign(acc, {
|
902
|
-
[k]: _get(state, k)
|
903
|
-
});
|
904
|
-
}, {});
|
905
|
-
const [disabled] = MatchRules.rulesMatcher(rules, {
|
906
|
-
...flat.unflatten(res)
|
907
|
-
}, false, {
|
908
|
-
prefix: flowName
|
909
|
-
});
|
910
|
-
return disabled;
|
911
|
-
}
|
912
809
|
};
|
913
810
|
|
914
811
|
exports.RulesModes = void 0;
|
915
812
|
(function (RulesModes) {
|
916
|
-
|
917
|
-
|
813
|
+
RulesModes["$and"] = "$and";
|
814
|
+
RulesModes["$or"] = "$or";
|
918
815
|
})(exports.RulesModes || (exports.RulesModes = {}));
|
919
816
|
var RulesOperators;
|
920
817
|
(function (RulesOperators) {
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
|
933
|
-
|
934
|
-
|
935
|
-
|
818
|
+
RulesOperators["$exist"] = "$exist";
|
819
|
+
RulesOperators["$eq"] = "$eq";
|
820
|
+
RulesOperators["$ne"] = "$ne";
|
821
|
+
RulesOperators["$gt"] = "$gt";
|
822
|
+
RulesOperators["$gte"] = "$gte";
|
823
|
+
RulesOperators["$lt"] = "$lt";
|
824
|
+
RulesOperators["$lte"] = "$lte";
|
825
|
+
RulesOperators["$strGt"] = "$strGt";
|
826
|
+
RulesOperators["$strGte"] = "$strGte";
|
827
|
+
RulesOperators["$strLt"] = "$strLt";
|
828
|
+
RulesOperators["$strLte"] = "$strLte";
|
829
|
+
RulesOperators["$in"] = "$in";
|
830
|
+
RulesOperators["$nin"] = "$nin";
|
831
|
+
RulesOperators["$all"] = "$all";
|
832
|
+
RulesOperators["$regex"] = "$regex";
|
936
833
|
})(RulesOperators || (RulesOperators = {}));
|
937
834
|
|
938
835
|
exports.RulesOperators = void 0;
|
939
836
|
(function (RulesOperators) {
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
837
|
+
RulesOperators["$exist"] = "$exist";
|
838
|
+
RulesOperators["$eq"] = "$eq";
|
839
|
+
RulesOperators["$ne"] = "$ne";
|
840
|
+
RulesOperators["$gt"] = "$gt";
|
841
|
+
RulesOperators["$gte"] = "$gte";
|
842
|
+
RulesOperators["$lt"] = "$lt";
|
843
|
+
RulesOperators["$lte"] = "$lte";
|
844
|
+
RulesOperators["$strGt"] = "$strGt";
|
845
|
+
RulesOperators["$strGte"] = "$strGte";
|
846
|
+
RulesOperators["$strLt"] = "$strLt";
|
847
|
+
RulesOperators["$strLte"] = "$strLte";
|
848
|
+
RulesOperators["$in"] = "$in";
|
849
|
+
RulesOperators["$nin"] = "$nin";
|
850
|
+
RulesOperators["$all"] = "$all";
|
851
|
+
RulesOperators["$regex"] = "$regex";
|
955
852
|
})(exports.RulesOperators || (exports.RulesOperators = {}));
|
956
853
|
|
957
854
|
const devtoolState = {};
|