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