@wordpress/core-data 6.11.0 → 6.12.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/CHANGELOG.md +2 -0
- package/README.md +4 -0
- package/build/actions.js +252 -276
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +8 -16
- package/build/batch/create-batch.js.map +1 -1
- package/build/batch/default-processor.js +1 -1
- package/build/batch/default-processor.js.map +1 -1
- package/build/entities.js +16 -25
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +12 -17
- package/build/entity-provider.js.map +1 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js +2 -6
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +1 -2
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/hooks/use-entity-record.js +7 -11
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +3 -5
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js +1 -6
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/index.js +1 -7
- package/build/index.js.map +1 -1
- package/build/locks/actions.js +3 -4
- package/build/locks/actions.js.map +1 -1
- package/build/locks/reducer.js +1 -4
- package/build/locks/reducer.js.map +1 -1
- package/build/locks/selectors.js +3 -4
- package/build/locks/selectors.js.map +1 -1
- package/build/locks/utils.js +3 -5
- package/build/locks/utils.js.map +1 -1
- package/build/private-selectors.js +37 -0
- package/build/private-selectors.js.map +1 -0
- package/build/queried-data/actions.js +2 -5
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/reducer.js +17 -47
- package/build/queried-data/reducer.js.map +1 -1
- package/build/queried-data/selectors.js +4 -11
- package/build/queried-data/selectors.js.map +1 -1
- package/build/reducer.js +167 -194
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +175 -220
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +53 -61
- package/build/selectors.js.map +1 -1
- package/build/utils/forward-resolver.js +4 -11
- package/build/utils/forward-resolver.js.map +1 -1
- package/build/utils/on-sub-key.js +1 -3
- package/build/utils/on-sub-key.js.map +1 -1
- package/build-module/actions.js +251 -276
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +8 -16
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/batch/default-processor.js +1 -1
- package/build-module/batch/default-processor.js.map +1 -1
- package/build-module/entities.js +16 -25
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +12 -17
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +2 -6
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +1 -2
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +7 -11
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +3 -5
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js +1 -6
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/index.js +1 -7
- package/build-module/index.js.map +1 -1
- package/build-module/locks/actions.js +3 -4
- package/build-module/locks/actions.js.map +1 -1
- package/build-module/locks/reducer.js +1 -4
- package/build-module/locks/reducer.js.map +1 -1
- package/build-module/locks/selectors.js +3 -4
- package/build-module/locks/selectors.js.map +1 -1
- package/build-module/locks/utils.js +3 -5
- package/build-module/locks/utils.js.map +1 -1
- package/build-module/private-selectors.js +28 -0
- package/build-module/private-selectors.js.map +1 -0
- package/build-module/queried-data/actions.js +2 -5
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/reducer.js +17 -47
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/queried-data/selectors.js +4 -11
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/reducer.js +168 -194
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +175 -220
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +55 -61
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/forward-resolver.js +4 -11
- package/build-module/utils/forward-resolver.js.map +1 -1
- package/build-module/utils/on-sub-key.js +1 -3
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/build-types/actions.d.ts.map +1 -1
- package/build-types/private-selectors.d.ts +25 -0
- package/build-types/private-selectors.d.ts.map +1 -0
- package/build-types/reducer.d.ts +6 -2
- package/build-types/reducer.d.ts.map +1 -1
- package/build-types/selectors.d.ts +16 -4
- package/build-types/selectors.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/actions.js +9 -8
- package/src/index.js +0 -1
- package/src/private-selectors.ts +30 -0
- package/src/reducer.js +130 -104
- package/src/selectors.ts +33 -13
- package/src/test/reducer.js +89 -54
- package/src/test/selectors.js +8 -8
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -535,6 +535,8 @@ _Returns_
|
|
|
535
535
|
|
|
536
536
|
### getRedoEdit
|
|
537
537
|
|
|
538
|
+
> **Deprecated** since 6.3
|
|
539
|
+
|
|
538
540
|
Returns the next edit from the current undo offset for the entity records edits history, if any.
|
|
539
541
|
|
|
540
542
|
_Parameters_
|
|
@@ -578,6 +580,8 @@ _Returns_
|
|
|
578
580
|
|
|
579
581
|
### getUndoEdit
|
|
580
582
|
|
|
583
|
+
> **Deprecated** since 6.3
|
|
584
|
+
|
|
581
585
|
Returns the previous edit from the current undo offset for the entity records edits history, if any.
|
|
582
586
|
|
|
583
587
|
_Parameters_
|
package/build/actions.js
CHANGED
|
@@ -44,6 +44,8 @@ var _batch = require("./batch");
|
|
|
44
44
|
|
|
45
45
|
var _name = require("./name");
|
|
46
46
|
|
|
47
|
+
var _privateSelectors = require("./private-selectors");
|
|
48
|
+
|
|
47
49
|
/**
|
|
48
50
|
* External dependencies
|
|
49
51
|
*/
|
|
@@ -120,10 +122,7 @@ function addEntities(entities) {
|
|
|
120
122
|
*/
|
|
121
123
|
|
|
122
124
|
|
|
123
|
-
function receiveEntityRecords(kind, name, records, query) {
|
|
124
|
-
let invalidateCache = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
125
|
-
let edits = arguments.length > 5 ? arguments[5] : undefined;
|
|
126
|
-
|
|
125
|
+
function receiveEntityRecords(kind, name, records, query, invalidateCache = false, edits) {
|
|
127
126
|
// Auto drafts should not have titles, but some plugins rely on them so we can't filter this
|
|
128
127
|
// on the server.
|
|
129
128
|
if (kind === 'postType') {
|
|
@@ -297,71 +296,67 @@ function receiveEmbedPreview(url, preview) {
|
|
|
297
296
|
*/
|
|
298
297
|
|
|
299
298
|
|
|
300
|
-
const deleteEntityRecord =
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
311
|
-
let error;
|
|
312
|
-
let deletedRecord = false;
|
|
299
|
+
const deleteEntityRecord = (kind, name, recordId, query, {
|
|
300
|
+
__unstableFetch = _apiFetch.default,
|
|
301
|
+
throwOnError = false
|
|
302
|
+
} = {}) => async ({
|
|
303
|
+
dispatch
|
|
304
|
+
}) => {
|
|
305
|
+
const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind));
|
|
306
|
+
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
307
|
+
let error;
|
|
308
|
+
let deletedRecord = false;
|
|
313
309
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
310
|
+
if (!entityConfig || entityConfig?.__experimentalNoFetch) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'records', kind, name, recordId], {
|
|
315
|
+
exclusive: true
|
|
316
|
+
});
|
|
317
317
|
|
|
318
|
-
|
|
319
|
-
|
|
318
|
+
try {
|
|
319
|
+
dispatch({
|
|
320
|
+
type: 'DELETE_ENTITY_RECORD_START',
|
|
321
|
+
kind,
|
|
322
|
+
name,
|
|
323
|
+
recordId
|
|
320
324
|
});
|
|
325
|
+
let hasError = false;
|
|
321
326
|
|
|
322
327
|
try {
|
|
323
|
-
|
|
324
|
-
type: 'DELETE_ENTITY_RECORD_START',
|
|
325
|
-
kind,
|
|
326
|
-
name,
|
|
327
|
-
recordId
|
|
328
|
-
});
|
|
329
|
-
let hasError = false;
|
|
330
|
-
|
|
331
|
-
try {
|
|
332
|
-
let path = `${entityConfig.baseURL}/${recordId}`;
|
|
333
|
-
|
|
334
|
-
if (query) {
|
|
335
|
-
path = (0, _url.addQueryArgs)(path, query);
|
|
336
|
-
}
|
|
328
|
+
let path = `${entityConfig.baseURL}/${recordId}`;
|
|
337
329
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
method: 'DELETE'
|
|
341
|
-
});
|
|
342
|
-
await dispatch((0, _queriedData.removeItems)(kind, name, recordId, true));
|
|
343
|
-
} catch (_error) {
|
|
344
|
-
hasError = true;
|
|
345
|
-
error = _error;
|
|
330
|
+
if (query) {
|
|
331
|
+
path = (0, _url.addQueryArgs)(path, query);
|
|
346
332
|
}
|
|
347
333
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
name,
|
|
352
|
-
recordId,
|
|
353
|
-
error
|
|
334
|
+
deletedRecord = await __unstableFetch({
|
|
335
|
+
path,
|
|
336
|
+
method: 'DELETE'
|
|
354
337
|
});
|
|
338
|
+
await dispatch((0, _queriedData.removeItems)(kind, name, recordId, true));
|
|
339
|
+
} catch (_error) {
|
|
340
|
+
hasError = true;
|
|
341
|
+
error = _error;
|
|
342
|
+
}
|
|
355
343
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
344
|
+
dispatch({
|
|
345
|
+
type: 'DELETE_ENTITY_RECORD_FINISH',
|
|
346
|
+
kind,
|
|
347
|
+
name,
|
|
348
|
+
recordId,
|
|
349
|
+
error
|
|
350
|
+
});
|
|
359
351
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
dispatch.__unstableReleaseStoreLock(lock);
|
|
352
|
+
if (hasError && throwOnError) {
|
|
353
|
+
throw error;
|
|
363
354
|
}
|
|
364
|
-
|
|
355
|
+
|
|
356
|
+
return deletedRecord;
|
|
357
|
+
} finally {
|
|
358
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
359
|
+
}
|
|
365
360
|
};
|
|
366
361
|
/**
|
|
367
362
|
* Returns an action object that triggers an
|
|
@@ -380,56 +375,52 @@ const deleteEntityRecord = function (kind, name, recordId, query) {
|
|
|
380
375
|
|
|
381
376
|
exports.deleteEntityRecord = deleteEntityRecord;
|
|
382
377
|
|
|
383
|
-
const editEntityRecord =
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
dispatch
|
|
389
|
-
} = _ref2;
|
|
390
|
-
const entityConfig = select.getEntityConfig(kind, name);
|
|
391
|
-
|
|
392
|
-
if (!entityConfig) {
|
|
393
|
-
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
394
|
-
}
|
|
378
|
+
const editEntityRecord = (kind, name, recordId, edits, options = {}) => ({
|
|
379
|
+
select,
|
|
380
|
+
dispatch
|
|
381
|
+
}) => {
|
|
382
|
+
const entityConfig = select.getEntityConfig(kind, name);
|
|
395
383
|
|
|
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
|
-
meta: {
|
|
423
|
-
undo: !options.undoIgnore && { ...edit,
|
|
424
|
-
// Send the current values for things like the first undo stack entry.
|
|
425
|
-
edits: Object.keys(edits).reduce((acc, key) => {
|
|
426
|
-
acc[key] = editedRecord[key];
|
|
427
|
-
return acc;
|
|
428
|
-
}, {})
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
});
|
|
384
|
+
if (!entityConfig) {
|
|
385
|
+
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const {
|
|
389
|
+
transientEdits = {},
|
|
390
|
+
mergedEdits = {}
|
|
391
|
+
} = entityConfig;
|
|
392
|
+
const record = select.getRawEntityRecord(kind, name, recordId);
|
|
393
|
+
const editedRecord = select.getEditedEntityRecord(kind, name, recordId);
|
|
394
|
+
const edit = {
|
|
395
|
+
kind,
|
|
396
|
+
name,
|
|
397
|
+
recordId,
|
|
398
|
+
// Clear edits when they are equal to their persisted counterparts
|
|
399
|
+
// so that the property is not considered dirty.
|
|
400
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
401
|
+
const recordValue = record[key];
|
|
402
|
+
const editedRecordValue = editedRecord[key];
|
|
403
|
+
const value = mergedEdits[key] ? { ...editedRecordValue,
|
|
404
|
+
...edits[key]
|
|
405
|
+
} : edits[key];
|
|
406
|
+
acc[key] = (0, _es.default)(recordValue, value) ? undefined : value;
|
|
407
|
+
return acc;
|
|
408
|
+
}, {}),
|
|
409
|
+
transientEdits
|
|
432
410
|
};
|
|
411
|
+
dispatch({
|
|
412
|
+
type: 'EDIT_ENTITY_RECORD',
|
|
413
|
+
...edit,
|
|
414
|
+
meta: {
|
|
415
|
+
undo: !options.undoIgnore && { ...edit,
|
|
416
|
+
// Send the current values for things like the first undo stack entry.
|
|
417
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
418
|
+
acc[key] = editedRecord[key];
|
|
419
|
+
return acc;
|
|
420
|
+
}, {})
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
433
424
|
};
|
|
434
425
|
/**
|
|
435
426
|
* Action triggered to undo the last edit to
|
|
@@ -439,23 +430,20 @@ const editEntityRecord = function (kind, name, recordId, edits) {
|
|
|
439
430
|
|
|
440
431
|
exports.editEntityRecord = editEntityRecord;
|
|
441
432
|
|
|
442
|
-
const undo = () =>
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
const undoEdit = select.
|
|
433
|
+
const undo = () => ({
|
|
434
|
+
select,
|
|
435
|
+
dispatch
|
|
436
|
+
}) => {
|
|
437
|
+
// Todo: we shouldn't have to pass "root" here.
|
|
438
|
+
const undoEdit = select(state => (0, _privateSelectors.getUndoEdits)(state.root));
|
|
448
439
|
|
|
449
440
|
if (!undoEdit) {
|
|
450
441
|
return;
|
|
451
442
|
}
|
|
452
443
|
|
|
453
444
|
dispatch({
|
|
454
|
-
type: '
|
|
455
|
-
|
|
456
|
-
meta: {
|
|
457
|
-
isUndo: true
|
|
458
|
-
}
|
|
445
|
+
type: 'UNDO',
|
|
446
|
+
stackedEdits: undoEdit
|
|
459
447
|
});
|
|
460
448
|
};
|
|
461
449
|
/**
|
|
@@ -466,23 +454,20 @@ const undo = () => _ref3 => {
|
|
|
466
454
|
|
|
467
455
|
exports.undo = undo;
|
|
468
456
|
|
|
469
|
-
const redo = () =>
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
const redoEdit = select.
|
|
457
|
+
const redo = () => ({
|
|
458
|
+
select,
|
|
459
|
+
dispatch
|
|
460
|
+
}) => {
|
|
461
|
+
// Todo: we shouldn't have to pass "root" here.
|
|
462
|
+
const redoEdit = select(state => (0, _privateSelectors.getRedoEdits)(state.root));
|
|
475
463
|
|
|
476
464
|
if (!redoEdit) {
|
|
477
465
|
return;
|
|
478
466
|
}
|
|
479
467
|
|
|
480
468
|
dispatch({
|
|
481
|
-
type: '
|
|
482
|
-
|
|
483
|
-
meta: {
|
|
484
|
-
isRedo: true
|
|
485
|
-
}
|
|
469
|
+
type: 'REDO',
|
|
470
|
+
stackedEdits: redoEdit
|
|
486
471
|
});
|
|
487
472
|
};
|
|
488
473
|
/**
|
|
@@ -515,157 +500,153 @@ function __unstableCreateUndoLevel() {
|
|
|
515
500
|
*/
|
|
516
501
|
|
|
517
502
|
|
|
518
|
-
const saveEntityRecord =
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
503
|
+
const saveEntityRecord = (kind, name, record, {
|
|
504
|
+
isAutosave = false,
|
|
505
|
+
__unstableFetch = _apiFetch.default,
|
|
506
|
+
throwOnError = false
|
|
507
|
+
} = {}) => async ({
|
|
508
|
+
select,
|
|
509
|
+
resolveSelect,
|
|
510
|
+
dispatch
|
|
511
|
+
}) => {
|
|
512
|
+
const configs = await dispatch((0, _entities.getOrLoadEntitiesConfig)(kind));
|
|
513
|
+
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
514
|
+
|
|
515
|
+
if (!entityConfig || entityConfig?.__experimentalNoFetch) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const entityIdKey = entityConfig.key || _entities.DEFAULT_ENTITY_KEY;
|
|
520
|
+
const recordId = record[entityIdKey];
|
|
521
|
+
const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'records', kind, name, recordId || (0, _uuid.v4)()], {
|
|
522
|
+
exclusive: true
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
try {
|
|
526
|
+
// Evaluate optimized edits.
|
|
527
|
+
// (Function edits that should be evaluated on save to avoid expensive computations on every edit.)
|
|
528
|
+
for (const [key, value] of Object.entries(record)) {
|
|
529
|
+
if (typeof value === 'function') {
|
|
530
|
+
const evaluatedValue = value(select.getEditedEntityRecord(kind, name, recordId));
|
|
531
|
+
dispatch.editEntityRecord(kind, name, recordId, {
|
|
532
|
+
[key]: evaluatedValue
|
|
533
|
+
}, {
|
|
534
|
+
undoIgnore: true
|
|
535
|
+
});
|
|
536
|
+
record[key] = evaluatedValue;
|
|
537
|
+
}
|
|
535
538
|
}
|
|
536
539
|
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
540
|
+
dispatch({
|
|
541
|
+
type: 'SAVE_ENTITY_RECORD_START',
|
|
542
|
+
kind,
|
|
543
|
+
name,
|
|
544
|
+
recordId,
|
|
545
|
+
isAutosave
|
|
541
546
|
});
|
|
547
|
+
let updatedRecord;
|
|
548
|
+
let error;
|
|
549
|
+
let hasError = false;
|
|
542
550
|
|
|
543
551
|
try {
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
552
|
+
const path = `${entityConfig.baseURL}${recordId ? '/' + recordId : ''}`;
|
|
553
|
+
const persistedRecord = select.getRawEntityRecord(kind, name, recordId);
|
|
554
|
+
|
|
555
|
+
if (isAutosave) {
|
|
556
|
+
// Most of this autosave logic is very specific to posts.
|
|
557
|
+
// This is fine for now as it is the only supported autosave,
|
|
558
|
+
// but ideally this should all be handled in the back end,
|
|
559
|
+
// so the client just sends and receives objects.
|
|
560
|
+
const currentUser = select.getCurrentUser();
|
|
561
|
+
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
562
|
+
const autosavePost = await resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
563
|
+
// So we fallback to the previous autosave and then
|
|
564
|
+
// to the actual persisted entity if the edits don't
|
|
565
|
+
// have a value.
|
|
566
|
+
|
|
567
|
+
let data = { ...persistedRecord,
|
|
568
|
+
...autosavePost,
|
|
569
|
+
...record
|
|
570
|
+
};
|
|
571
|
+
data = Object.keys(data).reduce((acc, key) => {
|
|
572
|
+
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
573
|
+
acc[key] = data[key];
|
|
574
|
+
}
|
|
557
575
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
// Most of this autosave logic is very specific to posts.
|
|
575
|
-
// This is fine for now as it is the only supported autosave,
|
|
576
|
-
// but ideally this should all be handled in the back end,
|
|
577
|
-
// so the client just sends and receives objects.
|
|
578
|
-
const currentUser = select.getCurrentUser();
|
|
579
|
-
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
580
|
-
const autosavePost = await resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
581
|
-
// So we fallback to the previous autosave and then
|
|
582
|
-
// to the actual persisted entity if the edits don't
|
|
583
|
-
// have a value.
|
|
584
|
-
|
|
585
|
-
let data = { ...persistedRecord,
|
|
586
|
-
...autosavePost,
|
|
587
|
-
...record
|
|
576
|
+
return acc;
|
|
577
|
+
}, {
|
|
578
|
+
status: data.status === 'auto-draft' ? 'draft' : data.status
|
|
579
|
+
});
|
|
580
|
+
updatedRecord = await __unstableFetch({
|
|
581
|
+
path: `${path}/autosaves`,
|
|
582
|
+
method: 'POST',
|
|
583
|
+
data
|
|
584
|
+
}); // An autosave may be processed by the server as a regular save
|
|
585
|
+
// when its update is requested by the author and the post had
|
|
586
|
+
// draft or auto-draft status.
|
|
587
|
+
|
|
588
|
+
if (persistedRecord.id === updatedRecord.id) {
|
|
589
|
+
let newRecord = { ...persistedRecord,
|
|
590
|
+
...data,
|
|
591
|
+
...updatedRecord
|
|
588
592
|
};
|
|
589
|
-
|
|
593
|
+
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
594
|
+
// These properties are persisted in autosaves.
|
|
590
595
|
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
591
|
-
acc[key] =
|
|
596
|
+
acc[key] = newRecord[key];
|
|
597
|
+
} else if (key === 'status') {
|
|
598
|
+
// Status is only persisted in autosaves when going from
|
|
599
|
+
// "auto-draft" to "draft".
|
|
600
|
+
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
601
|
+
} else {
|
|
602
|
+
// These properties are not persisted in autosaves.
|
|
603
|
+
acc[key] = persistedRecord[key];
|
|
592
604
|
}
|
|
593
605
|
|
|
594
606
|
return acc;
|
|
595
|
-
}, {
|
|
596
|
-
|
|
597
|
-
});
|
|
598
|
-
updatedRecord = await __unstableFetch({
|
|
599
|
-
path: `${path}/autosaves`,
|
|
600
|
-
method: 'POST',
|
|
601
|
-
data
|
|
602
|
-
}); // An autosave may be processed by the server as a regular save
|
|
603
|
-
// when its update is requested by the author and the post had
|
|
604
|
-
// draft or auto-draft status.
|
|
605
|
-
|
|
606
|
-
if (persistedRecord.id === updatedRecord.id) {
|
|
607
|
-
let newRecord = { ...persistedRecord,
|
|
608
|
-
...data,
|
|
609
|
-
...updatedRecord
|
|
610
|
-
};
|
|
611
|
-
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
612
|
-
// These properties are persisted in autosaves.
|
|
613
|
-
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
614
|
-
acc[key] = newRecord[key];
|
|
615
|
-
} else if (key === 'status') {
|
|
616
|
-
// Status is only persisted in autosaves when going from
|
|
617
|
-
// "auto-draft" to "draft".
|
|
618
|
-
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
619
|
-
} else {
|
|
620
|
-
// These properties are not persisted in autosaves.
|
|
621
|
-
acc[key] = persistedRecord[key];
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
return acc;
|
|
625
|
-
}, {});
|
|
626
|
-
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
627
|
-
} else {
|
|
628
|
-
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
629
|
-
}
|
|
607
|
+
}, {});
|
|
608
|
+
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
630
609
|
} else {
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
if (entityConfig.__unstablePrePersist) {
|
|
634
|
-
edits = { ...edits,
|
|
635
|
-
...entityConfig.__unstablePrePersist(persistedRecord, edits)
|
|
636
|
-
};
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
updatedRecord = await __unstableFetch({
|
|
640
|
-
path,
|
|
641
|
-
method: recordId ? 'PUT' : 'POST',
|
|
642
|
-
data: edits
|
|
643
|
-
});
|
|
644
|
-
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
610
|
+
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
645
611
|
}
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
error = _error;
|
|
649
|
-
}
|
|
612
|
+
} else {
|
|
613
|
+
let edits = record;
|
|
650
614
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
error,
|
|
657
|
-
isAutosave
|
|
658
|
-
});
|
|
615
|
+
if (entityConfig.__unstablePrePersist) {
|
|
616
|
+
edits = { ...edits,
|
|
617
|
+
...entityConfig.__unstablePrePersist(persistedRecord, edits)
|
|
618
|
+
};
|
|
619
|
+
}
|
|
659
620
|
|
|
660
|
-
|
|
661
|
-
|
|
621
|
+
updatedRecord = await __unstableFetch({
|
|
622
|
+
path,
|
|
623
|
+
method: recordId ? 'PUT' : 'POST',
|
|
624
|
+
data: edits
|
|
625
|
+
});
|
|
626
|
+
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
662
627
|
}
|
|
628
|
+
} catch (_error) {
|
|
629
|
+
hasError = true;
|
|
630
|
+
error = _error;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
dispatch({
|
|
634
|
+
type: 'SAVE_ENTITY_RECORD_FINISH',
|
|
635
|
+
kind,
|
|
636
|
+
name,
|
|
637
|
+
recordId,
|
|
638
|
+
error,
|
|
639
|
+
isAutosave
|
|
640
|
+
});
|
|
663
641
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
dispatch.__unstableReleaseStoreLock(lock);
|
|
642
|
+
if (hasError && throwOnError) {
|
|
643
|
+
throw error;
|
|
667
644
|
}
|
|
668
|
-
|
|
645
|
+
|
|
646
|
+
return updatedRecord;
|
|
647
|
+
} finally {
|
|
648
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
649
|
+
}
|
|
669
650
|
};
|
|
670
651
|
/**
|
|
671
652
|
* Runs multiple core-data actions at the same time using one API request.
|
|
@@ -693,10 +674,9 @@ const saveEntityRecord = function (kind, name, record) {
|
|
|
693
674
|
|
|
694
675
|
exports.saveEntityRecord = saveEntityRecord;
|
|
695
676
|
|
|
696
|
-
const __experimentalBatch = requests => async
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
} = _ref6;
|
|
677
|
+
const __experimentalBatch = requests => async ({
|
|
678
|
+
dispatch
|
|
679
|
+
}) => {
|
|
700
680
|
const batch = (0, _batch.createBatch)();
|
|
701
681
|
const api = {
|
|
702
682
|
saveEntityRecord(kind, name, record, options) {
|
|
@@ -734,12 +714,10 @@ const __experimentalBatch = requests => async _ref6 => {
|
|
|
734
714
|
|
|
735
715
|
exports.__experimentalBatch = __experimentalBatch;
|
|
736
716
|
|
|
737
|
-
const saveEditedEntityRecord = (kind, name, recordId, options) => async
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
} = _ref7;
|
|
742
|
-
|
|
717
|
+
const saveEditedEntityRecord = (kind, name, recordId, options) => async ({
|
|
718
|
+
select,
|
|
719
|
+
dispatch
|
|
720
|
+
}) => {
|
|
743
721
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
744
722
|
return;
|
|
745
723
|
}
|
|
@@ -772,12 +750,10 @@ const saveEditedEntityRecord = (kind, name, recordId, options) => async _ref7 =>
|
|
|
772
750
|
|
|
773
751
|
exports.saveEditedEntityRecord = saveEditedEntityRecord;
|
|
774
752
|
|
|
775
|
-
const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
} = _ref8;
|
|
780
|
-
|
|
753
|
+
const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async ({
|
|
754
|
+
select,
|
|
755
|
+
dispatch
|
|
756
|
+
}) => {
|
|
781
757
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
782
758
|
return;
|
|
783
759
|
}
|