@wordpress/core-data 4.2.0-next.e230fbab09.0 → 4.3.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 +7 -0
- package/README.md +41 -8
- package/build/actions.js +61 -41
- package/build/actions.js.map +1 -1
- package/build/entities.js +36 -23
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +46 -44
- package/build/entity-provider.js.map +1 -1
- package/build/entity-types/{base-entity-types.js → base-entity-records.js} +9 -9
- package/build/entity-types/base-entity-records.js.map +1 -0
- package/build/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/build/fetch/__experimental-fetch-link-suggestions.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/hooks/use-entity-record.js +15 -5
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +17 -5
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/index.js +9 -21
- package/build/index.js.map +1 -1
- package/build/queried-data/actions.js +4 -4
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/get-query-parts.js +7 -3
- package/build/queried-data/get-query-parts.js.map +1 -1
- package/build/queried-data/reducer.js +8 -6
- package/build/queried-data/reducer.js.map +1 -1
- package/build/reducer.js +16 -16
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +17 -23
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +92 -51
- package/build/selectors.js.map +1 -1
- package/build/utils/conservative-map-item.js +1 -1
- package/build/utils/conservative-map-item.js.map +1 -1
- package/build/utils/is-raw-attribute.js +1 -1
- package/build/utils/is-raw-attribute.js.map +1 -1
- package/build-module/actions.js +62 -42
- package/build-module/actions.js.map +1 -1
- package/build-module/entities.js +32 -19
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +47 -45
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/entity-types/{base-entity-types.js → base-entity-records.js} +7 -7
- package/build-module/entity-types/base-entity-records.js.map +1 -0
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/build-module/fetch/__experimental-fetch-link-suggestions.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/hooks/use-entity-record.js +15 -5
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +17 -5
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/index.js +10 -22
- package/build-module/index.js.map +1 -1
- package/build-module/queried-data/actions.js +4 -4
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/get-query-parts.js +7 -3
- package/build-module/queried-data/get-query-parts.js.map +1 -1
- package/build-module/queried-data/reducer.js +8 -6
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/reducer.js +17 -17
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +18 -24
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +88 -51
- package/build-module/selectors.js.map +1 -1
- package/build-module/utils/conservative-map-item.js +1 -1
- package/build-module/utils/conservative-map-item.js.map +1 -1
- package/build-module/utils/is-raw-attribute.js +1 -1
- package/build-module/utils/is-raw-attribute.js.map +1 -1
- package/package.json +11 -11
- package/src/actions.js +62 -42
- package/src/entities.js +43 -17
- package/src/entity-provider.js +50 -44
- package/src/entity-types/attachment.ts +5 -5
- package/src/entity-types/{base-entity-types.ts → base-entity-records.ts} +5 -5
- package/src/entity-types/comment.ts +5 -5
- package/src/entity-types/helpers.ts +1 -1
- package/src/entity-types/index.ts +4 -4
- package/src/entity-types/menu-location.ts +5 -5
- package/src/entity-types/nav-menu-item.ts +10 -5
- package/src/entity-types/nav-menu.ts +5 -5
- package/src/entity-types/navigation-area.ts +5 -5
- package/src/entity-types/page.ts +5 -5
- package/src/entity-types/plugin.ts +10 -5
- package/src/entity-types/post.ts +5 -5
- package/src/entity-types/settings.ts +10 -5
- package/src/entity-types/sidebar.ts +6 -7
- package/src/entity-types/taxonomy.ts +5 -5
- package/src/entity-types/theme.ts +5 -5
- package/src/entity-types/type.ts +5 -5
- package/src/entity-types/user.ts +10 -5
- package/src/entity-types/widget-type.ts +5 -5
- package/src/entity-types/widget.ts +5 -5
- package/src/entity-types/wp-template-part.ts +5 -5
- package/src/entity-types/wp-template.ts +5 -5
- package/src/fetch/__experimental-fetch-link-suggestions.js +1 -1
- package/src/fetch/__experimental-fetch-url-data.js +1 -0
- package/src/hooks/use-entity-record.ts +19 -8
- package/src/hooks/use-entity-records.ts +26 -9
- package/src/index.js +10 -15
- package/src/locks/test/selectors.js +4 -4
- package/src/queried-data/actions.js +4 -4
- package/src/queried-data/get-query-parts.js +5 -5
- package/src/queried-data/reducer.js +6 -6
- package/src/reducer.js +17 -17
- package/src/resolvers.js +25 -30
- package/src/selectors.js +92 -53
- package/src/test/actions.js +138 -33
- package/src/test/entities.js +11 -11
- package/src/test/reducer.js +4 -4
- package/src/test/resolvers.js +5 -5
- package/src/test/selectors.js +22 -22
- package/src/utils/conservative-map-item.js +1 -1
- package/src/utils/is-raw-attribute.js +1 -1
- package/src/utils/test/is-raw-attribute.js +4 -4
- package/build/entity-types/base-entity-types.js.map +0 -1
- package/build-module/entity-types/base-entity-types.js.map +0 -1
package/src/reducer.js
CHANGED
|
@@ -14,7 +14,7 @@ import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
|
14
14
|
*/
|
|
15
15
|
import { ifMatchingAction, replaceAction } from './utils';
|
|
16
16
|
import { reducer as queriedDataReducer } from './queried-data';
|
|
17
|
-
import {
|
|
17
|
+
import { rootEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Reducer managing terms state. Keyed by taxonomy slug, the value is either
|
|
@@ -105,10 +105,10 @@ export function taxonomies( state = [], action ) {
|
|
|
105
105
|
/**
|
|
106
106
|
* Reducer managing the current theme.
|
|
107
107
|
*
|
|
108
|
-
* @param {string} state Current state.
|
|
109
|
-
* @param {Object}
|
|
108
|
+
* @param {string|undefined} state Current state.
|
|
109
|
+
* @param {Object} action Dispatched action.
|
|
110
110
|
*
|
|
111
|
-
* @return {string} Updated state.
|
|
111
|
+
* @return {string|undefined} Updated state.
|
|
112
112
|
*/
|
|
113
113
|
export function currentTheme( state = undefined, action ) {
|
|
114
114
|
switch ( action.type ) {
|
|
@@ -122,10 +122,10 @@ export function currentTheme( state = undefined, action ) {
|
|
|
122
122
|
/**
|
|
123
123
|
* Reducer managing the current global styles id.
|
|
124
124
|
*
|
|
125
|
-
* @param {string} state Current state.
|
|
126
|
-
* @param {Object}
|
|
125
|
+
* @param {string|undefined} state Current state.
|
|
126
|
+
* @param {Object} action Dispatched action.
|
|
127
127
|
*
|
|
128
|
-
* @return {string} Updated state.
|
|
128
|
+
* @return {string|undefined} Updated state.
|
|
129
129
|
*/
|
|
130
130
|
export function currentGlobalStylesId( state = undefined, action ) {
|
|
131
131
|
switch ( action.type ) {
|
|
@@ -139,10 +139,10 @@ export function currentGlobalStylesId( state = undefined, action ) {
|
|
|
139
139
|
/**
|
|
140
140
|
* Reducer managing the theme base global styles.
|
|
141
141
|
*
|
|
142
|
-
* @param {string} state Current state.
|
|
143
|
-
* @param {Object}
|
|
142
|
+
* @param {Record<string, object>} state Current state.
|
|
143
|
+
* @param {Object} action Dispatched action.
|
|
144
144
|
*
|
|
145
|
-
* @return {string} Updated state.
|
|
145
|
+
* @return {Record<string, object>} Updated state.
|
|
146
146
|
*/
|
|
147
147
|
export function themeBaseGlobalStyles( state = {}, action ) {
|
|
148
148
|
switch ( action.type ) {
|
|
@@ -159,10 +159,10 @@ export function themeBaseGlobalStyles( state = {}, action ) {
|
|
|
159
159
|
/**
|
|
160
160
|
* Reducer managing the theme global styles variations.
|
|
161
161
|
*
|
|
162
|
-
* @param {string} state Current state.
|
|
163
|
-
* @param {Object}
|
|
162
|
+
* @param {Record<string, object>} state Current state.
|
|
163
|
+
* @param {Object} action Dispatched action.
|
|
164
164
|
*
|
|
165
|
-
* @return {string} Updated state.
|
|
165
|
+
* @return {Record<string, object>} Updated state.
|
|
166
166
|
*/
|
|
167
167
|
export function themeGlobalStyleVariations( state = {}, action ) {
|
|
168
168
|
switch ( action.type ) {
|
|
@@ -335,7 +335,7 @@ function entity( entityConfig ) {
|
|
|
335
335
|
*
|
|
336
336
|
* @return {Object} Updated state.
|
|
337
337
|
*/
|
|
338
|
-
export function entitiesConfig( state =
|
|
338
|
+
export function entitiesConfig( state = rootEntitiesConfig, action ) {
|
|
339
339
|
switch ( action.type ) {
|
|
340
340
|
case 'ADD_ENTITIES':
|
|
341
341
|
return [ ...state, ...action.entities ];
|
|
@@ -380,10 +380,10 @@ export const entities = ( state = {}, action ) => {
|
|
|
380
380
|
);
|
|
381
381
|
}
|
|
382
382
|
|
|
383
|
-
const newData = entitiesDataReducer( state.
|
|
383
|
+
const newData = entitiesDataReducer( state.records, action );
|
|
384
384
|
|
|
385
385
|
if (
|
|
386
|
-
newData === state.
|
|
386
|
+
newData === state.records &&
|
|
387
387
|
newConfig === state.config &&
|
|
388
388
|
entitiesDataReducer === state.reducer
|
|
389
389
|
) {
|
|
@@ -392,7 +392,7 @@ export const entities = ( state = {}, action ) => {
|
|
|
392
392
|
|
|
393
393
|
return {
|
|
394
394
|
reducer: entitiesDataReducer,
|
|
395
|
-
|
|
395
|
+
records: newData,
|
|
396
396
|
config: newConfig,
|
|
397
397
|
};
|
|
398
398
|
};
|
package/src/resolvers.js
CHANGED
|
@@ -13,7 +13,7 @@ import apiFetch from '@wordpress/api-fetch';
|
|
|
13
13
|
* Internal dependencies
|
|
14
14
|
*/
|
|
15
15
|
import { STORE_NAME } from './name';
|
|
16
|
-
import {
|
|
16
|
+
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
|
|
17
17
|
import { forwardResolver, getNormalizedCommaSeparable } from './utils';
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -52,15 +52,15 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
|
|
|
52
52
|
select,
|
|
53
53
|
dispatch,
|
|
54
54
|
} ) => {
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
if ( !
|
|
55
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
56
|
+
const entityConfig = find( configs, { kind, name } );
|
|
57
|
+
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
62
62
|
STORE_NAME,
|
|
63
|
-
[ 'entities', '
|
|
63
|
+
[ 'entities', 'records', kind, name, key ],
|
|
64
64
|
{ exclusive: false }
|
|
65
65
|
);
|
|
66
66
|
|
|
@@ -73,7 +73,7 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
|
|
|
73
73
|
...query,
|
|
74
74
|
_fields: uniq( [
|
|
75
75
|
...( getNormalizedCommaSeparable( query._fields ) || [] ),
|
|
76
|
-
|
|
76
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
77
77
|
] ).join(),
|
|
78
78
|
};
|
|
79
79
|
}
|
|
@@ -85,10 +85,13 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
|
|
|
85
85
|
// for how the request is made to the REST API.
|
|
86
86
|
|
|
87
87
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
88
|
-
const path = addQueryArgs(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
const path = addQueryArgs(
|
|
89
|
+
entityConfig.baseURL + ( key ? '/' + key : '' ),
|
|
90
|
+
{
|
|
91
|
+
...entityConfig.baseURLParams,
|
|
92
|
+
...query,
|
|
93
|
+
}
|
|
94
|
+
);
|
|
92
95
|
|
|
93
96
|
if ( query !== undefined ) {
|
|
94
97
|
query = { ...query, include: [ key ] };
|
|
@@ -104,10 +107,6 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
|
|
|
104
107
|
|
|
105
108
|
const record = await apiFetch( { path } );
|
|
106
109
|
dispatch.receiveEntityRecords( kind, name, record, query );
|
|
107
|
-
} catch ( error ) {
|
|
108
|
-
// We need a way to handle and access REST API errors in state
|
|
109
|
-
// Until then, catching the error ensures the resolver is marked as resolved.
|
|
110
|
-
// See similar implementation in `getEntityRecords()`.
|
|
111
110
|
} finally {
|
|
112
111
|
dispatch.__unstableReleaseStoreLock( lock );
|
|
113
112
|
}
|
|
@@ -133,15 +132,15 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
|
|
|
133
132
|
export const getEntityRecords = ( kind, name, query = {} ) => async ( {
|
|
134
133
|
dispatch,
|
|
135
134
|
} ) => {
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
if ( !
|
|
135
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
136
|
+
const entityConfig = find( configs, { kind, name } );
|
|
137
|
+
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
|
|
139
138
|
return;
|
|
140
139
|
}
|
|
141
140
|
|
|
142
141
|
const lock = await dispatch.__unstableAcquireStoreLock(
|
|
143
142
|
STORE_NAME,
|
|
144
|
-
[ 'entities', '
|
|
143
|
+
[ 'entities', 'records', kind, name ],
|
|
145
144
|
{ exclusive: false }
|
|
146
145
|
);
|
|
147
146
|
|
|
@@ -154,13 +153,13 @@ export const getEntityRecords = ( kind, name, query = {} ) => async ( {
|
|
|
154
153
|
...query,
|
|
155
154
|
_fields: uniq( [
|
|
156
155
|
...( getNormalizedCommaSeparable( query._fields ) || [] ),
|
|
157
|
-
|
|
156
|
+
entityConfig.key || DEFAULT_ENTITY_KEY,
|
|
158
157
|
] ).join(),
|
|
159
158
|
};
|
|
160
159
|
}
|
|
161
160
|
|
|
162
|
-
const path = addQueryArgs(
|
|
163
|
-
...
|
|
161
|
+
const path = addQueryArgs( entityConfig.baseURL, {
|
|
162
|
+
...entityConfig.baseURLParams,
|
|
164
163
|
...query,
|
|
165
164
|
} );
|
|
166
165
|
|
|
@@ -186,7 +185,7 @@ export const getEntityRecords = ( kind, name, query = {} ) => async ( {
|
|
|
186
185
|
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
|
|
187
186
|
// See https://github.com/WordPress/gutenberg/pull/26575
|
|
188
187
|
if ( ! query?._fields && ! query.context ) {
|
|
189
|
-
const key =
|
|
188
|
+
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
|
|
190
189
|
const resolutionsArgs = records
|
|
191
190
|
.filter( ( record ) => record[ key ] )
|
|
192
191
|
.map( ( record ) => [ kind, name, record[ key ] ] );
|
|
@@ -202,10 +201,6 @@ export const getEntityRecords = ( kind, name, query = {} ) => async ( {
|
|
|
202
201
|
args: resolutionsArgs,
|
|
203
202
|
} );
|
|
204
203
|
}
|
|
205
|
-
} catch ( error ) {
|
|
206
|
-
// We need a way to handle and access REST API errors in state
|
|
207
|
-
// Until then, catching the error ensures the resolver is marked as resolved.
|
|
208
|
-
// See similar implementation in `getEntityRecord()`.
|
|
209
204
|
} finally {
|
|
210
205
|
dispatch.__unstableReleaseStoreLock( lock );
|
|
211
206
|
}
|
|
@@ -312,13 +307,13 @@ export const canUser = ( action, resource, id ) => async ( { dispatch } ) => {
|
|
|
312
307
|
export const canUserEditEntityRecord = ( kind, name, recordId ) => async ( {
|
|
313
308
|
dispatch,
|
|
314
309
|
} ) => {
|
|
315
|
-
const
|
|
316
|
-
const
|
|
317
|
-
if ( !
|
|
310
|
+
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
|
|
311
|
+
const entityConfig = find( configs, { kind, name } );
|
|
312
|
+
if ( ! entityConfig ) {
|
|
318
313
|
return;
|
|
319
314
|
}
|
|
320
315
|
|
|
321
|
-
const resource =
|
|
316
|
+
const resource = entityConfig.__unstable_rest_base;
|
|
322
317
|
await dispatch( canUser( 'update', resource, recordId ) );
|
|
323
318
|
};
|
|
324
319
|
|
package/src/selectors.js
CHANGED
|
@@ -95,27 +95,62 @@ export const getUserQueryResults = createSelector(
|
|
|
95
95
|
);
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* Returns
|
|
98
|
+
* Returns the loaded entities for the given kind.
|
|
99
99
|
*
|
|
100
|
+
* @deprecated since WordPress 6.0. Use getEntitiesConfig instead
|
|
100
101
|
* @param {Object} state Data state.
|
|
101
102
|
* @param {string} kind Entity kind.
|
|
102
103
|
*
|
|
103
104
|
* @return {Array<Object>} Array of entities with config matching kind.
|
|
104
105
|
*/
|
|
105
106
|
export function getEntitiesByKind( state, kind ) {
|
|
107
|
+
deprecated( "wp.data.select( 'core' ).getEntitiesByKind()", {
|
|
108
|
+
since: '6.0',
|
|
109
|
+
alternative: "wp.data.select( 'core' ).getEntitiesConfig()",
|
|
110
|
+
} );
|
|
111
|
+
return getEntitiesConfig( state, kind );
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Returns the loaded entities for the given kind.
|
|
116
|
+
*
|
|
117
|
+
* @param {Object} state Data state.
|
|
118
|
+
* @param {string} kind Entity kind.
|
|
119
|
+
*
|
|
120
|
+
* @return {Array<Object>} Array of entities with config matching kind.
|
|
121
|
+
*/
|
|
122
|
+
export function getEntitiesConfig( state, kind ) {
|
|
106
123
|
return filter( state.entities.config, { kind } );
|
|
107
124
|
}
|
|
108
125
|
|
|
109
126
|
/**
|
|
110
|
-
* Returns the entity
|
|
127
|
+
* Returns the entity config given its kind and name.
|
|
111
128
|
*
|
|
129
|
+
* @deprecated since WordPress 6.0. Use getEntityConfig instead
|
|
112
130
|
* @param {Object} state Data state.
|
|
113
131
|
* @param {string} kind Entity kind.
|
|
114
132
|
* @param {string} name Entity name.
|
|
115
133
|
*
|
|
116
|
-
* @return {Object} Entity
|
|
134
|
+
* @return {Object} Entity config
|
|
117
135
|
*/
|
|
118
136
|
export function getEntity( state, kind, name ) {
|
|
137
|
+
deprecated( "wp.data.select( 'core' ).getEntity()", {
|
|
138
|
+
since: '6.0',
|
|
139
|
+
alternative: "wp.data.select( 'core' ).getEntityConfig()",
|
|
140
|
+
} );
|
|
141
|
+
return getEntityConfig( state, kind, name );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns the entity config given its kind and name.
|
|
146
|
+
*
|
|
147
|
+
* @param {Object} state Data state.
|
|
148
|
+
* @param {string} kind Entity kind.
|
|
149
|
+
* @param {string} name Entity name.
|
|
150
|
+
*
|
|
151
|
+
* @return {Object} Entity config
|
|
152
|
+
*/
|
|
153
|
+
export function getEntityConfig( state, kind, name ) {
|
|
119
154
|
return find( state.entities.config, { kind, name } );
|
|
120
155
|
}
|
|
121
156
|
|
|
@@ -130,11 +165,11 @@ export function getEntity( state, kind, name ) {
|
|
|
130
165
|
* @param {number} key Record's key
|
|
131
166
|
* @param {?Object} query Optional query.
|
|
132
167
|
*
|
|
133
|
-
* @return {Object
|
|
168
|
+
* @return {Object|undefined} Record.
|
|
134
169
|
*/
|
|
135
170
|
export const getEntityRecord = createSelector(
|
|
136
171
|
( state, kind, name, key, query ) => {
|
|
137
|
-
const queriedState = get( state.entities.
|
|
172
|
+
const queriedState = get( state.entities.records, [
|
|
138
173
|
kind,
|
|
139
174
|
name,
|
|
140
175
|
'queriedData',
|
|
@@ -156,7 +191,7 @@ export const getEntityRecord = createSelector(
|
|
|
156
191
|
const item = queriedState.items[ context ]?.[ key ];
|
|
157
192
|
if ( item && query._fields ) {
|
|
158
193
|
const filteredItem = {};
|
|
159
|
-
const fields = getNormalizedCommaSeparable( query._fields );
|
|
194
|
+
const fields = getNormalizedCommaSeparable( query._fields ) ?? [];
|
|
160
195
|
for ( let f = 0; f < fields.length; f++ ) {
|
|
161
196
|
const field = fields[ f ].split( '.' );
|
|
162
197
|
const value = get( item, field );
|
|
@@ -170,7 +205,7 @@ export const getEntityRecord = createSelector(
|
|
|
170
205
|
( state, kind, name, recordId, query ) => {
|
|
171
206
|
const context = query?.context ?? 'default';
|
|
172
207
|
return [
|
|
173
|
-
get( state.entities.
|
|
208
|
+
get( state.entities.records, [
|
|
174
209
|
kind,
|
|
175
210
|
name,
|
|
176
211
|
'queriedData',
|
|
@@ -178,7 +213,7 @@ export const getEntityRecord = createSelector(
|
|
|
178
213
|
context,
|
|
179
214
|
recordId,
|
|
180
215
|
] ),
|
|
181
|
-
get( state.entities.
|
|
216
|
+
get( state.entities.records, [
|
|
182
217
|
kind,
|
|
183
218
|
name,
|
|
184
219
|
'queriedData',
|
|
@@ -191,7 +226,7 @@ export const getEntityRecord = createSelector(
|
|
|
191
226
|
);
|
|
192
227
|
|
|
193
228
|
/**
|
|
194
|
-
* Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity from the API if the entity record isn't available in the local state.
|
|
229
|
+
* Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity records from the API if the entity record isn't available in the local state.
|
|
195
230
|
*
|
|
196
231
|
* @param {Object} state State tree
|
|
197
232
|
* @param {string} kind Entity kind.
|
|
@@ -226,7 +261,9 @@ export const getRawEntityRecord = createSelector(
|
|
|
226
261
|
return (
|
|
227
262
|
record &&
|
|
228
263
|
Object.keys( record ).reduce( ( accumulator, _key ) => {
|
|
229
|
-
if (
|
|
264
|
+
if (
|
|
265
|
+
isRawAttribute( getEntityConfig( state, kind, name ), _key )
|
|
266
|
+
) {
|
|
230
267
|
// Because edits are the "raw" attribute values,
|
|
231
268
|
// we return those from record selectors to make rendering,
|
|
232
269
|
// comparisons, and joins with edits easier.
|
|
@@ -246,7 +283,7 @@ export const getRawEntityRecord = createSelector(
|
|
|
246
283
|
const context = query?.context ?? 'default';
|
|
247
284
|
return [
|
|
248
285
|
state.entities.config,
|
|
249
|
-
get( state.entities.
|
|
286
|
+
get( state.entities.records, [
|
|
250
287
|
kind,
|
|
251
288
|
name,
|
|
252
289
|
'queriedData',
|
|
@@ -254,7 +291,7 @@ export const getRawEntityRecord = createSelector(
|
|
|
254
291
|
context,
|
|
255
292
|
recordId,
|
|
256
293
|
] ),
|
|
257
|
-
get( state.entities.
|
|
294
|
+
get( state.entities.records, [
|
|
258
295
|
kind,
|
|
259
296
|
name,
|
|
260
297
|
'queriedData',
|
|
@@ -294,7 +331,7 @@ export function hasEntityRecords( state, kind, name, query ) {
|
|
|
294
331
|
export function getEntityRecords( state, kind, name, query ) {
|
|
295
332
|
// Queried data state is prepopulated for all known entities. If this is not
|
|
296
333
|
// assigned for the given parameters, then it is known to not exist.
|
|
297
|
-
const queriedState = get( state.entities.
|
|
334
|
+
const queriedState = get( state.entities.records, [
|
|
298
335
|
kind,
|
|
299
336
|
name,
|
|
300
337
|
'queriedData',
|
|
@@ -315,13 +352,13 @@ export function getEntityRecords( state, kind, name, query ) {
|
|
|
315
352
|
export const __experimentalGetDirtyEntityRecords = createSelector(
|
|
316
353
|
( state ) => {
|
|
317
354
|
const {
|
|
318
|
-
entities: {
|
|
355
|
+
entities: { records },
|
|
319
356
|
} = state;
|
|
320
357
|
const dirtyRecords = [];
|
|
321
|
-
Object.keys(
|
|
322
|
-
Object.keys(
|
|
358
|
+
Object.keys( records ).forEach( ( kind ) => {
|
|
359
|
+
Object.keys( records[ kind ] ).forEach( ( name ) => {
|
|
323
360
|
const primaryKeys = Object.keys(
|
|
324
|
-
|
|
361
|
+
records[ kind ][ name ].edits
|
|
325
362
|
).filter(
|
|
326
363
|
( primaryKey ) =>
|
|
327
364
|
// The entity record must exist (not be deleted),
|
|
@@ -331,7 +368,7 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
|
|
|
331
368
|
);
|
|
332
369
|
|
|
333
370
|
if ( primaryKeys.length ) {
|
|
334
|
-
const
|
|
371
|
+
const entityConfig = getEntityConfig( state, kind, name );
|
|
335
372
|
primaryKeys.forEach( ( primaryKey ) => {
|
|
336
373
|
const entityRecord = getEditedEntityRecord(
|
|
337
374
|
state,
|
|
@@ -344,9 +381,10 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
|
|
|
344
381
|
// when it's used as an object key.
|
|
345
382
|
key:
|
|
346
383
|
entityRecord[
|
|
347
|
-
|
|
384
|
+
entityConfig.key || DEFAULT_ENTITY_KEY
|
|
348
385
|
],
|
|
349
|
-
title:
|
|
386
|
+
title:
|
|
387
|
+
entityConfig?.getTitle?.( entityRecord ) || '',
|
|
350
388
|
name,
|
|
351
389
|
kind,
|
|
352
390
|
} );
|
|
@@ -357,7 +395,7 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
|
|
|
357
395
|
|
|
358
396
|
return dirtyRecords;
|
|
359
397
|
},
|
|
360
|
-
( state ) => [ state.entities.
|
|
398
|
+
( state ) => [ state.entities.records ]
|
|
361
399
|
);
|
|
362
400
|
|
|
363
401
|
/**
|
|
@@ -370,19 +408,19 @@ export const __experimentalGetDirtyEntityRecords = createSelector(
|
|
|
370
408
|
export const __experimentalGetEntitiesBeingSaved = createSelector(
|
|
371
409
|
( state ) => {
|
|
372
410
|
const {
|
|
373
|
-
entities: {
|
|
411
|
+
entities: { records },
|
|
374
412
|
} = state;
|
|
375
413
|
const recordsBeingSaved = [];
|
|
376
|
-
Object.keys(
|
|
377
|
-
Object.keys(
|
|
414
|
+
Object.keys( records ).forEach( ( kind ) => {
|
|
415
|
+
Object.keys( records[ kind ] ).forEach( ( name ) => {
|
|
378
416
|
const primaryKeys = Object.keys(
|
|
379
|
-
|
|
417
|
+
records[ kind ][ name ].saving
|
|
380
418
|
).filter( ( primaryKey ) =>
|
|
381
419
|
isSavingEntityRecord( state, kind, name, primaryKey )
|
|
382
420
|
);
|
|
383
421
|
|
|
384
422
|
if ( primaryKeys.length ) {
|
|
385
|
-
const
|
|
423
|
+
const entityConfig = getEntityConfig( state, kind, name );
|
|
386
424
|
primaryKeys.forEach( ( primaryKey ) => {
|
|
387
425
|
const entityRecord = getEditedEntityRecord(
|
|
388
426
|
state,
|
|
@@ -395,9 +433,10 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
|
|
|
395
433
|
// when it's used as an object key.
|
|
396
434
|
key:
|
|
397
435
|
entityRecord[
|
|
398
|
-
|
|
436
|
+
entityConfig.key || DEFAULT_ENTITY_KEY
|
|
399
437
|
],
|
|
400
|
-
title:
|
|
438
|
+
title:
|
|
439
|
+
entityConfig?.getTitle?.( entityRecord ) || '',
|
|
401
440
|
name,
|
|
402
441
|
kind,
|
|
403
442
|
} );
|
|
@@ -407,7 +446,7 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
|
|
|
407
446
|
} );
|
|
408
447
|
return recordsBeingSaved;
|
|
409
448
|
},
|
|
410
|
-
( state ) => [ state.entities.
|
|
449
|
+
( state ) => [ state.entities.records ]
|
|
411
450
|
);
|
|
412
451
|
|
|
413
452
|
/**
|
|
@@ -421,7 +460,7 @@ export const __experimentalGetEntitiesBeingSaved = createSelector(
|
|
|
421
460
|
* @return {Object?} The entity record's edits.
|
|
422
461
|
*/
|
|
423
462
|
export function getEntityRecordEdits( state, kind, name, recordId ) {
|
|
424
|
-
return get( state.entities.
|
|
463
|
+
return get( state.entities.records, [ kind, name, 'edits', recordId ] );
|
|
425
464
|
}
|
|
426
465
|
|
|
427
466
|
/**
|
|
@@ -440,7 +479,7 @@ export function getEntityRecordEdits( state, kind, name, recordId ) {
|
|
|
440
479
|
*/
|
|
441
480
|
export const getEntityRecordNonTransientEdits = createSelector(
|
|
442
481
|
( state, kind, name, recordId ) => {
|
|
443
|
-
const { transientEdits } =
|
|
482
|
+
const { transientEdits } = getEntityConfig( state, kind, name ) || {};
|
|
444
483
|
const edits = getEntityRecordEdits( state, kind, name, recordId ) || {};
|
|
445
484
|
if ( ! transientEdits ) {
|
|
446
485
|
return edits;
|
|
@@ -454,7 +493,7 @@ export const getEntityRecordNonTransientEdits = createSelector(
|
|
|
454
493
|
},
|
|
455
494
|
( state, kind, name, recordId ) => [
|
|
456
495
|
state.entities.config,
|
|
457
|
-
get( state.entities.
|
|
496
|
+
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
|
|
458
497
|
]
|
|
459
498
|
);
|
|
460
499
|
|
|
@@ -481,10 +520,10 @@ export function hasEditsForEntityRecord( state, kind, name, recordId ) {
|
|
|
481
520
|
/**
|
|
482
521
|
* Returns the specified entity record, merged with its edits.
|
|
483
522
|
*
|
|
484
|
-
* @param {Object}
|
|
485
|
-
* @param {string}
|
|
486
|
-
* @param {string}
|
|
487
|
-
* @param {number} recordId Record ID.
|
|
523
|
+
* @param {Object} state State tree.
|
|
524
|
+
* @param {string} kind Entity kind.
|
|
525
|
+
* @param {string} name Entity name.
|
|
526
|
+
* @param {number|string} recordId Record ID.
|
|
488
527
|
*
|
|
489
528
|
* @return {Object?} The entity record, merged with its edits.
|
|
490
529
|
*/
|
|
@@ -497,7 +536,7 @@ export const getEditedEntityRecord = createSelector(
|
|
|
497
536
|
const context = query?.context ?? 'default';
|
|
498
537
|
return [
|
|
499
538
|
state.entities.config,
|
|
500
|
-
get( state.entities.
|
|
539
|
+
get( state.entities.records, [
|
|
501
540
|
kind,
|
|
502
541
|
name,
|
|
503
542
|
'queriedData',
|
|
@@ -505,7 +544,7 @@ export const getEditedEntityRecord = createSelector(
|
|
|
505
544
|
context,
|
|
506
545
|
recordId,
|
|
507
546
|
] ),
|
|
508
|
-
get( state.entities.
|
|
547
|
+
get( state.entities.records, [
|
|
509
548
|
kind,
|
|
510
549
|
name,
|
|
511
550
|
'queriedData',
|
|
@@ -513,7 +552,7 @@ export const getEditedEntityRecord = createSelector(
|
|
|
513
552
|
context,
|
|
514
553
|
recordId,
|
|
515
554
|
] ),
|
|
516
|
-
get( state.entities.
|
|
555
|
+
get( state.entities.records, [ kind, name, 'edits', recordId ] ),
|
|
517
556
|
];
|
|
518
557
|
}
|
|
519
558
|
);
|
|
@@ -530,7 +569,7 @@ export const getEditedEntityRecord = createSelector(
|
|
|
530
569
|
*/
|
|
531
570
|
export function isAutosavingEntityRecord( state, kind, name, recordId ) {
|
|
532
571
|
const { pending, isAutosave } = get(
|
|
533
|
-
state.entities.
|
|
572
|
+
state.entities.records,
|
|
534
573
|
[ kind, name, 'saving', recordId ],
|
|
535
574
|
{}
|
|
536
575
|
);
|
|
@@ -540,16 +579,16 @@ export function isAutosavingEntityRecord( state, kind, name, recordId ) {
|
|
|
540
579
|
/**
|
|
541
580
|
* Returns true if the specified entity record is saving, and false otherwise.
|
|
542
581
|
*
|
|
543
|
-
* @param {Object}
|
|
544
|
-
* @param {string}
|
|
545
|
-
* @param {string}
|
|
546
|
-
* @param {number} recordId Record ID.
|
|
582
|
+
* @param {Object} state State tree.
|
|
583
|
+
* @param {string} kind Entity kind.
|
|
584
|
+
* @param {string} name Entity name.
|
|
585
|
+
* @param {number|string} recordId Record ID.
|
|
547
586
|
*
|
|
548
587
|
* @return {boolean} Whether the entity record is saving or not.
|
|
549
588
|
*/
|
|
550
589
|
export function isSavingEntityRecord( state, kind, name, recordId ) {
|
|
551
590
|
return get(
|
|
552
|
-
state.entities.
|
|
591
|
+
state.entities.records,
|
|
553
592
|
[ kind, name, 'saving', recordId, 'pending' ],
|
|
554
593
|
false
|
|
555
594
|
);
|
|
@@ -567,7 +606,7 @@ export function isSavingEntityRecord( state, kind, name, recordId ) {
|
|
|
567
606
|
*/
|
|
568
607
|
export function isDeletingEntityRecord( state, kind, name, recordId ) {
|
|
569
608
|
return get(
|
|
570
|
-
state.entities.
|
|
609
|
+
state.entities.records,
|
|
571
610
|
[ kind, name, 'deleting', recordId, 'pending' ],
|
|
572
611
|
false
|
|
573
612
|
);
|
|
@@ -584,7 +623,7 @@ export function isDeletingEntityRecord( state, kind, name, recordId ) {
|
|
|
584
623
|
* @return {Object?} The entity record's save error.
|
|
585
624
|
*/
|
|
586
625
|
export function getLastEntitySaveError( state, kind, name, recordId ) {
|
|
587
|
-
return get( state.entities.
|
|
626
|
+
return get( state.entities.records, [
|
|
588
627
|
kind,
|
|
589
628
|
name,
|
|
590
629
|
'saving',
|
|
@@ -604,7 +643,7 @@ export function getLastEntitySaveError( state, kind, name, recordId ) {
|
|
|
604
643
|
* @return {Object?} The entity record's save error.
|
|
605
644
|
*/
|
|
606
645
|
export function getLastEntityDeleteError( state, kind, name, recordId ) {
|
|
607
|
-
return get( state.entities.
|
|
646
|
+
return get( state.entities.records, [
|
|
608
647
|
kind,
|
|
609
648
|
name,
|
|
610
649
|
'deleting',
|
|
@@ -780,11 +819,11 @@ export function canUser( state, action, resource, id ) {
|
|
|
780
819
|
* or `undefined` if the OPTIONS request is still being made.
|
|
781
820
|
*/
|
|
782
821
|
export function canUserEditEntityRecord( state, kind, name, recordId ) {
|
|
783
|
-
const
|
|
784
|
-
if ( !
|
|
822
|
+
const entityConfig = getEntityConfig( state, kind, name );
|
|
823
|
+
if ( ! entityConfig ) {
|
|
785
824
|
return false;
|
|
786
825
|
}
|
|
787
|
-
const resource =
|
|
826
|
+
const resource = entityConfig.__unstable_rest_base;
|
|
788
827
|
|
|
789
828
|
return canUser( state, 'update', resource, recordId );
|
|
790
829
|
}
|
|
@@ -899,7 +938,7 @@ export function __experimentalGetTemplateForLink( state, link ) {
|
|
|
899
938
|
*
|
|
900
939
|
* @param {Object} state Editor state.
|
|
901
940
|
*
|
|
902
|
-
* @return {Object
|
|
941
|
+
* @return {Object|null} The Global Styles object.
|
|
903
942
|
*/
|
|
904
943
|
export function __experimentalGetCurrentThemeBaseGlobalStyles( state ) {
|
|
905
944
|
const currentTheme = getCurrentTheme( state );
|
|
@@ -914,7 +953,7 @@ export function __experimentalGetCurrentThemeBaseGlobalStyles( state ) {
|
|
|
914
953
|
*
|
|
915
954
|
* @param {Object} state Data state.
|
|
916
955
|
*
|
|
917
|
-
* @return {string} The current global styles ID.
|
|
956
|
+
* @return {string|null} The current global styles ID.
|
|
918
957
|
*/
|
|
919
958
|
export function __experimentalGetCurrentThemeGlobalStylesVariations( state ) {
|
|
920
959
|
const currentTheme = getCurrentTheme( state );
|