@wordpress/core-data 6.10.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 +4 -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 +13 -13
- 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/build-module/actions.js
CHANGED
|
@@ -18,6 +18,7 @@ import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
|
|
|
18
18
|
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
|
|
19
19
|
import { createBatch } from './batch';
|
|
20
20
|
import { STORE_NAME } from './name';
|
|
21
|
+
import { getUndoEdits, getRedoEdits } from './private-selectors';
|
|
21
22
|
/**
|
|
22
23
|
* Returns an action object used in signalling that authors have been received.
|
|
23
24
|
* Ignored from documentation as it's internal to the data store.
|
|
@@ -80,10 +81,7 @@ export function addEntities(entities) {
|
|
|
80
81
|
* @return {Object} Action object.
|
|
81
82
|
*/
|
|
82
83
|
|
|
83
|
-
export function receiveEntityRecords(kind, name, records, query) {
|
|
84
|
-
let invalidateCache = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
85
|
-
let edits = arguments.length > 5 ? arguments[5] : undefined;
|
|
86
|
-
|
|
84
|
+
export function receiveEntityRecords(kind, name, records, query, invalidateCache = false, edits) {
|
|
87
85
|
// Auto drafts should not have titles, but some plugins rely on them so we can't filter this
|
|
88
86
|
// on the server.
|
|
89
87
|
if (kind === 'postType') {
|
|
@@ -249,71 +247,67 @@ export function receiveEmbedPreview(url, preview) {
|
|
|
249
247
|
* the exceptions. Defaults to false.
|
|
250
248
|
*/
|
|
251
249
|
|
|
252
|
-
export const deleteEntityRecord =
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
263
|
-
let error;
|
|
264
|
-
let deletedRecord = false;
|
|
250
|
+
export const deleteEntityRecord = (kind, name, recordId, query, {
|
|
251
|
+
__unstableFetch = apiFetch,
|
|
252
|
+
throwOnError = false
|
|
253
|
+
} = {}) => async ({
|
|
254
|
+
dispatch
|
|
255
|
+
}) => {
|
|
256
|
+
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
257
|
+
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
258
|
+
let error;
|
|
259
|
+
let deletedRecord = false;
|
|
265
260
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
261
|
+
if (!entityConfig || entityConfig?.__experimentalNoFetch) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
269
264
|
|
|
270
|
-
|
|
271
|
-
|
|
265
|
+
const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'records', kind, name, recordId], {
|
|
266
|
+
exclusive: true
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
try {
|
|
270
|
+
dispatch({
|
|
271
|
+
type: 'DELETE_ENTITY_RECORD_START',
|
|
272
|
+
kind,
|
|
273
|
+
name,
|
|
274
|
+
recordId
|
|
272
275
|
});
|
|
276
|
+
let hasError = false;
|
|
273
277
|
|
|
274
278
|
try {
|
|
275
|
-
|
|
276
|
-
type: 'DELETE_ENTITY_RECORD_START',
|
|
277
|
-
kind,
|
|
278
|
-
name,
|
|
279
|
-
recordId
|
|
280
|
-
});
|
|
281
|
-
let hasError = false;
|
|
279
|
+
let path = `${entityConfig.baseURL}/${recordId}`;
|
|
282
280
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (query) {
|
|
287
|
-
path = addQueryArgs(path, query);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
deletedRecord = await __unstableFetch({
|
|
291
|
-
path,
|
|
292
|
-
method: 'DELETE'
|
|
293
|
-
});
|
|
294
|
-
await dispatch(removeItems(kind, name, recordId, true));
|
|
295
|
-
} catch (_error) {
|
|
296
|
-
hasError = true;
|
|
297
|
-
error = _error;
|
|
281
|
+
if (query) {
|
|
282
|
+
path = addQueryArgs(path, query);
|
|
298
283
|
}
|
|
299
284
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
name,
|
|
304
|
-
recordId,
|
|
305
|
-
error
|
|
285
|
+
deletedRecord = await __unstableFetch({
|
|
286
|
+
path,
|
|
287
|
+
method: 'DELETE'
|
|
306
288
|
});
|
|
289
|
+
await dispatch(removeItems(kind, name, recordId, true));
|
|
290
|
+
} catch (_error) {
|
|
291
|
+
hasError = true;
|
|
292
|
+
error = _error;
|
|
293
|
+
}
|
|
307
294
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
295
|
+
dispatch({
|
|
296
|
+
type: 'DELETE_ENTITY_RECORD_FINISH',
|
|
297
|
+
kind,
|
|
298
|
+
name,
|
|
299
|
+
recordId,
|
|
300
|
+
error
|
|
301
|
+
});
|
|
311
302
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
dispatch.__unstableReleaseStoreLock(lock);
|
|
303
|
+
if (hasError && throwOnError) {
|
|
304
|
+
throw error;
|
|
315
305
|
}
|
|
316
|
-
|
|
306
|
+
|
|
307
|
+
return deletedRecord;
|
|
308
|
+
} finally {
|
|
309
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
310
|
+
}
|
|
317
311
|
};
|
|
318
312
|
/**
|
|
319
313
|
* Returns an action object that triggers an
|
|
@@ -329,79 +323,72 @@ export const deleteEntityRecord = function (kind, name, recordId, query) {
|
|
|
329
323
|
* @return {Object} Action object.
|
|
330
324
|
*/
|
|
331
325
|
|
|
332
|
-
export const editEntityRecord =
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
dispatch
|
|
338
|
-
} = _ref2;
|
|
339
|
-
const entityConfig = select.getEntityConfig(kind, name);
|
|
340
|
-
|
|
341
|
-
if (!entityConfig) {
|
|
342
|
-
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
343
|
-
}
|
|
326
|
+
export const editEntityRecord = (kind, name, recordId, edits, options = {}) => ({
|
|
327
|
+
select,
|
|
328
|
+
dispatch
|
|
329
|
+
}) => {
|
|
330
|
+
const entityConfig = select.getEntityConfig(kind, name);
|
|
344
331
|
|
|
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
|
-
meta: {
|
|
372
|
-
undo: !options.undoIgnore && { ...edit,
|
|
373
|
-
// Send the current values for things like the first undo stack entry.
|
|
374
|
-
edits: Object.keys(edits).reduce((acc, key) => {
|
|
375
|
-
acc[key] = editedRecord[key];
|
|
376
|
-
return acc;
|
|
377
|
-
}, {})
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
});
|
|
332
|
+
if (!entityConfig) {
|
|
333
|
+
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const {
|
|
337
|
+
transientEdits = {},
|
|
338
|
+
mergedEdits = {}
|
|
339
|
+
} = entityConfig;
|
|
340
|
+
const record = select.getRawEntityRecord(kind, name, recordId);
|
|
341
|
+
const editedRecord = select.getEditedEntityRecord(kind, name, recordId);
|
|
342
|
+
const edit = {
|
|
343
|
+
kind,
|
|
344
|
+
name,
|
|
345
|
+
recordId,
|
|
346
|
+
// Clear edits when they are equal to their persisted counterparts
|
|
347
|
+
// so that the property is not considered dirty.
|
|
348
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
349
|
+
const recordValue = record[key];
|
|
350
|
+
const editedRecordValue = editedRecord[key];
|
|
351
|
+
const value = mergedEdits[key] ? { ...editedRecordValue,
|
|
352
|
+
...edits[key]
|
|
353
|
+
} : edits[key];
|
|
354
|
+
acc[key] = fastDeepEqual(recordValue, value) ? undefined : value;
|
|
355
|
+
return acc;
|
|
356
|
+
}, {}),
|
|
357
|
+
transientEdits
|
|
381
358
|
};
|
|
359
|
+
dispatch({
|
|
360
|
+
type: 'EDIT_ENTITY_RECORD',
|
|
361
|
+
...edit,
|
|
362
|
+
meta: {
|
|
363
|
+
undo: !options.undoIgnore && { ...edit,
|
|
364
|
+
// Send the current values for things like the first undo stack entry.
|
|
365
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
366
|
+
acc[key] = editedRecord[key];
|
|
367
|
+
return acc;
|
|
368
|
+
}, {})
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
});
|
|
382
372
|
};
|
|
383
373
|
/**
|
|
384
374
|
* Action triggered to undo the last edit to
|
|
385
375
|
* an entity record, if any.
|
|
386
376
|
*/
|
|
387
377
|
|
|
388
|
-
export const undo = () =>
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const undoEdit = select.
|
|
378
|
+
export const undo = () => ({
|
|
379
|
+
select,
|
|
380
|
+
dispatch
|
|
381
|
+
}) => {
|
|
382
|
+
// Todo: we shouldn't have to pass "root" here.
|
|
383
|
+
const undoEdit = select(state => getUndoEdits(state.root));
|
|
394
384
|
|
|
395
385
|
if (!undoEdit) {
|
|
396
386
|
return;
|
|
397
387
|
}
|
|
398
388
|
|
|
399
389
|
dispatch({
|
|
400
|
-
type: '
|
|
401
|
-
|
|
402
|
-
meta: {
|
|
403
|
-
isUndo: true
|
|
404
|
-
}
|
|
390
|
+
type: 'UNDO',
|
|
391
|
+
stackedEdits: undoEdit
|
|
405
392
|
});
|
|
406
393
|
};
|
|
407
394
|
/**
|
|
@@ -409,23 +396,20 @@ export const undo = () => _ref3 => {
|
|
|
409
396
|
* edit to an entity record, if any.
|
|
410
397
|
*/
|
|
411
398
|
|
|
412
|
-
export const redo = () =>
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const redoEdit = select.
|
|
399
|
+
export const redo = () => ({
|
|
400
|
+
select,
|
|
401
|
+
dispatch
|
|
402
|
+
}) => {
|
|
403
|
+
// Todo: we shouldn't have to pass "root" here.
|
|
404
|
+
const redoEdit = select(state => getRedoEdits(state.root));
|
|
418
405
|
|
|
419
406
|
if (!redoEdit) {
|
|
420
407
|
return;
|
|
421
408
|
}
|
|
422
409
|
|
|
423
410
|
dispatch({
|
|
424
|
-
type: '
|
|
425
|
-
|
|
426
|
-
meta: {
|
|
427
|
-
isRedo: true
|
|
428
|
-
}
|
|
411
|
+
type: 'REDO',
|
|
412
|
+
stackedEdits: redoEdit
|
|
429
413
|
});
|
|
430
414
|
};
|
|
431
415
|
/**
|
|
@@ -454,157 +438,153 @@ export function __unstableCreateUndoLevel() {
|
|
|
454
438
|
* the exceptions. Defaults to false.
|
|
455
439
|
*/
|
|
456
440
|
|
|
457
|
-
export const saveEntityRecord =
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
441
|
+
export const saveEntityRecord = (kind, name, record, {
|
|
442
|
+
isAutosave = false,
|
|
443
|
+
__unstableFetch = apiFetch,
|
|
444
|
+
throwOnError = false
|
|
445
|
+
} = {}) => async ({
|
|
446
|
+
select,
|
|
447
|
+
resolveSelect,
|
|
448
|
+
dispatch
|
|
449
|
+
}) => {
|
|
450
|
+
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
451
|
+
const entityConfig = configs.find(config => config.kind === kind && config.name === name);
|
|
452
|
+
|
|
453
|
+
if (!entityConfig || entityConfig?.__experimentalNoFetch) {
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const entityIdKey = entityConfig.key || DEFAULT_ENTITY_KEY;
|
|
458
|
+
const recordId = record[entityIdKey];
|
|
459
|
+
const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'records', kind, name, recordId || uuid()], {
|
|
460
|
+
exclusive: true
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
try {
|
|
464
|
+
// Evaluate optimized edits.
|
|
465
|
+
// (Function edits that should be evaluated on save to avoid expensive computations on every edit.)
|
|
466
|
+
for (const [key, value] of Object.entries(record)) {
|
|
467
|
+
if (typeof value === 'function') {
|
|
468
|
+
const evaluatedValue = value(select.getEditedEntityRecord(kind, name, recordId));
|
|
469
|
+
dispatch.editEntityRecord(kind, name, recordId, {
|
|
470
|
+
[key]: evaluatedValue
|
|
471
|
+
}, {
|
|
472
|
+
undoIgnore: true
|
|
473
|
+
});
|
|
474
|
+
record[key] = evaluatedValue;
|
|
475
|
+
}
|
|
474
476
|
}
|
|
475
477
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
478
|
+
dispatch({
|
|
479
|
+
type: 'SAVE_ENTITY_RECORD_START',
|
|
480
|
+
kind,
|
|
481
|
+
name,
|
|
482
|
+
recordId,
|
|
483
|
+
isAutosave
|
|
480
484
|
});
|
|
485
|
+
let updatedRecord;
|
|
486
|
+
let error;
|
|
487
|
+
let hasError = false;
|
|
481
488
|
|
|
482
489
|
try {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
490
|
+
const path = `${entityConfig.baseURL}${recordId ? '/' + recordId : ''}`;
|
|
491
|
+
const persistedRecord = select.getRawEntityRecord(kind, name, recordId);
|
|
492
|
+
|
|
493
|
+
if (isAutosave) {
|
|
494
|
+
// Most of this autosave logic is very specific to posts.
|
|
495
|
+
// This is fine for now as it is the only supported autosave,
|
|
496
|
+
// but ideally this should all be handled in the back end,
|
|
497
|
+
// so the client just sends and receives objects.
|
|
498
|
+
const currentUser = select.getCurrentUser();
|
|
499
|
+
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
500
|
+
const autosavePost = await resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
501
|
+
// So we fallback to the previous autosave and then
|
|
502
|
+
// to the actual persisted entity if the edits don't
|
|
503
|
+
// have a value.
|
|
504
|
+
|
|
505
|
+
let data = { ...persistedRecord,
|
|
506
|
+
...autosavePost,
|
|
507
|
+
...record
|
|
508
|
+
};
|
|
509
|
+
data = Object.keys(data).reduce((acc, key) => {
|
|
510
|
+
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
511
|
+
acc[key] = data[key];
|
|
512
|
+
}
|
|
496
513
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
// Most of this autosave logic is very specific to posts.
|
|
514
|
-
// This is fine for now as it is the only supported autosave,
|
|
515
|
-
// but ideally this should all be handled in the back end,
|
|
516
|
-
// so the client just sends and receives objects.
|
|
517
|
-
const currentUser = select.getCurrentUser();
|
|
518
|
-
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
519
|
-
const autosavePost = await resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
520
|
-
// So we fallback to the previous autosave and then
|
|
521
|
-
// to the actual persisted entity if the edits don't
|
|
522
|
-
// have a value.
|
|
523
|
-
|
|
524
|
-
let data = { ...persistedRecord,
|
|
525
|
-
...autosavePost,
|
|
526
|
-
...record
|
|
514
|
+
return acc;
|
|
515
|
+
}, {
|
|
516
|
+
status: data.status === 'auto-draft' ? 'draft' : data.status
|
|
517
|
+
});
|
|
518
|
+
updatedRecord = await __unstableFetch({
|
|
519
|
+
path: `${path}/autosaves`,
|
|
520
|
+
method: 'POST',
|
|
521
|
+
data
|
|
522
|
+
}); // An autosave may be processed by the server as a regular save
|
|
523
|
+
// when its update is requested by the author and the post had
|
|
524
|
+
// draft or auto-draft status.
|
|
525
|
+
|
|
526
|
+
if (persistedRecord.id === updatedRecord.id) {
|
|
527
|
+
let newRecord = { ...persistedRecord,
|
|
528
|
+
...data,
|
|
529
|
+
...updatedRecord
|
|
527
530
|
};
|
|
528
|
-
|
|
531
|
+
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
532
|
+
// These properties are persisted in autosaves.
|
|
529
533
|
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
530
|
-
acc[key] =
|
|
534
|
+
acc[key] = newRecord[key];
|
|
535
|
+
} else if (key === 'status') {
|
|
536
|
+
// Status is only persisted in autosaves when going from
|
|
537
|
+
// "auto-draft" to "draft".
|
|
538
|
+
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
539
|
+
} else {
|
|
540
|
+
// These properties are not persisted in autosaves.
|
|
541
|
+
acc[key] = persistedRecord[key];
|
|
531
542
|
}
|
|
532
543
|
|
|
533
544
|
return acc;
|
|
534
|
-
}, {
|
|
535
|
-
|
|
536
|
-
});
|
|
537
|
-
updatedRecord = await __unstableFetch({
|
|
538
|
-
path: `${path}/autosaves`,
|
|
539
|
-
method: 'POST',
|
|
540
|
-
data
|
|
541
|
-
}); // An autosave may be processed by the server as a regular save
|
|
542
|
-
// when its update is requested by the author and the post had
|
|
543
|
-
// draft or auto-draft status.
|
|
544
|
-
|
|
545
|
-
if (persistedRecord.id === updatedRecord.id) {
|
|
546
|
-
let newRecord = { ...persistedRecord,
|
|
547
|
-
...data,
|
|
548
|
-
...updatedRecord
|
|
549
|
-
};
|
|
550
|
-
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
551
|
-
// These properties are persisted in autosaves.
|
|
552
|
-
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
553
|
-
acc[key] = newRecord[key];
|
|
554
|
-
} else if (key === 'status') {
|
|
555
|
-
// Status is only persisted in autosaves when going from
|
|
556
|
-
// "auto-draft" to "draft".
|
|
557
|
-
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
558
|
-
} else {
|
|
559
|
-
// These properties are not persisted in autosaves.
|
|
560
|
-
acc[key] = persistedRecord[key];
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
return acc;
|
|
564
|
-
}, {});
|
|
565
|
-
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
566
|
-
} else {
|
|
567
|
-
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
568
|
-
}
|
|
545
|
+
}, {});
|
|
546
|
+
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
569
547
|
} else {
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (entityConfig.__unstablePrePersist) {
|
|
573
|
-
edits = { ...edits,
|
|
574
|
-
...entityConfig.__unstablePrePersist(persistedRecord, edits)
|
|
575
|
-
};
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
updatedRecord = await __unstableFetch({
|
|
579
|
-
path,
|
|
580
|
-
method: recordId ? 'PUT' : 'POST',
|
|
581
|
-
data: edits
|
|
582
|
-
});
|
|
583
|
-
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
548
|
+
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
584
549
|
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
error = _error;
|
|
588
|
-
}
|
|
550
|
+
} else {
|
|
551
|
+
let edits = record;
|
|
589
552
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
error,
|
|
596
|
-
isAutosave
|
|
597
|
-
});
|
|
553
|
+
if (entityConfig.__unstablePrePersist) {
|
|
554
|
+
edits = { ...edits,
|
|
555
|
+
...entityConfig.__unstablePrePersist(persistedRecord, edits)
|
|
556
|
+
};
|
|
557
|
+
}
|
|
598
558
|
|
|
599
|
-
|
|
600
|
-
|
|
559
|
+
updatedRecord = await __unstableFetch({
|
|
560
|
+
path,
|
|
561
|
+
method: recordId ? 'PUT' : 'POST',
|
|
562
|
+
data: edits
|
|
563
|
+
});
|
|
564
|
+
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
601
565
|
}
|
|
566
|
+
} catch (_error) {
|
|
567
|
+
hasError = true;
|
|
568
|
+
error = _error;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
dispatch({
|
|
572
|
+
type: 'SAVE_ENTITY_RECORD_FINISH',
|
|
573
|
+
kind,
|
|
574
|
+
name,
|
|
575
|
+
recordId,
|
|
576
|
+
error,
|
|
577
|
+
isAutosave
|
|
578
|
+
});
|
|
602
579
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
dispatch.__unstableReleaseStoreLock(lock);
|
|
580
|
+
if (hasError && throwOnError) {
|
|
581
|
+
throw error;
|
|
606
582
|
}
|
|
607
|
-
|
|
583
|
+
|
|
584
|
+
return updatedRecord;
|
|
585
|
+
} finally {
|
|
586
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
587
|
+
}
|
|
608
588
|
};
|
|
609
589
|
/**
|
|
610
590
|
* Runs multiple core-data actions at the same time using one API request.
|
|
@@ -629,10 +609,9 @@ export const saveEntityRecord = function (kind, name, record) {
|
|
|
629
609
|
* values of each function given in `requests`.
|
|
630
610
|
*/
|
|
631
611
|
|
|
632
|
-
export const __experimentalBatch = requests => async
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
} = _ref6;
|
|
612
|
+
export const __experimentalBatch = requests => async ({
|
|
613
|
+
dispatch
|
|
614
|
+
}) => {
|
|
636
615
|
const batch = createBatch();
|
|
637
616
|
const api = {
|
|
638
617
|
saveEntityRecord(kind, name, record, options) {
|
|
@@ -667,12 +646,10 @@ export const __experimentalBatch = requests => async _ref6 => {
|
|
|
667
646
|
* @param {Object} options Saving options.
|
|
668
647
|
*/
|
|
669
648
|
|
|
670
|
-
export const saveEditedEntityRecord = (kind, name, recordId, options) => async
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
} = _ref7;
|
|
675
|
-
|
|
649
|
+
export const saveEditedEntityRecord = (kind, name, recordId, options) => async ({
|
|
650
|
+
select,
|
|
651
|
+
dispatch
|
|
652
|
+
}) => {
|
|
676
653
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
677
654
|
return;
|
|
678
655
|
}
|
|
@@ -702,12 +679,10 @@ export const saveEditedEntityRecord = (kind, name, recordId, options) => async _
|
|
|
702
679
|
* @param {Object} options Saving options.
|
|
703
680
|
*/
|
|
704
681
|
|
|
705
|
-
export const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
} = _ref8;
|
|
710
|
-
|
|
682
|
+
export const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async ({
|
|
683
|
+
select,
|
|
684
|
+
dispatch
|
|
685
|
+
}) => {
|
|
711
686
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
712
687
|
return;
|
|
713
688
|
}
|