relay-forms-nodeps 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -108
- package/lib/RelayFormsTypes.d.ts +1 -0
- package/lib/Utils.d.ts +5 -8
- package/lib/Utils.js +10 -65
- package/lib/es/RelayFormsTypes.d.ts +1 -0
- package/lib/es/Utils.d.ts +5 -8
- package/lib/es/Utils.js +10 -67
- package/lib/es/relay/RelayRecordProxy.d.ts +1 -0
- package/lib/es/relay/RelayRecordProxy.js +3 -0
- package/lib/es/relay/RelayRecordSourceMutator.d.ts +1 -0
- package/lib/es/relay/RelayRecordSourceMutator.js +13 -0
- package/lib/es/relay/RelayTypes.d.ts +1 -0
- package/lib/es/relay/queries.d.ts +6 -4
- package/lib/es/relay/queries.js +6 -3
- package/lib/es/useFormSetValue.d.ts +5 -1
- package/lib/es/useFormSetValue.js +162 -64
- package/lib/es/useFormState.d.ts +12 -2
- package/lib/es/useFormState.js +10 -40
- package/lib/es/useFormSubmit.js +104 -89
- package/lib/es/useFormValue.js +1 -1
- package/lib/relay/RelayRecordProxy.d.ts +1 -0
- package/lib/relay/RelayRecordProxy.js +3 -0
- package/lib/relay/RelayRecordSourceMutator.d.ts +1 -0
- package/lib/relay/RelayRecordSourceMutator.js +13 -0
- package/lib/relay/RelayTypes.d.ts +1 -0
- package/lib/relay/queries.d.ts +6 -4
- package/lib/relay/queries.js +6 -3
- package/lib/umd/relay-forms-nodeps.js +319 -273
- package/lib/umd/relay-forms-nodeps.js.map +1 -1
- package/lib/umd/relay-forms-nodeps.min.js +1 -1
- package/lib/umd/relay-forms-nodeps.min.js.map +1 -1
- package/lib/useFormSetValue.d.ts +5 -1
- package/lib/useFormSetValue.js +162 -63
- package/lib/useFormState.d.ts +12 -2
- package/lib/useFormState.js +10 -40
- package/lib/useFormSubmit.js +103 -88
- package/lib/useFormValue.js +1 -1
- package/package.json +15 -2
- package/yarn-error.log +31 -31
|
@@ -223,14 +223,6 @@ function hasOverlappingIDs(seenRecords, updatedRecordIDs) {
|
|
|
223
223
|
return false;
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
function useForceUpdate() {
|
|
227
|
-
var _a = React.useReducer(function (x) {
|
|
228
|
-
return x + 1;
|
|
229
|
-
}, 0),
|
|
230
|
-
forceUpdate = _a[1];
|
|
231
|
-
return forceUpdate;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
226
|
var id = {
|
|
235
227
|
name: 'id'
|
|
236
228
|
};
|
|
@@ -246,6 +238,9 @@ var error = {
|
|
|
246
238
|
var value = {
|
|
247
239
|
name: 'value'
|
|
248
240
|
};
|
|
241
|
+
var label = {
|
|
242
|
+
name: 'label'
|
|
243
|
+
};
|
|
249
244
|
var kindFragment = 'Fragment';
|
|
250
245
|
var QueryErrors = {
|
|
251
246
|
id: '1',
|
|
@@ -262,9 +257,9 @@ var QueryErrors = {
|
|
|
262
257
|
name: 'isValidating'
|
|
263
258
|
}, {
|
|
264
259
|
kind: LINKED_FIELD,
|
|
265
|
-
name: '
|
|
260
|
+
name: 'errors',
|
|
266
261
|
plural: true,
|
|
267
|
-
selections: [id, key, error, check]
|
|
262
|
+
selections: [id, key, error, check, label]
|
|
268
263
|
}]
|
|
269
264
|
}] /*: any*/
|
|
270
265
|
}
|
|
@@ -296,7 +291,7 @@ var FragmentSet = {
|
|
|
296
291
|
var FragmentValue = {
|
|
297
292
|
kind: kindFragment,
|
|
298
293
|
name: 'fragmentValue',
|
|
299
|
-
selections: [id, value, error]
|
|
294
|
+
selections: [id, value, error, label]
|
|
300
295
|
};
|
|
301
296
|
|
|
302
297
|
/**
|
|
@@ -328,16 +323,6 @@ function createOperationDescriptor(request) {
|
|
|
328
323
|
}
|
|
329
324
|
|
|
330
325
|
var PREFIX_LOCAL_FORM = 'local:form';
|
|
331
|
-
var internalCommitLocalUpdate = function (environment, updater) {
|
|
332
|
-
environment.commitUpdate(function (store) {
|
|
333
|
-
var localForm = store.get(PREFIX_LOCAL_FORM);
|
|
334
|
-
if (!localForm) {
|
|
335
|
-
localForm = store.create(PREFIX_LOCAL_FORM).setLinkedRecords([], 'entries');
|
|
336
|
-
store.getRoot().setLinkedRecord(localForm, 'form');
|
|
337
|
-
}
|
|
338
|
-
updater(store, localForm);
|
|
339
|
-
});
|
|
340
|
-
};
|
|
341
326
|
function getFieldId(key) {
|
|
342
327
|
return PREFIX_LOCAL_FORM + '.' + key;
|
|
343
328
|
}
|
|
@@ -347,291 +332,336 @@ function getSnapshot(environment, fragment, key) {
|
|
|
347
332
|
};
|
|
348
333
|
return environment.lookup(getSingularSelector(fragment, item));
|
|
349
334
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
};
|
|
359
|
-
var commitValidateIntoRelay = function (entries, isSubmitting, environment) {
|
|
360
|
-
internalCommitLocalUpdate(environment, function (store, form) {
|
|
361
|
-
form.setValue(isSubmitting, 'isSubmitting');
|
|
362
|
-
form.setValue(true, 'isValidating');
|
|
363
|
-
entries.forEach(function (entry) {
|
|
364
|
-
return store.get(getFieldId(entry.key)).setValue('START', 'check');
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
};
|
|
368
|
-
var commitSubmitEndRelay = function (environment) {
|
|
369
|
-
internalCommitLocalUpdate(environment, function (_, form) {
|
|
370
|
-
form.setValue(false, 'isSubmitting');
|
|
371
|
-
});
|
|
372
|
-
};
|
|
373
|
-
var commitValidateEndRelay = function (environment) {
|
|
374
|
-
internalCommitLocalUpdate(environment, function (_, form) {
|
|
375
|
-
form.setValue(false, 'isValidating');
|
|
376
|
-
});
|
|
377
|
-
};
|
|
378
|
-
var commitResetField = function (environment, key) {
|
|
379
|
-
var id = getFieldId(key);
|
|
380
|
-
internalCommitLocalUpdate(environment, function (store, form) {
|
|
381
|
-
var entriesArray = form.getLinkedRecords('entries');
|
|
382
|
-
var newEntries = entriesArray.filter(function (value) {
|
|
383
|
-
return value.getDataID() != id;
|
|
384
|
-
});
|
|
385
|
-
form.setLinkedRecords(newEntries, 'entries');
|
|
386
|
-
store.delete(id);
|
|
387
|
-
});
|
|
388
|
-
};
|
|
389
|
-
var commitValue = function (key, value, check, environment) {
|
|
390
|
-
var id = getFieldId(key);
|
|
391
|
-
internalCommitLocalUpdate(environment, function (store, form) {
|
|
392
|
-
var field = store.get(id);
|
|
393
|
-
if (!field) {
|
|
394
|
-
field = store.create(id);
|
|
395
|
-
var entriesArray = form.getLinkedRecords('entries');
|
|
396
|
-
entriesArray.push(field);
|
|
397
|
-
form.setLinkedRecords(entriesArray, 'entries');
|
|
335
|
+
function commit(environment, updater) {
|
|
336
|
+
environment.commitUpdate(function (store) {
|
|
337
|
+
var localForm = store.get(PREFIX_LOCAL_FORM);
|
|
338
|
+
if (!localForm) {
|
|
339
|
+
localForm = store.create(PREFIX_LOCAL_FORM).setLinkedRecords([], 'entries').setLinkedRecords([], 'errors');
|
|
340
|
+
store.getRoot().setLinkedRecord(localForm, 'form');
|
|
398
341
|
}
|
|
399
|
-
|
|
400
|
-
});
|
|
401
|
-
};
|
|
402
|
-
var commitErrorIntoRelay = function (key, error, environment) {
|
|
403
|
-
internalCommitLocalUpdate(environment, function (store) {
|
|
404
|
-
store.get(getFieldId(key)).setValue(error, 'error').setValue('DONE', 'check');
|
|
342
|
+
updater(store, localForm);
|
|
405
343
|
});
|
|
406
|
-
}
|
|
344
|
+
}
|
|
407
345
|
var operationQueryForm = createOperationDescriptor(QueryFields);
|
|
408
346
|
var operationQueryErrorsForm = createOperationDescriptor(QueryErrors);
|
|
409
347
|
|
|
410
348
|
var useFormState = function () {
|
|
411
|
-
var ref = React__default["default"].useRef({
|
|
412
|
-
errors: undefined,
|
|
413
|
-
isValidating: false,
|
|
414
|
-
isSubmitting: false,
|
|
415
|
-
isValid: false
|
|
416
|
-
});
|
|
417
|
-
var forceUpdate = useForceUpdate();
|
|
418
349
|
var environment = useStore();
|
|
350
|
+
var snapshot = React__default["default"].useMemo(function () {
|
|
351
|
+
return environment.lookup(operationQueryErrorsForm.fragment);
|
|
352
|
+
}, [environment]);
|
|
353
|
+
var _a = React__default["default"].useState(snapshot.data.form),
|
|
354
|
+
data = _a[0],
|
|
355
|
+
setData = _a[1];
|
|
419
356
|
React__default["default"].useEffect(function () {
|
|
420
|
-
function checkError(s) {
|
|
421
|
-
var data = s.data;
|
|
422
|
-
var form = data.form;
|
|
423
|
-
var entries = form.entries;
|
|
424
|
-
var current = ref.current;
|
|
425
|
-
var entryErrors = entries.filter(function (value) {
|
|
426
|
-
return !!value.error;
|
|
427
|
-
});
|
|
428
|
-
var entryValidated = entries.filter(function (value) {
|
|
429
|
-
return value.check === 'DONE';
|
|
430
|
-
});
|
|
431
|
-
var errors = entryErrors.length > 0 ? entryErrors : undefined;
|
|
432
|
-
var isValidating = form.isValidating;
|
|
433
|
-
var isSubmitting = form.isSubmitting;
|
|
434
|
-
var isValid = entries.length === entryValidated.length && (!errors || Object.keys(errors).length === 0);
|
|
435
|
-
var newState = {
|
|
436
|
-
errors: errors,
|
|
437
|
-
isValid: isValid,
|
|
438
|
-
isValidating: isValidating,
|
|
439
|
-
isSubmitting: isSubmitting
|
|
440
|
-
};
|
|
441
|
-
if (!areEqual(current, newState)) {
|
|
442
|
-
ref.current = newState;
|
|
443
|
-
forceUpdate();
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
var snapshot = environment.lookup(operationQueryErrorsForm.fragment);
|
|
447
|
-
checkError(snapshot);
|
|
448
357
|
return environment.subscribe(snapshot, function (s) {
|
|
449
|
-
|
|
358
|
+
setData(s.data.form);
|
|
450
359
|
}).dispose;
|
|
451
|
-
|
|
452
|
-
|
|
360
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
361
|
+
}, [snapshot]);
|
|
362
|
+
return data;
|
|
453
363
|
};
|
|
454
364
|
|
|
455
|
-
|
|
456
|
-
var
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
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
|
-
var submit_1 = onSubmit(result_1);
|
|
500
|
-
if (isPromise(submit_1)) {
|
|
501
|
-
submit_1.then(internalDispose).catch(internalDispose);
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
internalDispose();
|
|
365
|
+
function useForceUpdate() {
|
|
366
|
+
var _a = React.useReducer(function (x) {
|
|
367
|
+
return x + 1;
|
|
368
|
+
}, 0),
|
|
369
|
+
forceUpdate = _a[1];
|
|
370
|
+
return forceUpdate;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
var INIT = 0;
|
|
374
|
+
var TOBEVALIDATE = 1;
|
|
375
|
+
var VALIDATING = 2;
|
|
376
|
+
var DONE = 3;
|
|
377
|
+
var DONEVALIDATED = 4;
|
|
378
|
+
var RESET = 5;
|
|
379
|
+
function getInitCheck(validate) {
|
|
380
|
+
return validate ? TOBEVALIDATE : DONE;
|
|
381
|
+
}
|
|
382
|
+
function isDone(check) {
|
|
383
|
+
return check === DONE || check === DONEVALIDATED;
|
|
384
|
+
}
|
|
385
|
+
function logicSetValue(params) {
|
|
386
|
+
var environment = params.environment,
|
|
387
|
+
key = params.key,
|
|
388
|
+
initialValue = params.initialValue,
|
|
389
|
+
forceUpdate = params.forceUpdate,
|
|
390
|
+
validateOnChange = params.validateOnChange,
|
|
391
|
+
label = params.label;
|
|
392
|
+
var validate = undefined;
|
|
393
|
+
var firstSet = true;
|
|
394
|
+
var localId = getFieldId(key);
|
|
395
|
+
function getValidate() {
|
|
396
|
+
return validate;
|
|
397
|
+
}
|
|
398
|
+
var ref = {
|
|
399
|
+
check: INIT,
|
|
400
|
+
isChecking: false
|
|
401
|
+
};
|
|
402
|
+
var dataResult = {
|
|
403
|
+
value: initialValue,
|
|
404
|
+
error: undefined
|
|
405
|
+
};
|
|
406
|
+
function commitField(commitValue) {
|
|
407
|
+
if (commitValue === void 0) {
|
|
408
|
+
commitValue = true;
|
|
506
409
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
410
|
+
commit(environment, function (store, form) {
|
|
411
|
+
var field = store.get(localId);
|
|
412
|
+
if (!field) {
|
|
413
|
+
field = store.create(localId).setValue(localId, 'id').setValue(label, 'label').setValue(key, 'key');
|
|
414
|
+
var entriesArray = form.getLinkedRecords('entries');
|
|
415
|
+
entriesArray.push(field);
|
|
416
|
+
form.setLinkedRecords(entriesArray, 'entries');
|
|
417
|
+
}
|
|
418
|
+
if (ref.check === VALIDATING) {
|
|
419
|
+
form.setValue(true, 'isValidating');
|
|
420
|
+
}
|
|
421
|
+
field.setValue(ref.check, 'check').setValue(dataResult.error, 'error');
|
|
422
|
+
if (commitValue) field.setValue(dataResult.value, 'value');
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
function setValidate(newValidate) {
|
|
426
|
+
if (validate === newValidate && !firstSet) {
|
|
427
|
+
// first set only needs this
|
|
511
428
|
return;
|
|
512
429
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
430
|
+
firstSet = false;
|
|
431
|
+
validate = newValidate;
|
|
432
|
+
// validation was called
|
|
433
|
+
if (ref.check === DONEVALIDATED && validate) {
|
|
434
|
+
ref.check = VALIDATING;
|
|
435
|
+
} else {
|
|
436
|
+
// reinit
|
|
437
|
+
ref.check = getInitCheck(validate);
|
|
519
438
|
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
439
|
+
commitField();
|
|
440
|
+
}
|
|
441
|
+
function getData() {
|
|
442
|
+
return dataResult;
|
|
443
|
+
}
|
|
444
|
+
function changeData(value, error) {
|
|
445
|
+
dataResult = {
|
|
446
|
+
value: value,
|
|
447
|
+
error: error
|
|
448
|
+
};
|
|
449
|
+
forceUpdate();
|
|
450
|
+
}
|
|
451
|
+
function setValue(newValue) {
|
|
452
|
+
setValueInternal(newValue);
|
|
453
|
+
}
|
|
454
|
+
function setValueInternal(newValue, error) {
|
|
455
|
+
if (ref.check === DONEVALIDATED) {
|
|
456
|
+
ref.check = VALIDATING;
|
|
526
457
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
subscribe(false);
|
|
458
|
+
changeData(newValue, error);
|
|
459
|
+
commitField();
|
|
460
|
+
}
|
|
461
|
+
function finalizeCheck(error) {
|
|
462
|
+
ref.isChecking = false;
|
|
463
|
+
ref.check = DONEVALIDATED;
|
|
464
|
+
if (dataResult.error !== error) {
|
|
465
|
+
changeData(dataResult.value, error);
|
|
536
466
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
467
|
+
commitField(false);
|
|
468
|
+
}
|
|
469
|
+
function internalValidate(value, validateFunction) {
|
|
470
|
+
ref.isChecking = true;
|
|
471
|
+
var result = validateFunction(value);
|
|
472
|
+
function internalFinalize(error) {
|
|
473
|
+
var validate = getValidate();
|
|
474
|
+
if (value !== dataResult.value || validate !== validateFunction) {
|
|
475
|
+
internalValidate(dataResult.value, validate);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
finalizeCheck(error);
|
|
541
479
|
}
|
|
542
|
-
if (
|
|
543
|
-
|
|
544
|
-
|
|
480
|
+
if (isPromise(result)) {
|
|
481
|
+
result.then(internalFinalize).catch(internalFinalize);
|
|
482
|
+
} else {
|
|
483
|
+
internalFinalize(result);
|
|
545
484
|
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
485
|
+
}
|
|
486
|
+
function subscribe() {
|
|
487
|
+
var snapshot = getSnapshot(environment, FragmentSet, key);
|
|
488
|
+
var disposeSubscrition = environment.subscribe(snapshot, function (s) {
|
|
489
|
+
var data = s.data;
|
|
490
|
+
var dataCheck = data.check;
|
|
491
|
+
var isValidating = dataCheck === VALIDATING;
|
|
492
|
+
var isReset = dataCheck === RESET;
|
|
493
|
+
ref.check = dataCheck;
|
|
494
|
+
var validate = getValidate();
|
|
495
|
+
if (isReset) {
|
|
496
|
+
ref.check = getInitCheck(validate);
|
|
497
|
+
setValueInternal(initialValue);
|
|
498
|
+
} else if (isValidating && !ref.isChecking) {
|
|
499
|
+
internalValidate(dataResult.value, validate);
|
|
500
|
+
}
|
|
501
|
+
}).dispose;
|
|
502
|
+
var dispose = function () {
|
|
503
|
+
disposeSubscrition();
|
|
504
|
+
commit(environment, function (store, form) {
|
|
505
|
+
var entriesArray = form.getLinkedRecords('entries');
|
|
506
|
+
var newEntries = entriesArray.filter(function (value) {
|
|
507
|
+
return value.getDataID() != localId;
|
|
508
|
+
});
|
|
509
|
+
form.setLinkedRecords(newEntries, 'entries');
|
|
510
|
+
store.delete(localId);
|
|
511
|
+
});
|
|
552
512
|
};
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
556
|
-
|
|
513
|
+
if (validateOnChange) {
|
|
514
|
+
internalValidate(initialValue, getValidate());
|
|
515
|
+
}
|
|
516
|
+
return dispose;
|
|
517
|
+
}
|
|
518
|
+
return {
|
|
519
|
+
subscribe: subscribe,
|
|
520
|
+
getData: getData,
|
|
521
|
+
setValue: setValue,
|
|
522
|
+
setValidate: setValidate
|
|
523
|
+
};
|
|
524
|
+
}
|
|
557
525
|
function useFormSetValue(_a) {
|
|
558
526
|
var key = _a.key,
|
|
559
527
|
initialValue = _a.initialValue,
|
|
560
528
|
validate = _a.validate,
|
|
561
|
-
validateOnChange = _a.validateOnChange
|
|
529
|
+
validateOnChange = _a.validateOnChange,
|
|
530
|
+
label = _a.label;
|
|
562
531
|
var forceUpdate = useForceUpdate();
|
|
563
532
|
var environment = useStore();
|
|
564
|
-
var
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
commitValue(key, newValue, ref.current.check, environment);
|
|
576
|
-
forceUpdate();
|
|
577
|
-
}, [environment, key, validate, forceUpdate]);
|
|
533
|
+
var resolver = React__default["default"].useMemo(function () {
|
|
534
|
+
return logicSetValue({
|
|
535
|
+
environment: environment,
|
|
536
|
+
key: key,
|
|
537
|
+
label: label,
|
|
538
|
+
initialValue: initialValue,
|
|
539
|
+
forceUpdate: forceUpdate,
|
|
540
|
+
validateOnChange: validateOnChange
|
|
541
|
+
});
|
|
542
|
+
}, [environment, key, initialValue, forceUpdate, validateOnChange, label]);
|
|
543
|
+
resolver.setValidate(validate);
|
|
578
544
|
React__default["default"].useEffect(function () {
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}).dispose;
|
|
600
|
-
var dispose = function () {
|
|
601
|
-
disposeSubscrition();
|
|
602
|
-
commitResetField(environment, key);
|
|
545
|
+
return resolver.subscribe();
|
|
546
|
+
}, [resolver]);
|
|
547
|
+
return [resolver.getData(), resolver.setValue];
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function logicSubmit(environment, onSubmit) {
|
|
551
|
+
function dispose() {
|
|
552
|
+
subscription && subscription.dispose();
|
|
553
|
+
subscription = undefined;
|
|
554
|
+
}
|
|
555
|
+
var isSubmitting = false;
|
|
556
|
+
var subscription;
|
|
557
|
+
function execute(snapshot) {
|
|
558
|
+
var disposeSubmit = function () {
|
|
559
|
+
isSubmitting = false;
|
|
560
|
+
commit(environment, function (_, form) {
|
|
561
|
+
form.setValue(false, 'isSubmitting');
|
|
562
|
+
});
|
|
603
563
|
};
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
564
|
+
var data = snapshot.data;
|
|
565
|
+
var entries = data.form.entries;
|
|
566
|
+
var filtered = entries.filter(function (value) {
|
|
567
|
+
return isDone(value.check);
|
|
568
|
+
});
|
|
569
|
+
var isValidating = filtered.length !== entries.length;
|
|
570
|
+
var errors = entries.filter(function (value) {
|
|
571
|
+
return !!value.error;
|
|
572
|
+
}).map(function (value) {
|
|
573
|
+
return value.id;
|
|
574
|
+
});
|
|
575
|
+
var isValid = !isValidating && errors.length === 0;
|
|
576
|
+
commit(environment, function (store, form) {
|
|
577
|
+
form.setValue(isValidating, 'isValidating').setValue(isValid, 'isValid');
|
|
578
|
+
var errorFields = [];
|
|
579
|
+
errors.forEach(function (id) {
|
|
580
|
+
errorFields.push(store.get(id));
|
|
581
|
+
});
|
|
582
|
+
form.setLinkedRecords(errorFields, 'errors');
|
|
583
|
+
});
|
|
584
|
+
if (isValid && isSubmitting) {
|
|
585
|
+
dispose();
|
|
586
|
+
var result_1 = {};
|
|
587
|
+
entries.forEach(function (entry) {
|
|
588
|
+
result_1[entry.key] = entry.value;
|
|
589
|
+
});
|
|
590
|
+
var submit_1 = onSubmit(result_1);
|
|
591
|
+
if (isPromise(submit_1)) {
|
|
592
|
+
submit_1.then(disposeSubmit).catch(disposeSubmit);
|
|
593
|
+
return;
|
|
609
594
|
}
|
|
610
|
-
|
|
595
|
+
} else if (!isValidating) {
|
|
596
|
+
disposeSubmit();
|
|
611
597
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
return;
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
}
|
|
622
|
-
if (isPromise(result)) {
|
|
623
|
-
result.then(internalFinalize).catch(internalFinalize);
|
|
624
|
-
} else {
|
|
625
|
-
internalFinalize(result);
|
|
626
|
-
}
|
|
598
|
+
}
|
|
599
|
+
function reset() {
|
|
600
|
+
if (!isSubmitting) {
|
|
601
|
+
dispose();
|
|
602
|
+
commit(environment, function (_, form) {
|
|
603
|
+
form.setValue(false, 'isSubmitting').setValue(false, 'isValidating').setLinkedRecords([], 'errors').getLinkedRecords('entries').forEach(function (entry) {
|
|
604
|
+
return entry.setValue(RESET, 'check').setValue(undefined, 'error');
|
|
605
|
+
});
|
|
606
|
+
});
|
|
627
607
|
}
|
|
628
|
-
|
|
629
|
-
|
|
608
|
+
}
|
|
609
|
+
function validate() {
|
|
610
|
+
commit(environment, function (_, form) {
|
|
611
|
+
var tobeValitating = form.setValue(isSubmitting, 'isSubmitting').getLinkedRecords('entries').filter(function (value) {
|
|
612
|
+
return value.getValue('check') === TOBEVALIDATE;
|
|
613
|
+
});
|
|
614
|
+
form.setValue(tobeValitating.length === 0, 'isValidating');
|
|
615
|
+
tobeValitating.forEach(function (entry) {
|
|
616
|
+
return entry.setValue(VALIDATING, 'check');
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
var snapshot = environment.lookup(operationQueryForm.fragment);
|
|
620
|
+
if (!subscription) {
|
|
621
|
+
subscription = environment.subscribe(snapshot, function (s) {
|
|
622
|
+
return execute(s);
|
|
623
|
+
});
|
|
630
624
|
}
|
|
631
|
-
|
|
632
|
-
}
|
|
633
|
-
|
|
625
|
+
execute(snapshot);
|
|
626
|
+
}
|
|
627
|
+
function submit(event) {
|
|
628
|
+
event && event.preventDefault();
|
|
629
|
+
if (!isSubmitting) {
|
|
630
|
+
isSubmitting = true;
|
|
631
|
+
validate();
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
function subscribe() {
|
|
635
|
+
var disposeRetain = environment.retain(operationQueryForm).dispose;
|
|
636
|
+
return function () {
|
|
637
|
+
dispose();
|
|
638
|
+
disposeRetain();
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
var result = {
|
|
642
|
+
submit: submit,
|
|
643
|
+
validate: validate,
|
|
644
|
+
reset: reset
|
|
645
|
+
};
|
|
646
|
+
function getData() {
|
|
647
|
+
return result;
|
|
648
|
+
}
|
|
649
|
+
return {
|
|
650
|
+
subscribe: subscribe,
|
|
651
|
+
getData: getData
|
|
652
|
+
};
|
|
634
653
|
}
|
|
654
|
+
var useFormSubmit = function (_a) {
|
|
655
|
+
var onSubmit = _a.onSubmit;
|
|
656
|
+
var environment = useStore();
|
|
657
|
+
var resolver = React__default["default"].useMemo(function () {
|
|
658
|
+
return logicSubmit(environment, onSubmit);
|
|
659
|
+
}, [environment, onSubmit]);
|
|
660
|
+
React__default["default"].useEffect(function () {
|
|
661
|
+
return resolver.subscribe();
|
|
662
|
+
}, [resolver]);
|
|
663
|
+
return resolver.getData();
|
|
664
|
+
};
|
|
635
665
|
|
|
636
666
|
function useFormValue(key) {
|
|
637
667
|
var environment = useStore();
|
|
@@ -645,7 +675,7 @@ function useFormValue(key) {
|
|
|
645
675
|
return environment.subscribe(snapshot, function (s) {
|
|
646
676
|
setData(s.data);
|
|
647
677
|
}).dispose;
|
|
648
|
-
}, [environment, snapshot
|
|
678
|
+
}, [environment, snapshot]);
|
|
649
679
|
return data;
|
|
650
680
|
}
|
|
651
681
|
|
|
@@ -1048,6 +1078,19 @@ var RelayRecordSourceMutator = /** @class */function () {
|
|
|
1048
1078
|
RelayRecordSourceMutator.prototype.getStatus = function (dataID) {
|
|
1049
1079
|
return this._sink.has(dataID) ? this._sink.getStatus(dataID) : this._base.getStatus(dataID);
|
|
1050
1080
|
};
|
|
1081
|
+
RelayRecordSourceMutator.prototype.getValue = function (dataID, storageKey) {
|
|
1082
|
+
for (var ii = 0; ii < this.__sources.length; ii++) {
|
|
1083
|
+
var record = this.__sources[ii].get(dataID);
|
|
1084
|
+
if (record) {
|
|
1085
|
+
var value = RelayModernRecord.getValue(record, storageKey);
|
|
1086
|
+
if (value !== undefined) {
|
|
1087
|
+
return value;
|
|
1088
|
+
}
|
|
1089
|
+
} else if (record === null) {
|
|
1090
|
+
return null;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1051
1094
|
RelayRecordSourceMutator.prototype.setValue = function (dataID, storageKey, value) {
|
|
1052
1095
|
var sinkRecord = this._getSinkRecord(dataID);
|
|
1053
1096
|
RelayModernRecord.setValue(sinkRecord, storageKey, value);
|
|
@@ -1097,6 +1140,9 @@ var RelayRecordProxy = /** @class */function () {
|
|
|
1097
1140
|
this._mutator.setValue(this._dataID, name, value);
|
|
1098
1141
|
return this;
|
|
1099
1142
|
};
|
|
1143
|
+
RelayRecordProxy.prototype.getValue = function (name) {
|
|
1144
|
+
return this._mutator.getValue(this._dataID, name);
|
|
1145
|
+
};
|
|
1100
1146
|
RelayRecordProxy.prototype.setLinkedRecord = function (record, name) {
|
|
1101
1147
|
var linkedID = record.getDataID();
|
|
1102
1148
|
this._mutator.setLinkedRecordID(this._dataID, name, linkedID);
|