@wordpress/core-data 4.0.4 → 4.0.8
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 +2 -4
- package/build/actions.js +321 -247
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +17 -9
- package/build/batch/create-batch.js.map +1 -1
- package/build/entities.js +67 -14
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +18 -13
- package/build/entity-provider.js.map +1 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js +3 -1
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +3 -2
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/index.js +25 -5
- package/build/index.js.map +1 -1
- package/build/locks/actions.js +4 -3
- package/build/locks/actions.js.map +1 -1
- package/build/locks/reducer.js +4 -1
- package/build/locks/reducer.js.map +1 -1
- package/build/locks/selectors.js +4 -3
- package/build/locks/selectors.js.map +1 -1
- package/build/locks/utils.js +7 -5
- package/build/locks/utils.js.map +1 -1
- package/build/queried-data/actions.js +6 -3
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/get-query-parts.js +1 -1
- package/build/queried-data/reducer.js +17 -6
- package/build/queried-data/reducer.js.map +1 -1
- package/build/queried-data/selectors.js +2 -1
- package/build/queried-data/selectors.js.map +1 -1
- package/build/reducer.js +90 -43
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +216 -163
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +75 -22
- package/build/selectors.js.map +1 -1
- package/build/utils/forward-resolver.js +30 -0
- package/build/utils/forward-resolver.js.map +1 -0
- package/build/utils/index.js +9 -9
- package/build/utils/index.js.map +1 -1
- package/build/utils/on-sub-key.js +4 -2
- package/build/utils/on-sub-key.js.map +1 -1
- package/build-module/actions.js +306 -240
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +17 -9
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/entities.js +66 -13
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +17 -12
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +3 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +3 -2
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/index.js +21 -3
- package/build-module/index.js.map +1 -1
- package/build-module/locks/actions.js +4 -3
- package/build-module/locks/actions.js.map +1 -1
- package/build-module/locks/reducer.js +4 -1
- package/build-module/locks/reducer.js.map +1 -1
- package/build-module/locks/selectors.js +4 -3
- package/build-module/locks/selectors.js.map +1 -1
- package/build-module/locks/utils.js +5 -3
- package/build-module/locks/utils.js.map +1 -1
- package/build-module/queried-data/actions.js +5 -2
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/reducer.js +15 -4
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/queried-data/selectors.js +2 -1
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/reducer.js +80 -34
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +211 -161
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +44 -3
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/forward-resolver.js +22 -0
- package/build-module/utils/forward-resolver.js.map +1 -0
- package/build-module/utils/index.js +1 -1
- package/build-module/utils/index.js.map +1 -1
- package/build-module/utils/on-sub-key.js +3 -1
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/package.json +12 -12
- package/src/actions.js +51 -5
- package/src/entities.js +43 -6
- package/src/fetch/__experimental-fetch-url-data.js +1 -1
- package/src/reducer.js +14 -17
- package/src/resolvers.js +56 -19
- package/src/selectors.js +43 -4
- package/src/test/actions.js +89 -0
- package/src/test/selectors.js +56 -1
- package/src/utils/forward-resolver.js +14 -0
- package/src/utils/index.js +1 -1
- package/build/utils/if-not-resolved.js +0 -31
- package/build/utils/if-not-resolved.js.map +0 -1
- package/build-module/utils/if-not-resolved.js +0 -23
- package/build-module/utils/if-not-resolved.js.map +0 -1
- package/src/utils/if-not-resolved.js +0 -22
- package/src/utils/test/if-not-resolved.js +0 -76
package/build-module/actions.js
CHANGED
|
@@ -9,6 +9,7 @@ import { v4 as uuid } from 'uuid';
|
|
|
9
9
|
|
|
10
10
|
import apiFetch from '@wordpress/api-fetch';
|
|
11
11
|
import { addQueryArgs } from '@wordpress/url';
|
|
12
|
+
import deprecated from '@wordpress/deprecated';
|
|
12
13
|
/**
|
|
13
14
|
* Internal dependencies
|
|
14
15
|
*/
|
|
@@ -73,7 +74,10 @@ export function addEntities(entities) {
|
|
|
73
74
|
* @return {Object} Action object.
|
|
74
75
|
*/
|
|
75
76
|
|
|
76
|
-
export function receiveEntityRecords(kind, name, records, query
|
|
77
|
+
export function receiveEntityRecords(kind, name, records, query) {
|
|
78
|
+
let invalidateCache = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
|
|
79
|
+
let edits = arguments.length > 5 ? arguments[5] : undefined;
|
|
80
|
+
|
|
77
81
|
// Auto drafts should not have titles, but some plugins rely on them so we can't filter this
|
|
78
82
|
// on the server.
|
|
79
83
|
if (kind === 'postType') {
|
|
@@ -110,18 +114,50 @@ export function receiveCurrentTheme(currentTheme) {
|
|
|
110
114
|
currentTheme
|
|
111
115
|
};
|
|
112
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Returns an action object used in signalling that the current global styles id has been received.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} currentGlobalStylesId The current global styles id.
|
|
121
|
+
*
|
|
122
|
+
* @return {Object} Action object.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
export function __experimentalReceiveCurrentGlobalStylesId(currentGlobalStylesId) {
|
|
126
|
+
return {
|
|
127
|
+
type: 'RECEIVE_CURRENT_GLOBAL_STYLES_ID',
|
|
128
|
+
id: currentGlobalStylesId
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Returns an action object used in signalling that the theme base global styles have been received
|
|
133
|
+
*
|
|
134
|
+
* @param {string} stylesheet The theme's identifier
|
|
135
|
+
* @param {Object} globalStyles The global styles object.
|
|
136
|
+
*
|
|
137
|
+
* @return {Object} Action object.
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
export function __experimentalReceiveThemeBaseGlobalStyles(stylesheet, globalStyles) {
|
|
141
|
+
return {
|
|
142
|
+
type: 'RECEIVE_THEME_GLOBAL_STYLES',
|
|
143
|
+
stylesheet,
|
|
144
|
+
globalStyles
|
|
145
|
+
};
|
|
146
|
+
}
|
|
113
147
|
/**
|
|
114
148
|
* Returns an action object used in signalling that the index has been received.
|
|
115
149
|
*
|
|
116
|
-
* @
|
|
150
|
+
* @deprecated since WP 5.9, this is not useful anymore, use the selector direclty.
|
|
117
151
|
*
|
|
118
152
|
* @return {Object} Action object.
|
|
119
153
|
*/
|
|
120
154
|
|
|
121
|
-
export function receiveThemeSupports(
|
|
155
|
+
export function receiveThemeSupports() {
|
|
156
|
+
deprecated("wp.data.dispatch( 'core' ).receiveThemeSupports", {
|
|
157
|
+
since: '5.9'
|
|
158
|
+
});
|
|
122
159
|
return {
|
|
123
|
-
type: '
|
|
124
|
-
themeSupports
|
|
160
|
+
type: 'DO_NOTHING'
|
|
125
161
|
};
|
|
126
162
|
}
|
|
127
163
|
/**
|
|
@@ -155,62 +191,66 @@ export function receiveEmbedPreview(url, preview) {
|
|
|
155
191
|
* Must return a promise.
|
|
156
192
|
*/
|
|
157
193
|
|
|
158
|
-
export const deleteEntityRecord = (kind, name, recordId, query
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
194
|
+
export const deleteEntityRecord = function (kind, name, recordId, query) {
|
|
195
|
+
let {
|
|
196
|
+
__unstableFetch = apiFetch
|
|
197
|
+
} = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
198
|
+
return async _ref => {
|
|
199
|
+
let {
|
|
200
|
+
dispatch
|
|
201
|
+
} = _ref;
|
|
202
|
+
const entities = await dispatch(getKindEntities(kind));
|
|
203
|
+
const entity = find(entities, {
|
|
204
|
+
kind,
|
|
205
|
+
name
|
|
206
|
+
});
|
|
207
|
+
let error;
|
|
208
|
+
let deletedRecord = false;
|
|
174
209
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
210
|
+
if (!entity || entity !== null && entity !== void 0 && entity.__experimentalNoFetch) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
178
213
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
type: 'DELETE_ENTITY_RECORD_START',
|
|
182
|
-
kind,
|
|
183
|
-
name,
|
|
184
|
-
recordId
|
|
214
|
+
const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'data', kind, name, recordId], {
|
|
215
|
+
exclusive: true
|
|
185
216
|
});
|
|
186
217
|
|
|
187
218
|
try {
|
|
188
|
-
|
|
219
|
+
dispatch({
|
|
220
|
+
type: 'DELETE_ENTITY_RECORD_START',
|
|
221
|
+
kind,
|
|
222
|
+
name,
|
|
223
|
+
recordId
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
let path = `${entity.baseURL}/${recordId}`;
|
|
228
|
+
|
|
229
|
+
if (query) {
|
|
230
|
+
path = addQueryArgs(path, query);
|
|
231
|
+
}
|
|
189
232
|
|
|
190
|
-
|
|
191
|
-
|
|
233
|
+
deletedRecord = await __unstableFetch({
|
|
234
|
+
path,
|
|
235
|
+
method: 'DELETE'
|
|
236
|
+
});
|
|
237
|
+
await dispatch(removeItems(kind, name, recordId, true));
|
|
238
|
+
} catch (_error) {
|
|
239
|
+
error = _error;
|
|
192
240
|
}
|
|
193
241
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
242
|
+
dispatch({
|
|
243
|
+
type: 'DELETE_ENTITY_RECORD_FINISH',
|
|
244
|
+
kind,
|
|
245
|
+
name,
|
|
246
|
+
recordId,
|
|
247
|
+
error
|
|
197
248
|
});
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
249
|
+
return deletedRecord;
|
|
250
|
+
} finally {
|
|
251
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
201
252
|
}
|
|
202
|
-
|
|
203
|
-
dispatch({
|
|
204
|
-
type: 'DELETE_ENTITY_RECORD_FINISH',
|
|
205
|
-
kind,
|
|
206
|
-
name,
|
|
207
|
-
recordId,
|
|
208
|
-
error
|
|
209
|
-
});
|
|
210
|
-
return deletedRecord;
|
|
211
|
-
} finally {
|
|
212
|
-
dispatch.__unstableReleaseStoreLock(lock);
|
|
213
|
-
}
|
|
253
|
+
};
|
|
214
254
|
};
|
|
215
255
|
/**
|
|
216
256
|
* Returns an action object that triggers an
|
|
@@ -226,52 +266,56 @@ export const deleteEntityRecord = (kind, name, recordId, query, {
|
|
|
226
266
|
* @return {Object} Action object.
|
|
227
267
|
*/
|
|
228
268
|
|
|
229
|
-
export const editEntityRecord = (kind, name, recordId, edits
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
269
|
+
export const editEntityRecord = function (kind, name, recordId, edits) {
|
|
270
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
271
|
+
return _ref2 => {
|
|
272
|
+
let {
|
|
273
|
+
select,
|
|
274
|
+
dispatch
|
|
275
|
+
} = _ref2;
|
|
276
|
+
const entity = select.getEntity(kind, name);
|
|
277
|
+
|
|
278
|
+
if (!entity) {
|
|
279
|
+
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
280
|
+
}
|
|
238
281
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
282
|
+
const {
|
|
283
|
+
transientEdits = {},
|
|
284
|
+
mergedEdits = {}
|
|
285
|
+
} = entity;
|
|
286
|
+
const record = select.getRawEntityRecord(kind, name, recordId);
|
|
287
|
+
const editedRecord = select.getEditedEntityRecord(kind, name, recordId);
|
|
288
|
+
const edit = {
|
|
289
|
+
kind,
|
|
290
|
+
name,
|
|
291
|
+
recordId,
|
|
292
|
+
// Clear edits when they are equal to their persisted counterparts
|
|
293
|
+
// so that the property is not considered dirty.
|
|
294
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
295
|
+
const recordValue = record[key];
|
|
296
|
+
const editedRecordValue = editedRecord[key];
|
|
297
|
+
const value = mergedEdits[key] ? { ...editedRecordValue,
|
|
298
|
+
...edits[key]
|
|
299
|
+
} : edits[key];
|
|
300
|
+
acc[key] = isEqual(recordValue, value) ? undefined : value;
|
|
301
|
+
return acc;
|
|
302
|
+
}, {}),
|
|
303
|
+
transientEdits
|
|
304
|
+
};
|
|
305
|
+
dispatch({
|
|
306
|
+
type: 'EDIT_ENTITY_RECORD',
|
|
307
|
+
...edit,
|
|
308
|
+
meta: {
|
|
309
|
+
undo: !options.undoIgnore && { ...edit,
|
|
310
|
+
// Send the current values for things like the first undo stack entry.
|
|
311
|
+
edits: Object.keys(edits).reduce((acc, key) => {
|
|
312
|
+
acc[key] = editedRecord[key];
|
|
313
|
+
return acc;
|
|
314
|
+
}, {})
|
|
315
|
+
}
|
|
272
316
|
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
317
|
+
});
|
|
318
|
+
};
|
|
275
319
|
};
|
|
276
320
|
/**
|
|
277
321
|
* Action triggered to undo the last edit to
|
|
@@ -280,10 +324,11 @@ export const editEntityRecord = (kind, name, recordId, edits, options = {}) => (
|
|
|
280
324
|
* @return {undefined}
|
|
281
325
|
*/
|
|
282
326
|
|
|
283
|
-
export const undo = () =>
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
327
|
+
export const undo = () => _ref3 => {
|
|
328
|
+
let {
|
|
329
|
+
select,
|
|
330
|
+
dispatch
|
|
331
|
+
} = _ref3;
|
|
287
332
|
const undoEdit = select.getUndoEdit();
|
|
288
333
|
|
|
289
334
|
if (!undoEdit) {
|
|
@@ -305,10 +350,11 @@ export const undo = () => ({
|
|
|
305
350
|
* @return {undefined}
|
|
306
351
|
*/
|
|
307
352
|
|
|
308
|
-
export const redo = () =>
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
353
|
+
export const redo = () => _ref4 => {
|
|
354
|
+
let {
|
|
355
|
+
select,
|
|
356
|
+
dispatch
|
|
357
|
+
} = _ref4;
|
|
312
358
|
const redoEdit = select.getRedoEdit();
|
|
313
359
|
|
|
314
360
|
if (!redoEdit) {
|
|
@@ -347,148 +393,152 @@ export function __unstableCreateUndoLevel() {
|
|
|
347
393
|
* Must return a promise.
|
|
348
394
|
*/
|
|
349
395
|
|
|
350
|
-
export const saveEntityRecord = (kind, name, record
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const entityIdKey = entity.key || DEFAULT_ENTITY_KEY;
|
|
369
|
-
const recordId = record[entityIdKey];
|
|
370
|
-
const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'data', kind, name, recordId || uuid()], {
|
|
371
|
-
exclusive: true
|
|
372
|
-
});
|
|
396
|
+
export const saveEntityRecord = function (kind, name, record) {
|
|
397
|
+
let {
|
|
398
|
+
isAutosave = false,
|
|
399
|
+
__unstableFetch = apiFetch
|
|
400
|
+
} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
401
|
+
return async _ref5 => {
|
|
402
|
+
let {
|
|
403
|
+
select,
|
|
404
|
+
resolveSelect,
|
|
405
|
+
dispatch
|
|
406
|
+
} = _ref5;
|
|
407
|
+
const entities = await dispatch(getKindEntities(kind));
|
|
408
|
+
const entity = find(entities, {
|
|
409
|
+
kind,
|
|
410
|
+
name
|
|
411
|
+
});
|
|
373
412
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
// (Function edits that should be evaluated on save to avoid expensive computations on every edit.)
|
|
377
|
-
for (const [key, value] of Object.entries(record)) {
|
|
378
|
-
if (typeof value === 'function') {
|
|
379
|
-
const evaluatedValue = value(select.getEditedEntityRecord(kind, name, recordId));
|
|
380
|
-
dispatch.editEntityRecord(kind, name, recordId, {
|
|
381
|
-
[key]: evaluatedValue
|
|
382
|
-
}, {
|
|
383
|
-
undoIgnore: true
|
|
384
|
-
});
|
|
385
|
-
record[key] = evaluatedValue;
|
|
386
|
-
}
|
|
413
|
+
if (!entity || entity !== null && entity !== void 0 && entity.__experimentalNoFetch) {
|
|
414
|
+
return;
|
|
387
415
|
}
|
|
388
416
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
recordId,
|
|
394
|
-
isAutosave
|
|
417
|
+
const entityIdKey = entity.key || DEFAULT_ENTITY_KEY;
|
|
418
|
+
const recordId = record[entityIdKey];
|
|
419
|
+
const lock = await dispatch.__unstableAcquireStoreLock(STORE_NAME, ['entities', 'data', kind, name, recordId || uuid()], {
|
|
420
|
+
exclusive: true
|
|
395
421
|
});
|
|
396
|
-
let updatedRecord;
|
|
397
|
-
let error;
|
|
398
422
|
|
|
399
423
|
try {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
// have a value.
|
|
414
|
-
|
|
415
|
-
let data = { ...persistedRecord,
|
|
416
|
-
...autosavePost,
|
|
417
|
-
...record
|
|
418
|
-
};
|
|
419
|
-
data = Object.keys(data).reduce((acc, key) => {
|
|
420
|
-
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
421
|
-
acc[key] = data[key];
|
|
422
|
-
}
|
|
424
|
+
// Evaluate optimized edits.
|
|
425
|
+
// (Function edits that should be evaluated on save to avoid expensive computations on every edit.)
|
|
426
|
+
for (const [key, value] of Object.entries(record)) {
|
|
427
|
+
if (typeof value === 'function') {
|
|
428
|
+
const evaluatedValue = value(select.getEditedEntityRecord(kind, name, recordId));
|
|
429
|
+
dispatch.editEntityRecord(kind, name, recordId, {
|
|
430
|
+
[key]: evaluatedValue
|
|
431
|
+
}, {
|
|
432
|
+
undoIgnore: true
|
|
433
|
+
});
|
|
434
|
+
record[key] = evaluatedValue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
423
437
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
438
|
+
dispatch({
|
|
439
|
+
type: 'SAVE_ENTITY_RECORD_START',
|
|
440
|
+
kind,
|
|
441
|
+
name,
|
|
442
|
+
recordId,
|
|
443
|
+
isAutosave
|
|
444
|
+
});
|
|
445
|
+
let updatedRecord;
|
|
446
|
+
let error;
|
|
447
|
+
|
|
448
|
+
try {
|
|
449
|
+
const path = `${entity.baseURL}${recordId ? '/' + recordId : ''}`;
|
|
450
|
+
const persistedRecord = select.getRawEntityRecord(kind, name, recordId);
|
|
451
|
+
|
|
452
|
+
if (isAutosave) {
|
|
453
|
+
// Most of this autosave logic is very specific to posts.
|
|
454
|
+
// This is fine for now as it is the only supported autosave,
|
|
455
|
+
// but ideally this should all be handled in the back end,
|
|
456
|
+
// so the client just sends and receives objects.
|
|
457
|
+
const currentUser = select.getCurrentUser();
|
|
458
|
+
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
459
|
+
const autosavePost = resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
460
|
+
// So we fallback to the previous autosave and then
|
|
461
|
+
// to the actual persisted entity if the edits don't
|
|
462
|
+
// have a value.
|
|
463
|
+
|
|
464
|
+
let data = { ...persistedRecord,
|
|
465
|
+
...autosavePost,
|
|
466
|
+
...record
|
|
440
467
|
};
|
|
441
|
-
|
|
442
|
-
// These properties are persisted in autosaves.
|
|
468
|
+
data = Object.keys(data).reduce((acc, key) => {
|
|
443
469
|
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
444
|
-
acc[key] =
|
|
445
|
-
} else if (key === 'status') {
|
|
446
|
-
// Status is only persisted in autosaves when going from
|
|
447
|
-
// "auto-draft" to "draft".
|
|
448
|
-
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
449
|
-
} else {
|
|
450
|
-
// These properties are not persisted in autosaves.
|
|
451
|
-
acc[key] = persistedRecord[key];
|
|
470
|
+
acc[key] = data[key];
|
|
452
471
|
}
|
|
453
472
|
|
|
454
473
|
return acc;
|
|
455
|
-
}, {
|
|
456
|
-
|
|
474
|
+
}, {
|
|
475
|
+
status: data.status === 'auto-draft' ? 'draft' : data.status
|
|
476
|
+
});
|
|
477
|
+
updatedRecord = await __unstableFetch({
|
|
478
|
+
path: `${path}/autosaves`,
|
|
479
|
+
method: 'POST',
|
|
480
|
+
data
|
|
481
|
+
}); // An autosave may be processed by the server as a regular save
|
|
482
|
+
// when its update is requested by the author and the post had
|
|
483
|
+
// draft or auto-draft status.
|
|
484
|
+
|
|
485
|
+
if (persistedRecord.id === updatedRecord.id) {
|
|
486
|
+
let newRecord = { ...persistedRecord,
|
|
487
|
+
...data,
|
|
488
|
+
...updatedRecord
|
|
489
|
+
};
|
|
490
|
+
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
491
|
+
// These properties are persisted in autosaves.
|
|
492
|
+
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
493
|
+
acc[key] = newRecord[key];
|
|
494
|
+
} else if (key === 'status') {
|
|
495
|
+
// Status is only persisted in autosaves when going from
|
|
496
|
+
// "auto-draft" to "draft".
|
|
497
|
+
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
498
|
+
} else {
|
|
499
|
+
// These properties are not persisted in autosaves.
|
|
500
|
+
acc[key] = persistedRecord[key];
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return acc;
|
|
504
|
+
}, {});
|
|
505
|
+
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
506
|
+
} else {
|
|
507
|
+
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
508
|
+
}
|
|
457
509
|
} else {
|
|
458
|
-
|
|
459
|
-
}
|
|
460
|
-
} else {
|
|
461
|
-
let edits = record;
|
|
510
|
+
let edits = record;
|
|
462
511
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
512
|
+
if (entity.__unstablePrePersist) {
|
|
513
|
+
edits = { ...edits,
|
|
514
|
+
...entity.__unstablePrePersist(persistedRecord, edits)
|
|
515
|
+
};
|
|
516
|
+
}
|
|
468
517
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
518
|
+
updatedRecord = await __unstableFetch({
|
|
519
|
+
path,
|
|
520
|
+
method: recordId ? 'PUT' : 'POST',
|
|
521
|
+
data: edits
|
|
522
|
+
});
|
|
523
|
+
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
524
|
+
}
|
|
525
|
+
} catch (_error) {
|
|
526
|
+
error = _error;
|
|
475
527
|
}
|
|
476
|
-
} catch (_error) {
|
|
477
|
-
error = _error;
|
|
478
|
-
}
|
|
479
528
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
529
|
+
dispatch({
|
|
530
|
+
type: 'SAVE_ENTITY_RECORD_FINISH',
|
|
531
|
+
kind,
|
|
532
|
+
name,
|
|
533
|
+
recordId,
|
|
534
|
+
error,
|
|
535
|
+
isAutosave
|
|
536
|
+
});
|
|
537
|
+
return updatedRecord;
|
|
538
|
+
} finally {
|
|
539
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
492
542
|
};
|
|
493
543
|
/**
|
|
494
544
|
* Runs multiple core-data actions at the same time using one API request.
|
|
@@ -513,9 +563,10 @@ export const saveEntityRecord = (kind, name, record, {
|
|
|
513
563
|
* values of each function given in `requests`.
|
|
514
564
|
*/
|
|
515
565
|
|
|
516
|
-
export const __experimentalBatch = requests => async
|
|
517
|
-
|
|
518
|
-
|
|
566
|
+
export const __experimentalBatch = requests => async _ref6 => {
|
|
567
|
+
let {
|
|
568
|
+
dispatch
|
|
569
|
+
} = _ref6;
|
|
519
570
|
const batch = createBatch();
|
|
520
571
|
const api = {
|
|
521
572
|
saveEntityRecord(kind, name, record, options) {
|
|
@@ -550,17 +601,30 @@ export const __experimentalBatch = requests => async ({
|
|
|
550
601
|
* @param {Object} options Saving options.
|
|
551
602
|
*/
|
|
552
603
|
|
|
553
|
-
export const saveEditedEntityRecord = (kind, name, recordId, options) => async
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
604
|
+
export const saveEditedEntityRecord = (kind, name, recordId, options) => async _ref7 => {
|
|
605
|
+
let {
|
|
606
|
+
select,
|
|
607
|
+
dispatch
|
|
608
|
+
} = _ref7;
|
|
609
|
+
|
|
557
610
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
558
611
|
return;
|
|
559
612
|
}
|
|
560
613
|
|
|
614
|
+
const entities = await dispatch(getKindEntities(kind));
|
|
615
|
+
const entity = find(entities, {
|
|
616
|
+
kind,
|
|
617
|
+
name
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
if (!entity) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const entityIdKey = entity.key || DEFAULT_ENTITY_KEY;
|
|
561
625
|
const edits = select.getEntityRecordNonTransientEdits(kind, name, recordId);
|
|
562
626
|
const record = {
|
|
563
|
-
|
|
627
|
+
[entityIdKey]: recordId,
|
|
564
628
|
...edits
|
|
565
629
|
};
|
|
566
630
|
return await dispatch.saveEntityRecord(kind, name, record, options);
|
|
@@ -575,10 +639,12 @@ export const saveEditedEntityRecord = (kind, name, recordId, options) => async (
|
|
|
575
639
|
* @param {Object} options Saving options.
|
|
576
640
|
*/
|
|
577
641
|
|
|
578
|
-
export const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
642
|
+
export const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async _ref8 => {
|
|
643
|
+
let {
|
|
644
|
+
select,
|
|
645
|
+
dispatch
|
|
646
|
+
} = _ref8;
|
|
647
|
+
|
|
582
648
|
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
583
649
|
return;
|
|
584
650
|
}
|