@wordpress/core-data 4.0.1 → 4.0.5
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 +13 -7
- package/build/actions.js +178 -122
- package/build/actions.js.map +1 -1
- package/build/batch/default-processor.js +58 -27
- package/build/batch/default-processor.js.map +1 -1
- package/build/entities.js +70 -23
- package/build/entities.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +1 -1
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/index.js +9 -17
- package/build/index.js.map +1 -1
- package/build/locks/actions.js +17 -77
- package/build/locks/actions.js.map +1 -1
- package/build/locks/engine.js +77 -0
- package/build/locks/engine.js.map +1 -0
- package/build/locks/reducer.js +1 -5
- package/build/locks/reducer.js.map +1 -1
- package/build/locks/selectors.js +6 -6
- package/build/locks/selectors.js.map +1 -1
- package/build/queried-data/get-query-parts.js +9 -4
- package/build/queried-data/get-query-parts.js.map +1 -1
- package/build/queried-data/selectors.js +3 -9
- package/build/queried-data/selectors.js.map +1 -1
- package/build/reducer.js +17 -22
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +151 -97
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +79 -14
- package/build/selectors.js.map +1 -1
- package/build/utils/forward-resolver.js +23 -0
- package/build/utils/forward-resolver.js.map +1 -0
- package/build/utils/index.js +11 -3
- package/build/utils/index.js.map +1 -1
- package/build/utils/is-raw-attribute.js +19 -0
- package/build/utils/is-raw-attribute.js.map +1 -0
- package/build-module/actions.js +155 -112
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/default-processor.js +57 -27
- package/build-module/batch/default-processor.js.map +1 -1
- package/build-module/entities.js +65 -19
- package/build-module/entities.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/index.js +10 -14
- package/build-module/index.js.map +1 -1
- package/build-module/locks/actions.js +14 -68
- package/build-module/locks/actions.js.map +1 -1
- package/build-module/locks/engine.js +66 -0
- package/build-module/locks/engine.js.map +1 -0
- package/build-module/locks/reducer.js +1 -2
- package/build-module/locks/reducer.js.map +1 -1
- package/build-module/locks/selectors.js +4 -4
- package/build-module/locks/selectors.js.map +1 -1
- package/build-module/queried-data/get-query-parts.js +9 -4
- package/build-module/queried-data/get-query-parts.js.map +1 -1
- package/build-module/queried-data/selectors.js +3 -9
- package/build-module/queried-data/selectors.js.map +1 -1
- package/build-module/reducer.js +15 -19
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +123 -81
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +74 -13
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/forward-resolver.js +15 -0
- package/build-module/utils/forward-resolver.js.map +1 -0
- package/build-module/utils/index.js +2 -1
- package/build-module/utils/index.js.map +1 -1
- package/build-module/utils/is-raw-attribute.js +12 -0
- package/build-module/utils/is-raw-attribute.js.map +1 -0
- package/package.json +10 -11
- package/src/actions.js +163 -194
- package/src/batch/default-processor.js +57 -26
- package/src/batch/test/default-processor.js +53 -26
- package/src/entities.js +47 -19
- package/src/fetch/__experimental-fetch-url-data.js +1 -1
- package/src/index.js +7 -10
- package/src/locks/actions.js +10 -61
- package/src/locks/engine.js +43 -0
- package/src/locks/reducer.js +1 -3
- package/src/locks/selectors.js +4 -4
- package/src/locks/test/engine.js +135 -0
- package/src/locks/test/reducer.js +1 -1
- package/src/locks/test/selectors.js +105 -124
- package/src/queried-data/get-query-parts.js +11 -6
- package/src/queried-data/selectors.js +2 -9
- package/src/queried-data/test/get-query-parts.js +1 -1
- package/src/queried-data/test/selectors.js +1 -0
- package/src/reducer.js +14 -19
- package/src/resolvers.js +132 -120
- package/src/selectors.js +156 -44
- package/src/test/actions.js +330 -170
- package/src/test/entities.js +40 -26
- package/src/test/resolvers.js +270 -223
- package/src/test/selectors.js +127 -1
- package/src/utils/forward-resolver.js +14 -0
- package/src/utils/index.js +2 -1
- package/src/utils/is-raw-attribute.js +11 -0
- package/src/utils/test/is-raw-attribute.js +22 -0
- package/build/controls.js +0 -44
- package/build/controls.js.map +0 -1
- package/build/locks/index.js +0 -47
- package/build/locks/index.js.map +0 -1
- package/build/utils/if-not-resolved.js +0 -46
- package/build/utils/if-not-resolved.js.map +0 -1
- package/build-module/controls.js +0 -31
- package/build-module/controls.js.map +0 -1
- package/build-module/locks/index.js +0 -4
- package/build-module/locks/index.js.map +0 -1
- package/build-module/utils/if-not-resolved.js +0 -36
- package/build-module/utils/if-not-resolved.js.map +0 -1
- package/src/controls.js +0 -31
- package/src/locks/index.js +0 -3
- package/src/locks/test/actions.js +0 -307
- package/src/test/integration.js +0 -264
- package/src/utils/if-not-resolved.js +0 -40
- package/src/utils/test/if-not-resolved.js +0 -75
package/build/actions.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
3
5
|
Object.defineProperty(exports, "__esModule", {
|
|
4
6
|
value: true
|
|
5
7
|
});
|
|
@@ -8,41 +10,32 @@ exports.receiveCurrentUser = receiveCurrentUser;
|
|
|
8
10
|
exports.addEntities = addEntities;
|
|
9
11
|
exports.receiveEntityRecords = receiveEntityRecords;
|
|
10
12
|
exports.receiveCurrentTheme = receiveCurrentTheme;
|
|
13
|
+
exports.__experimentalReceiveCurrentGlobalStylesId = __experimentalReceiveCurrentGlobalStylesId;
|
|
14
|
+
exports.__experimentalReceiveThemeBaseGlobalStyles = __experimentalReceiveThemeBaseGlobalStyles;
|
|
11
15
|
exports.receiveThemeSupports = receiveThemeSupports;
|
|
12
16
|
exports.receiveEmbedPreview = receiveEmbedPreview;
|
|
13
|
-
exports.deleteEntityRecord = deleteEntityRecord;
|
|
14
|
-
exports.editEntityRecord = editEntityRecord;
|
|
15
|
-
exports.undo = undo;
|
|
16
|
-
exports.redo = redo;
|
|
17
17
|
exports.__unstableCreateUndoLevel = __unstableCreateUndoLevel;
|
|
18
|
-
exports.saveEntityRecord = saveEntityRecord;
|
|
19
|
-
exports.__experimentalBatch = __experimentalBatch;
|
|
20
|
-
exports.saveEditedEntityRecord = saveEditedEntityRecord;
|
|
21
|
-
exports.__experimentalSaveSpecifiedEntityEdits = __experimentalSaveSpecifiedEntityEdits;
|
|
22
18
|
exports.receiveUploadPermissions = receiveUploadPermissions;
|
|
23
19
|
exports.receiveUserPermission = receiveUserPermission;
|
|
24
20
|
exports.receiveAutosaves = receiveAutosaves;
|
|
21
|
+
exports.__experimentalSaveSpecifiedEntityEdits = exports.saveEditedEntityRecord = exports.__experimentalBatch = exports.saveEntityRecord = exports.redo = exports.undo = exports.editEntityRecord = exports.deleteEntityRecord = void 0;
|
|
25
22
|
|
|
26
23
|
var _lodash = require("lodash");
|
|
27
24
|
|
|
28
25
|
var _uuid = require("uuid");
|
|
29
26
|
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
var _dataControls = require("@wordpress/data-controls");
|
|
27
|
+
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
33
28
|
|
|
34
29
|
var _url = require("@wordpress/url");
|
|
35
30
|
|
|
31
|
+
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
|
|
32
|
+
|
|
36
33
|
var _queriedData = require("./queried-data");
|
|
37
34
|
|
|
38
35
|
var _entities = require("./entities");
|
|
39
36
|
|
|
40
|
-
var _locks = require("./locks");
|
|
41
|
-
|
|
42
37
|
var _batch = require("./batch");
|
|
43
38
|
|
|
44
|
-
var _controls = require("./controls");
|
|
45
|
-
|
|
46
39
|
var _name = require("./name");
|
|
47
40
|
|
|
48
41
|
/**
|
|
@@ -153,19 +146,53 @@ function receiveCurrentTheme(currentTheme) {
|
|
|
153
146
|
currentTheme
|
|
154
147
|
};
|
|
155
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns an action object used in signalling that the current global styles id has been received.
|
|
151
|
+
*
|
|
152
|
+
* @param {string} currentGlobalStylesId The current global styles id.
|
|
153
|
+
*
|
|
154
|
+
* @return {Object} Action object.
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
function __experimentalReceiveCurrentGlobalStylesId(currentGlobalStylesId) {
|
|
159
|
+
return {
|
|
160
|
+
type: 'RECEIVE_CURRENT_GLOBAL_STYLES_ID',
|
|
161
|
+
id: currentGlobalStylesId
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns an action object used in signalling that the theme base global styles have been received
|
|
166
|
+
*
|
|
167
|
+
* @param {string} stylesheet The theme's identifier
|
|
168
|
+
* @param {Object} globalStyles The global styles object.
|
|
169
|
+
*
|
|
170
|
+
* @return {Object} Action object.
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
function __experimentalReceiveThemeBaseGlobalStyles(stylesheet, globalStyles) {
|
|
175
|
+
return {
|
|
176
|
+
type: 'RECEIVE_THEME_GLOBAL_STYLES',
|
|
177
|
+
stylesheet,
|
|
178
|
+
globalStyles
|
|
179
|
+
};
|
|
180
|
+
}
|
|
156
181
|
/**
|
|
157
182
|
* Returns an action object used in signalling that the index has been received.
|
|
158
183
|
*
|
|
159
|
-
* @
|
|
184
|
+
* @deprecated since WP 5.9, this is not useful anymore, use the selector direclty.
|
|
160
185
|
*
|
|
161
186
|
* @return {Object} Action object.
|
|
162
187
|
*/
|
|
163
188
|
|
|
164
189
|
|
|
165
|
-
function receiveThemeSupports(
|
|
190
|
+
function receiveThemeSupports() {
|
|
191
|
+
(0, _deprecated.default)("wp.data.dispatch( 'core' ).receiveThemeSupports", {
|
|
192
|
+
since: '5.9'
|
|
193
|
+
});
|
|
166
194
|
return {
|
|
167
|
-
type: '
|
|
168
|
-
themeSupports
|
|
195
|
+
type: 'DO_NOTHING'
|
|
169
196
|
};
|
|
170
197
|
}
|
|
171
198
|
/**
|
|
@@ -197,14 +224,16 @@ function receiveEmbedPreview(url, preview) {
|
|
|
197
224
|
* @param {Object} [options] Delete options.
|
|
198
225
|
* @param {Function} [options.__unstableFetch] Internal use only. Function to
|
|
199
226
|
* call instead of `apiFetch()`.
|
|
200
|
-
* Must return a
|
|
227
|
+
* Must return a promise.
|
|
201
228
|
*/
|
|
202
229
|
|
|
203
230
|
|
|
204
|
-
|
|
205
|
-
__unstableFetch =
|
|
206
|
-
} = {}) {
|
|
207
|
-
|
|
231
|
+
const deleteEntityRecord = (kind, name, recordId, query, {
|
|
232
|
+
__unstableFetch = _apiFetch.default
|
|
233
|
+
} = {}) => async ({
|
|
234
|
+
dispatch
|
|
235
|
+
}) => {
|
|
236
|
+
const entities = await dispatch((0, _entities.getKindEntities)(kind));
|
|
208
237
|
const entity = (0, _lodash.find)(entities, {
|
|
209
238
|
kind,
|
|
210
239
|
name
|
|
@@ -212,21 +241,21 @@ function* deleteEntityRecord(kind, name, recordId, query, {
|
|
|
212
241
|
let error;
|
|
213
242
|
let deletedRecord = false;
|
|
214
243
|
|
|
215
|
-
if (!entity) {
|
|
244
|
+
if (!entity || entity !== null && entity !== void 0 && entity.__experimentalNoFetch) {
|
|
216
245
|
return;
|
|
217
246
|
}
|
|
218
247
|
|
|
219
|
-
const lock =
|
|
248
|
+
const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'data', kind, name, recordId], {
|
|
220
249
|
exclusive: true
|
|
221
250
|
});
|
|
222
251
|
|
|
223
252
|
try {
|
|
224
|
-
|
|
253
|
+
dispatch({
|
|
225
254
|
type: 'DELETE_ENTITY_RECORD_START',
|
|
226
255
|
kind,
|
|
227
256
|
name,
|
|
228
257
|
recordId
|
|
229
|
-
};
|
|
258
|
+
});
|
|
230
259
|
|
|
231
260
|
try {
|
|
232
261
|
let path = `${entity.baseURL}/${recordId}`;
|
|
@@ -235,34 +264,27 @@ function* deleteEntityRecord(kind, name, recordId, query, {
|
|
|
235
264
|
path = (0, _url.addQueryArgs)(path, query);
|
|
236
265
|
}
|
|
237
266
|
|
|
238
|
-
|
|
267
|
+
deletedRecord = await __unstableFetch({
|
|
239
268
|
path,
|
|
240
269
|
method: 'DELETE'
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
if (__unstableFetch) {
|
|
244
|
-
deletedRecord = yield (0, _dataControls.__unstableAwaitPromise)(__unstableFetch(options));
|
|
245
|
-
} else {
|
|
246
|
-
deletedRecord = yield (0, _dataControls.apiFetch)(options);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
yield (0, _queriedData.removeItems)(kind, name, recordId, true);
|
|
270
|
+
});
|
|
271
|
+
await dispatch((0, _queriedData.removeItems)(kind, name, recordId, true));
|
|
250
272
|
} catch (_error) {
|
|
251
273
|
error = _error;
|
|
252
274
|
}
|
|
253
275
|
|
|
254
|
-
|
|
276
|
+
dispatch({
|
|
255
277
|
type: 'DELETE_ENTITY_RECORD_FINISH',
|
|
256
278
|
kind,
|
|
257
279
|
name,
|
|
258
280
|
recordId,
|
|
259
281
|
error
|
|
260
|
-
};
|
|
282
|
+
});
|
|
261
283
|
return deletedRecord;
|
|
262
284
|
} finally {
|
|
263
|
-
|
|
285
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
264
286
|
}
|
|
265
|
-
}
|
|
287
|
+
};
|
|
266
288
|
/**
|
|
267
289
|
* Returns an action object that triggers an
|
|
268
290
|
* edit to an entity record.
|
|
@@ -278,8 +300,13 @@ function* deleteEntityRecord(kind, name, recordId, query, {
|
|
|
278
300
|
*/
|
|
279
301
|
|
|
280
302
|
|
|
281
|
-
|
|
282
|
-
|
|
303
|
+
exports.deleteEntityRecord = deleteEntityRecord;
|
|
304
|
+
|
|
305
|
+
const editEntityRecord = (kind, name, recordId, edits, options = {}) => ({
|
|
306
|
+
select,
|
|
307
|
+
dispatch
|
|
308
|
+
}) => {
|
|
309
|
+
const entity = select.getEntity(kind, name);
|
|
283
310
|
|
|
284
311
|
if (!entity) {
|
|
285
312
|
throw new Error(`The entity being edited (${kind}, ${name}) does not have a loaded config.`);
|
|
@@ -289,8 +316,8 @@ function* editEntityRecord(kind, name, recordId, edits, options = {}) {
|
|
|
289
316
|
transientEdits = {},
|
|
290
317
|
mergedEdits = {}
|
|
291
318
|
} = entity;
|
|
292
|
-
const record =
|
|
293
|
-
const editedRecord =
|
|
319
|
+
const record = select.getRawEntityRecord(kind, name, recordId);
|
|
320
|
+
const editedRecord = select.getEditedEntityRecord(kind, name, recordId);
|
|
294
321
|
const edit = {
|
|
295
322
|
kind,
|
|
296
323
|
name,
|
|
@@ -308,7 +335,7 @@ function* editEntityRecord(kind, name, recordId, edits, options = {}) {
|
|
|
308
335
|
}, {}),
|
|
309
336
|
transientEdits
|
|
310
337
|
};
|
|
311
|
-
|
|
338
|
+
dispatch({
|
|
312
339
|
type: 'EDIT_ENTITY_RECORD',
|
|
313
340
|
...edit,
|
|
314
341
|
meta: {
|
|
@@ -320,50 +347,64 @@ function* editEntityRecord(kind, name, recordId, edits, options = {}) {
|
|
|
320
347
|
}, {})
|
|
321
348
|
}
|
|
322
349
|
}
|
|
323
|
-
};
|
|
324
|
-
}
|
|
350
|
+
});
|
|
351
|
+
};
|
|
325
352
|
/**
|
|
326
353
|
* Action triggered to undo the last edit to
|
|
327
354
|
* an entity record, if any.
|
|
355
|
+
*
|
|
356
|
+
* @return {undefined}
|
|
328
357
|
*/
|
|
329
358
|
|
|
330
359
|
|
|
331
|
-
|
|
332
|
-
|
|
360
|
+
exports.editEntityRecord = editEntityRecord;
|
|
361
|
+
|
|
362
|
+
const undo = () => ({
|
|
363
|
+
select,
|
|
364
|
+
dispatch
|
|
365
|
+
}) => {
|
|
366
|
+
const undoEdit = select.getUndoEdit();
|
|
333
367
|
|
|
334
368
|
if (!undoEdit) {
|
|
335
369
|
return;
|
|
336
370
|
}
|
|
337
371
|
|
|
338
|
-
|
|
372
|
+
dispatch({
|
|
339
373
|
type: 'EDIT_ENTITY_RECORD',
|
|
340
374
|
...undoEdit,
|
|
341
375
|
meta: {
|
|
342
376
|
isUndo: true
|
|
343
377
|
}
|
|
344
|
-
};
|
|
345
|
-
}
|
|
378
|
+
});
|
|
379
|
+
};
|
|
346
380
|
/**
|
|
347
381
|
* Action triggered to redo the last undoed
|
|
348
382
|
* edit to an entity record, if any.
|
|
383
|
+
*
|
|
384
|
+
* @return {undefined}
|
|
349
385
|
*/
|
|
350
386
|
|
|
351
387
|
|
|
352
|
-
|
|
353
|
-
|
|
388
|
+
exports.undo = undo;
|
|
389
|
+
|
|
390
|
+
const redo = () => ({
|
|
391
|
+
select,
|
|
392
|
+
dispatch
|
|
393
|
+
}) => {
|
|
394
|
+
const redoEdit = select.getRedoEdit();
|
|
354
395
|
|
|
355
396
|
if (!redoEdit) {
|
|
356
397
|
return;
|
|
357
398
|
}
|
|
358
399
|
|
|
359
|
-
|
|
400
|
+
dispatch({
|
|
360
401
|
type: 'EDIT_ENTITY_RECORD',
|
|
361
402
|
...redoEdit,
|
|
362
403
|
meta: {
|
|
363
404
|
isRedo: true
|
|
364
405
|
}
|
|
365
|
-
};
|
|
366
|
-
}
|
|
406
|
+
});
|
|
407
|
+
};
|
|
367
408
|
/**
|
|
368
409
|
* Forces the creation of a new undo level.
|
|
369
410
|
*
|
|
@@ -371,6 +412,8 @@ function* redo() {
|
|
|
371
412
|
*/
|
|
372
413
|
|
|
373
414
|
|
|
415
|
+
exports.redo = redo;
|
|
416
|
+
|
|
374
417
|
function __unstableCreateUndoLevel() {
|
|
375
418
|
return {
|
|
376
419
|
type: 'CREATE_UNDO_LEVEL'
|
|
@@ -386,28 +429,31 @@ function __unstableCreateUndoLevel() {
|
|
|
386
429
|
* @param {boolean} [options.isAutosave=false] Whether this is an autosave.
|
|
387
430
|
* @param {Function} [options.__unstableFetch] Internal use only. Function to
|
|
388
431
|
* call instead of `apiFetch()`.
|
|
389
|
-
* Must return a
|
|
390
|
-
* descriptor.
|
|
432
|
+
* Must return a promise.
|
|
391
433
|
*/
|
|
392
434
|
|
|
393
435
|
|
|
394
|
-
|
|
436
|
+
const saveEntityRecord = (kind, name, record, {
|
|
395
437
|
isAutosave = false,
|
|
396
|
-
__unstableFetch =
|
|
397
|
-
} = {}) {
|
|
398
|
-
|
|
438
|
+
__unstableFetch = _apiFetch.default
|
|
439
|
+
} = {}) => async ({
|
|
440
|
+
select,
|
|
441
|
+
resolveSelect,
|
|
442
|
+
dispatch
|
|
443
|
+
}) => {
|
|
444
|
+
const entities = await dispatch((0, _entities.getKindEntities)(kind));
|
|
399
445
|
const entity = (0, _lodash.find)(entities, {
|
|
400
446
|
kind,
|
|
401
447
|
name
|
|
402
448
|
});
|
|
403
449
|
|
|
404
|
-
if (!entity) {
|
|
450
|
+
if (!entity || entity !== null && entity !== void 0 && entity.__experimentalNoFetch) {
|
|
405
451
|
return;
|
|
406
452
|
}
|
|
407
453
|
|
|
408
454
|
const entityIdKey = entity.key || _entities.DEFAULT_ENTITY_KEY;
|
|
409
455
|
const recordId = record[entityIdKey];
|
|
410
|
-
const lock =
|
|
456
|
+
const lock = await dispatch.__unstableAcquireStoreLock(_name.STORE_NAME, ['entities', 'data', kind, name, recordId || (0, _uuid.v4)()], {
|
|
411
457
|
exclusive: true
|
|
412
458
|
});
|
|
413
459
|
|
|
@@ -416,8 +462,8 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
416
462
|
// (Function edits that should be evaluated on save to avoid expensive computations on every edit.)
|
|
417
463
|
for (const [key, value] of Object.entries(record)) {
|
|
418
464
|
if (typeof value === 'function') {
|
|
419
|
-
const evaluatedValue = value(
|
|
420
|
-
|
|
465
|
+
const evaluatedValue = value(select.getEditedEntityRecord(kind, name, recordId));
|
|
466
|
+
dispatch.editEntityRecord(kind, name, recordId, {
|
|
421
467
|
[key]: evaluatedValue
|
|
422
468
|
}, {
|
|
423
469
|
undoIgnore: true
|
|
@@ -426,28 +472,28 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
426
472
|
}
|
|
427
473
|
}
|
|
428
474
|
|
|
429
|
-
|
|
475
|
+
dispatch({
|
|
430
476
|
type: 'SAVE_ENTITY_RECORD_START',
|
|
431
477
|
kind,
|
|
432
478
|
name,
|
|
433
479
|
recordId,
|
|
434
480
|
isAutosave
|
|
435
|
-
};
|
|
481
|
+
});
|
|
436
482
|
let updatedRecord;
|
|
437
483
|
let error;
|
|
438
484
|
|
|
439
485
|
try {
|
|
440
486
|
const path = `${entity.baseURL}${recordId ? '/' + recordId : ''}`;
|
|
441
|
-
const persistedRecord =
|
|
487
|
+
const persistedRecord = select.getRawEntityRecord(kind, name, recordId);
|
|
442
488
|
|
|
443
489
|
if (isAutosave) {
|
|
444
490
|
// Most of this autosave logic is very specific to posts.
|
|
445
491
|
// This is fine for now as it is the only supported autosave,
|
|
446
492
|
// but ideally this should all be handled in the back end,
|
|
447
493
|
// so the client just sends and receives objects.
|
|
448
|
-
const currentUser =
|
|
494
|
+
const currentUser = select.getCurrentUser();
|
|
449
495
|
const currentUserId = currentUser ? currentUser.id : undefined;
|
|
450
|
-
const autosavePost =
|
|
496
|
+
const autosavePost = resolveSelect.getAutosave(persistedRecord.type, persistedRecord.id, currentUserId); // Autosaves need all expected fields to be present.
|
|
451
497
|
// So we fallback to the previous autosave and then
|
|
452
498
|
// to the actual persisted entity if the edits don't
|
|
453
499
|
// have a value.
|
|
@@ -458,29 +504,21 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
458
504
|
};
|
|
459
505
|
data = Object.keys(data).reduce((acc, key) => {
|
|
460
506
|
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
461
|
-
|
|
462
|
-
acc[key] = (0, _lodash.get)(data[key], 'raw', data[key]);
|
|
507
|
+
acc[key] = data[key];
|
|
463
508
|
}
|
|
464
509
|
|
|
465
510
|
return acc;
|
|
466
511
|
}, {
|
|
467
512
|
status: data.status === 'auto-draft' ? 'draft' : data.status
|
|
468
513
|
});
|
|
469
|
-
|
|
514
|
+
updatedRecord = await __unstableFetch({
|
|
470
515
|
path: `${path}/autosaves`,
|
|
471
516
|
method: 'POST',
|
|
472
517
|
data
|
|
473
|
-
};
|
|
474
|
-
|
|
475
|
-
if (__unstableFetch) {
|
|
476
|
-
updatedRecord = yield (0, _dataControls.__unstableAwaitPromise)(__unstableFetch(options));
|
|
477
|
-
} else {
|
|
478
|
-
updatedRecord = yield (0, _dataControls.apiFetch)(options);
|
|
479
|
-
} // An autosave may be processed by the server as a regular save
|
|
518
|
+
}); // An autosave may be processed by the server as a regular save
|
|
480
519
|
// when its update is requested by the author and the post had
|
|
481
520
|
// draft or auto-draft status.
|
|
482
521
|
|
|
483
|
-
|
|
484
522
|
if (persistedRecord.id === updatedRecord.id) {
|
|
485
523
|
let newRecord = { ...persistedRecord,
|
|
486
524
|
...data,
|
|
@@ -489,22 +527,21 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
489
527
|
newRecord = Object.keys(newRecord).reduce((acc, key) => {
|
|
490
528
|
// These properties are persisted in autosaves.
|
|
491
529
|
if (['title', 'excerpt', 'content'].includes(key)) {
|
|
492
|
-
|
|
493
|
-
acc[key] = (0, _lodash.get)(newRecord[key], 'raw', newRecord[key]);
|
|
530
|
+
acc[key] = newRecord[key];
|
|
494
531
|
} else if (key === 'status') {
|
|
495
532
|
// Status is only persisted in autosaves when going from
|
|
496
533
|
// "auto-draft" to "draft".
|
|
497
534
|
acc[key] = persistedRecord.status === 'auto-draft' && newRecord.status === 'draft' ? newRecord.status : persistedRecord.status;
|
|
498
535
|
} else {
|
|
499
536
|
// These properties are not persisted in autosaves.
|
|
500
|
-
acc[key] =
|
|
537
|
+
acc[key] = persistedRecord[key];
|
|
501
538
|
}
|
|
502
539
|
|
|
503
540
|
return acc;
|
|
504
541
|
}, {});
|
|
505
|
-
|
|
542
|
+
dispatch.receiveEntityRecords(kind, name, newRecord, undefined, true);
|
|
506
543
|
} else {
|
|
507
|
-
|
|
544
|
+
dispatch.receiveAutosaves(persistedRecord.id, updatedRecord);
|
|
508
545
|
}
|
|
509
546
|
} else {
|
|
510
547
|
let edits = record;
|
|
@@ -515,37 +552,30 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
515
552
|
};
|
|
516
553
|
}
|
|
517
554
|
|
|
518
|
-
|
|
555
|
+
updatedRecord = await __unstableFetch({
|
|
519
556
|
path,
|
|
520
557
|
method: recordId ? 'PUT' : 'POST',
|
|
521
558
|
data: edits
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
if (__unstableFetch) {
|
|
525
|
-
updatedRecord = yield (0, _dataControls.__unstableAwaitPromise)(__unstableFetch(options));
|
|
526
|
-
} else {
|
|
527
|
-
updatedRecord = yield (0, _dataControls.apiFetch)(options);
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
yield receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
559
|
+
});
|
|
560
|
+
dispatch.receiveEntityRecords(kind, name, updatedRecord, undefined, true, edits);
|
|
531
561
|
}
|
|
532
562
|
} catch (_error) {
|
|
533
563
|
error = _error;
|
|
534
564
|
}
|
|
535
565
|
|
|
536
|
-
|
|
566
|
+
dispatch({
|
|
537
567
|
type: 'SAVE_ENTITY_RECORD_FINISH',
|
|
538
568
|
kind,
|
|
539
569
|
name,
|
|
540
570
|
recordId,
|
|
541
571
|
error,
|
|
542
572
|
isAutosave
|
|
543
|
-
};
|
|
573
|
+
});
|
|
544
574
|
return updatedRecord;
|
|
545
575
|
} finally {
|
|
546
|
-
|
|
576
|
+
dispatch.__unstableReleaseStoreLock(lock);
|
|
547
577
|
}
|
|
548
|
-
}
|
|
578
|
+
};
|
|
549
579
|
/**
|
|
550
580
|
* Runs multiple core-data actions at the same time using one API request.
|
|
551
581
|
*
|
|
@@ -570,33 +600,36 @@ function* saveEntityRecord(kind, name, record, {
|
|
|
570
600
|
*/
|
|
571
601
|
|
|
572
602
|
|
|
573
|
-
|
|
603
|
+
exports.saveEntityRecord = saveEntityRecord;
|
|
604
|
+
|
|
605
|
+
const __experimentalBatch = requests => async ({
|
|
606
|
+
dispatch
|
|
607
|
+
}) => {
|
|
574
608
|
const batch = (0, _batch.createBatch)();
|
|
575
|
-
const dispatch = yield (0, _controls.getDispatch)();
|
|
576
609
|
const api = {
|
|
577
610
|
saveEntityRecord(kind, name, record, options) {
|
|
578
|
-
return batch.add(add => dispatch
|
|
611
|
+
return batch.add(add => dispatch.saveEntityRecord(kind, name, record, { ...options,
|
|
579
612
|
__unstableFetch: add
|
|
580
613
|
}));
|
|
581
614
|
},
|
|
582
615
|
|
|
583
616
|
saveEditedEntityRecord(kind, name, recordId, options) {
|
|
584
|
-
return batch.add(add => dispatch
|
|
617
|
+
return batch.add(add => dispatch.saveEditedEntityRecord(kind, name, recordId, { ...options,
|
|
585
618
|
__unstableFetch: add
|
|
586
619
|
}));
|
|
587
620
|
},
|
|
588
621
|
|
|
589
622
|
deleteEntityRecord(kind, name, recordId, query, options) {
|
|
590
|
-
return batch.add(add => dispatch
|
|
623
|
+
return batch.add(add => dispatch.deleteEntityRecord(kind, name, recordId, query, { ...options,
|
|
591
624
|
__unstableFetch: add
|
|
592
625
|
}));
|
|
593
626
|
}
|
|
594
627
|
|
|
595
628
|
};
|
|
596
629
|
const resultPromises = requests.map(request => request(api));
|
|
597
|
-
const [, ...results] =
|
|
630
|
+
const [, ...results] = await Promise.all([batch.run(), ...resultPromises]);
|
|
598
631
|
return results;
|
|
599
|
-
}
|
|
632
|
+
};
|
|
600
633
|
/**
|
|
601
634
|
* Action triggered to save an entity record's edits.
|
|
602
635
|
*
|
|
@@ -607,18 +640,34 @@ function* __experimentalBatch(requests) {
|
|
|
607
640
|
*/
|
|
608
641
|
|
|
609
642
|
|
|
610
|
-
|
|
611
|
-
|
|
643
|
+
exports.__experimentalBatch = __experimentalBatch;
|
|
644
|
+
|
|
645
|
+
const saveEditedEntityRecord = (kind, name, recordId, options) => async ({
|
|
646
|
+
select,
|
|
647
|
+
dispatch
|
|
648
|
+
}) => {
|
|
649
|
+
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
612
650
|
return;
|
|
613
651
|
}
|
|
614
652
|
|
|
615
|
-
const
|
|
653
|
+
const entities = await dispatch((0, _entities.getKindEntities)(kind));
|
|
654
|
+
const entity = (0, _lodash.find)(entities, {
|
|
655
|
+
kind,
|
|
656
|
+
name
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
if (!entity) {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const entityIdKey = entity.key || _entities.DEFAULT_ENTITY_KEY;
|
|
664
|
+
const edits = select.getEntityRecordNonTransientEdits(kind, name, recordId);
|
|
616
665
|
const record = {
|
|
617
|
-
|
|
666
|
+
[entityIdKey]: recordId,
|
|
618
667
|
...edits
|
|
619
668
|
};
|
|
620
|
-
return
|
|
621
|
-
}
|
|
669
|
+
return await dispatch.saveEntityRecord(kind, name, record, options);
|
|
670
|
+
};
|
|
622
671
|
/**
|
|
623
672
|
* Action triggered to save only specified properties for the entity.
|
|
624
673
|
*
|
|
@@ -630,12 +679,17 @@ function* saveEditedEntityRecord(kind, name, recordId, options) {
|
|
|
630
679
|
*/
|
|
631
680
|
|
|
632
681
|
|
|
633
|
-
|
|
634
|
-
|
|
682
|
+
exports.saveEditedEntityRecord = saveEditedEntityRecord;
|
|
683
|
+
|
|
684
|
+
const __experimentalSaveSpecifiedEntityEdits = (kind, name, recordId, itemsToSave, options) => async ({
|
|
685
|
+
select,
|
|
686
|
+
dispatch
|
|
687
|
+
}) => {
|
|
688
|
+
if (!select.hasEditsForEntityRecord(kind, name, recordId)) {
|
|
635
689
|
return;
|
|
636
690
|
}
|
|
637
691
|
|
|
638
|
-
const edits =
|
|
692
|
+
const edits = select.getEntityRecordNonTransientEdits(kind, name, recordId);
|
|
639
693
|
const editsToSave = {};
|
|
640
694
|
|
|
641
695
|
for (const edit in edits) {
|
|
@@ -644,8 +698,8 @@ function* __experimentalSaveSpecifiedEntityEdits(kind, name, recordId, itemsToSa
|
|
|
644
698
|
}
|
|
645
699
|
}
|
|
646
700
|
|
|
647
|
-
return
|
|
648
|
-
}
|
|
701
|
+
return await dispatch.saveEntityRecord(kind, name, editsToSave, options);
|
|
702
|
+
};
|
|
649
703
|
/**
|
|
650
704
|
* Returns an action object used in signalling that Upload permissions have been received.
|
|
651
705
|
*
|
|
@@ -655,6 +709,8 @@ function* __experimentalSaveSpecifiedEntityEdits(kind, name, recordId, itemsToSa
|
|
|
655
709
|
*/
|
|
656
710
|
|
|
657
711
|
|
|
712
|
+
exports.__experimentalSaveSpecifiedEntityEdits = __experimentalSaveSpecifiedEntityEdits;
|
|
713
|
+
|
|
658
714
|
function receiveUploadPermissions(hasUploadPermissions) {
|
|
659
715
|
return {
|
|
660
716
|
type: 'RECEIVE_USER_PERMISSION',
|