@wordpress/core-data 4.11.0 → 4.14.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 +10 -0
- package/README.md +124 -81
- package/build/actions.js +27 -0
- package/build/actions.js.map +1 -1
- package/build/batch/create-batch.js +3 -15
- package/build/batch/create-batch.js.map +1 -1
- package/build/entities.js +7 -5
- package/build/entities.js.map +1 -1
- package/build/hooks/index.js +14 -0
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use-entity-record.js +84 -2
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +72 -11
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +1 -28
- package/build/index.js.map +1 -1
- package/build/resolvers.js +22 -24
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +5 -3
- package/build/selectors.js.map +1 -1
- package/build-module/actions.js +27 -0
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/create-batch.js +3 -14
- package/build-module/batch/create-batch.js.map +1 -1
- package/build-module/entities.js +7 -6
- package/build-module/entities.js.map +1 -1
- package/build-module/hooks/index.js +1 -0
- package/build-module/hooks/index.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +82 -2
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +68 -10
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +0 -3
- package/build-module/index.js.map +1 -1
- package/build-module/resolvers.js +22 -24
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +6 -4
- package/build-module/selectors.js.map +1 -1
- package/package.json +12 -11
- package/src/actions.js +27 -0
- package/src/batch/create-batch.js +3 -12
- package/src/entities.ts +8 -6
- package/src/entity-types/theme.ts +4 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/test/use-entity-record.js +49 -1
- package/src/hooks/test/use-resource-permissions.js +32 -36
- package/src/hooks/use-entity-record.ts +107 -2
- package/src/hooks/use-resource-permissions.ts +82 -20
- package/src/index.js +0 -3
- package/src/resolvers.js +41 -36
- package/src/selectors.ts +6 -4
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
+
import { useDispatch, useSelect } from '@wordpress/data';
|
|
4
5
|
import deprecated from '@wordpress/deprecated';
|
|
6
|
+
import { useMemo } from '@wordpress/element';
|
|
5
7
|
/**
|
|
6
8
|
* Internal dependencies
|
|
7
9
|
*/
|
|
@@ -38,6 +40,60 @@ import { store as coreStore } from '../';
|
|
|
38
40
|
* application, the page and the resolution details will be retrieved from
|
|
39
41
|
* the store state using `getEntityRecord()`, or resolved if missing.
|
|
40
42
|
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```js
|
|
45
|
+
* import { useDispatch } from '@wordpress/data';
|
|
46
|
+
* import { useCallback } from '@wordpress/element';
|
|
47
|
+
* import { __ } from '@wordpress/i18n';
|
|
48
|
+
* import { TextControl } from '@wordpress/components';
|
|
49
|
+
* import { store as noticeStore } from '@wordpress/notices';
|
|
50
|
+
* import { useEntityRecord } from '@wordpress/core-data';
|
|
51
|
+
*
|
|
52
|
+
* function PageRenameForm( { id } ) {
|
|
53
|
+
* const page = useEntityRecord( 'postType', 'page', id );
|
|
54
|
+
* const { createSuccessNotice, createErrorNotice } =
|
|
55
|
+
* useDispatch( noticeStore );
|
|
56
|
+
*
|
|
57
|
+
* const setTitle = useCallback( ( title ) => {
|
|
58
|
+
* page.edit( { title } );
|
|
59
|
+
* }, [ page.edit ] );
|
|
60
|
+
*
|
|
61
|
+
* if ( page.isResolving ) {
|
|
62
|
+
* return 'Loading...';
|
|
63
|
+
* }
|
|
64
|
+
*
|
|
65
|
+
* async function onRename( event ) {
|
|
66
|
+
* event.preventDefault();
|
|
67
|
+
* try {
|
|
68
|
+
* await page.save();
|
|
69
|
+
* createSuccessNotice( __( 'Page renamed.' ), {
|
|
70
|
+
* type: 'snackbar',
|
|
71
|
+
* } );
|
|
72
|
+
* } catch ( error ) {
|
|
73
|
+
* createErrorNotice( error.message, { type: 'snackbar' } );
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
*
|
|
77
|
+
* return (
|
|
78
|
+
* <form onSubmit={ onRename }>
|
|
79
|
+
* <TextControl
|
|
80
|
+
* label={ __( 'Name' ) }
|
|
81
|
+
* value={ page.editedRecord.title }
|
|
82
|
+
* onChange={ setTitle }
|
|
83
|
+
* />
|
|
84
|
+
* <button type="submit">{ __( 'Save' ) }</button>
|
|
85
|
+
* </form>
|
|
86
|
+
* );
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* // Rendered in the application:
|
|
90
|
+
* // <PageRenameForm id={ 1 } />
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* In the above example, updating and saving the page title is handled
|
|
94
|
+
* via the `edit()` and `save()` mutation helpers provided by
|
|
95
|
+
* `useEntityRecord()`;
|
|
96
|
+
*
|
|
41
97
|
* @return Entity record data.
|
|
42
98
|
* @template RecordType
|
|
43
99
|
*/
|
|
@@ -45,9 +101,30 @@ export default function useEntityRecord(kind, name, recordId) {
|
|
|
45
101
|
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {
|
|
46
102
|
enabled: true
|
|
47
103
|
};
|
|
104
|
+
const {
|
|
105
|
+
editEntityRecord,
|
|
106
|
+
saveEditedEntityRecord
|
|
107
|
+
} = useDispatch(coreStore);
|
|
108
|
+
const mutations = useMemo(() => ({
|
|
109
|
+
edit: record => editEntityRecord(kind, name, recordId, record),
|
|
110
|
+
save: function () {
|
|
111
|
+
let saveOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
112
|
+
return saveEditedEntityRecord(kind, name, recordId, {
|
|
113
|
+
throwOnError: true,
|
|
114
|
+
...saveOptions
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}), [recordId]);
|
|
118
|
+
const {
|
|
119
|
+
editedRecord,
|
|
120
|
+
hasEdits
|
|
121
|
+
} = useSelect(select => ({
|
|
122
|
+
editedRecord: select(coreStore).getEditedEntityRecord(kind, name, recordId),
|
|
123
|
+
hasEdits: select(coreStore).hasEditsForEntityRecord(kind, name, recordId)
|
|
124
|
+
}), [kind, name, recordId]);
|
|
48
125
|
const {
|
|
49
126
|
data: record,
|
|
50
|
-
...
|
|
127
|
+
...querySelectRest
|
|
51
128
|
} = useQuerySelect(query => {
|
|
52
129
|
if (!options.enabled) {
|
|
53
130
|
return null;
|
|
@@ -57,7 +134,10 @@ export default function useEntityRecord(kind, name, recordId) {
|
|
|
57
134
|
}, [kind, name, recordId, options.enabled]);
|
|
58
135
|
return {
|
|
59
136
|
record,
|
|
60
|
-
|
|
137
|
+
editedRecord,
|
|
138
|
+
hasEdits,
|
|
139
|
+
...querySelectRest,
|
|
140
|
+
...mutations
|
|
61
141
|
};
|
|
62
142
|
}
|
|
63
143
|
export function __experimentalUseEntityRecord(kind, name, recordId, options) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-record.ts"],"names":["deprecated","useQuerySelect","store","coreStore","useEntityRecord","kind","name","recordId","options","enabled","
|
|
1
|
+
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-record.ts"],"names":["useDispatch","useSelect","deprecated","useMemo","useQuerySelect","store","coreStore","useEntityRecord","kind","name","recordId","options","enabled","editEntityRecord","saveEditedEntityRecord","mutations","edit","record","save","saveOptions","throwOnError","editedRecord","hasEdits","select","getEditedEntityRecord","hasEditsForEntityRecord","data","querySelectRest","query","getEntityRecord","__experimentalUseEntityRecord","alternative","since"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAT,EAAsBC,SAAtB,QAAuC,iBAAvC;AACA,OAAOC,UAAP,MAAuB,uBAAvB;AACA,SAASC,OAAT,QAAwB,oBAAxB;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;;AA4CA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,eAAT,CACdC,IADc,EAEdC,IAFc,EAGdC,QAHc,EAKyB;AAAA,MADvCC,OACuC,uEADpB;AAAEC,IAAAA,OAAO,EAAE;AAAX,GACoB;AACvC,QAAM;AAAEC,IAAAA,gBAAF;AAAoBC,IAAAA;AAApB,MACLd,WAAW,CAAEM,SAAF,CADZ;AAGA,QAAMS,SAAS,GAAGZ,OAAO,CACxB,OAAQ;AACPa,IAAAA,IAAI,EAAIC,MAAF,IACLJ,gBAAgB,CAAEL,IAAF,EAAQC,IAAR,EAAcC,QAAd,EAAwBO,MAAxB,CAFV;AAGPC,IAAAA,IAAI,EAAE;AAAA,UAAEC,WAAF,uEAAqB,EAArB;AAAA,aACLL,sBAAsB,CAAEN,IAAF,EAAQC,IAAR,EAAcC,QAAd,EAAwB;AAC7CU,QAAAA,YAAY,EAAE,IAD+B;AAE7C,WAAGD;AAF0C,OAAxB,CADjB;AAAA;AAHC,GAAR,CADwB,EAUxB,CAAET,QAAF,CAVwB,CAAzB;AAaA,QAAM;AAAEW,IAAAA,YAAF;AAAgBC,IAAAA;AAAhB,MAA6BrB,SAAS,CACzCsB,MAAF,KAAgB;AACfF,IAAAA,YAAY,EAAEE,MAAM,CAAEjB,SAAF,CAAN,CAAoBkB,qBAApB,CACbhB,IADa,EAEbC,IAFa,EAGbC,QAHa,CADC;AAMfY,IAAAA,QAAQ,EAAEC,MAAM,CAAEjB,SAAF,CAAN,CAAoBmB,uBAApB,CACTjB,IADS,EAETC,IAFS,EAGTC,QAHS;AANK,GAAhB,CAD2C,EAa3C,CAAEF,IAAF,EAAQC,IAAR,EAAcC,QAAd,CAb2C,CAA5C;AAgBA,QAAM;AAAEgB,IAAAA,IAAI,EAAET,MAAR;AAAgB,OAAGU;AAAnB,MAAuCvB,cAAc,CACxDwB,KAAF,IAAa;AACZ,QAAK,CAAEjB,OAAO,CAACC,OAAf,EAAyB;AACxB,aAAO,IAAP;AACA;;AACD,WAAOgB,KAAK,CAAEtB,SAAF,CAAL,CAAmBuB,eAAnB,CAAoCrB,IAApC,EAA0CC,IAA1C,EAAgDC,QAAhD,CAAP;AACA,GANyD,EAO1D,CAAEF,IAAF,EAAQC,IAAR,EAAcC,QAAd,EAAwBC,OAAO,CAACC,OAAhC,CAP0D,CAA3D;AAUA,SAAO;AACNK,IAAAA,MADM;AAENI,IAAAA,YAFM;AAGNC,IAAAA,QAHM;AAIN,OAAGK,eAJG;AAKN,OAAGZ;AALG,GAAP;AAOA;AAED,OAAO,SAASe,6BAAT,CACNtB,IADM,EAENC,IAFM,EAGNC,QAHM,EAINC,OAJM,EAKL;AACDT,EAAAA,UAAU,CAAG,uCAAH,EAA2C;AACpD6B,IAAAA,WAAW,EAAE,yBADuC;AAEpDC,IAAAA,KAAK,EAAE;AAF6C,GAA3C,CAAV;AAIA,SAAOzB,eAAe,CAAEC,IAAF,EAAQC,IAAR,EAAcC,QAAd,EAAwBC,OAAxB,CAAtB;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport deprecated from '@wordpress/deprecated';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Status } from './constants';\n\nexport interface EntityRecordResolution< RecordType > {\n\t/** The requested entity record */\n\trecord: RecordType | null;\n\n\t/** The edited entity record */\n\teditedRecord: Partial< RecordType >;\n\n\t/** Apply local (in-browser) edits to the edited entity record */\n\tedit: ( diff: Partial< RecordType > ) => void;\n\n\t/** Persist the edits to the server */\n\tsave: () => Promise< void >;\n\n\t/**\n\t * Is the record still being resolved?\n\t */\n\tisResolving: boolean;\n\n\t/**\n\t * Does the record have any local edits?\n\t */\n\thasEdits: boolean;\n\n\t/**\n\t * Is the record resolved by now?\n\t */\n\thasResolved: boolean;\n\n\t/** Resolution status */\n\tstatus: Status;\n}\n\nexport interface Options {\n\t/**\n\t * Whether to run the query or short-circuit and return null.\n\t *\n\t * @default true\n\t */\n\tenabled: boolean;\n}\n\n/**\n * Resolves the specified entity record.\n *\n * @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.\n * @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.\n * @param recordId ID of the requested entity record.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { record, isResolving } = useEntityRecord( 'postType', 'page', id );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return record.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 1 } />\n * ```\n *\n * In the above example, when `PageTitleDisplay` is rendered into an\n * application, the page and the resolution details will be retrieved from\n * the store state using `getEntityRecord()`, or resolved if missing.\n *\n * @example\n * ```js\n * import { useDispatch } from '@wordpress/data';\n * import { useCallback } from '@wordpress/element';\n * import { __ } from '@wordpress/i18n';\n * import { TextControl } from '@wordpress/components';\n * import { store as noticeStore } from '@wordpress/notices';\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageRenameForm( { id } ) {\n * \tconst page = useEntityRecord( 'postType', 'page', id );\n * \tconst { createSuccessNotice, createErrorNotice } =\n * \t\tuseDispatch( noticeStore );\n *\n * \tconst setTitle = useCallback( ( title ) => {\n * \t\tpage.edit( { title } );\n * \t}, [ page.edit ] );\n *\n * \tif ( page.isResolving ) {\n * \t\treturn 'Loading...';\n * \t}\n *\n * \tasync function onRename( event ) {\n * \t\tevent.preventDefault();\n * \t\ttry {\n * \t\t\tawait page.save();\n * \t\t\tcreateSuccessNotice( __( 'Page renamed.' ), {\n * \t\t\t\ttype: 'snackbar',\n * \t\t\t} );\n * \t\t} catch ( error ) {\n * \t\t\tcreateErrorNotice( error.message, { type: 'snackbar' } );\n * \t\t}\n * \t}\n *\n * \treturn (\n * \t\t<form onSubmit={ onRename }>\n * \t\t\t<TextControl\n * \t\t\t\tlabel={ __( 'Name' ) }\n * \t\t\t\tvalue={ page.editedRecord.title }\n * \t\t\t\tonChange={ setTitle }\n * \t\t\t/>\n * \t\t\t<button type=\"submit\">{ __( 'Save' ) }</button>\n * \t\t</form>\n * \t);\n * }\n *\n * // Rendered in the application:\n * // <PageRenameForm id={ 1 } />\n * ```\n *\n * In the above example, updating and saving the page title is handled\n * via the `edit()` and `save()` mutation helpers provided by\n * `useEntityRecord()`;\n *\n * @return Entity record data.\n * @template RecordType\n */\nexport default function useEntityRecord< RecordType >(\n\tkind: string,\n\tname: string,\n\trecordId: string | number,\n\toptions: Options = { enabled: true }\n): EntityRecordResolution< RecordType > {\n\tconst { editEntityRecord, saveEditedEntityRecord } =\n\t\tuseDispatch( coreStore );\n\n\tconst mutations = useMemo(\n\t\t() => ( {\n\t\t\tedit: ( record ) =>\n\t\t\t\teditEntityRecord( kind, name, recordId, record ),\n\t\t\tsave: ( saveOptions: any = {} ) =>\n\t\t\t\tsaveEditedEntityRecord( kind, name, recordId, {\n\t\t\t\t\tthrowOnError: true,\n\t\t\t\t\t...saveOptions,\n\t\t\t\t} ),\n\t\t} ),\n\t\t[ recordId ]\n\t);\n\n\tconst { editedRecord, hasEdits } = useSelect(\n\t\t( select ) => ( {\n\t\t\teditedRecord: select( coreStore ).getEditedEntityRecord(\n\t\t\t\tkind,\n\t\t\t\tname,\n\t\t\t\trecordId\n\t\t\t),\n\t\t\thasEdits: select( coreStore ).hasEditsForEntityRecord(\n\t\t\t\tkind,\n\t\t\t\tname,\n\t\t\t\trecordId\n\t\t\t),\n\t\t} ),\n\t\t[ kind, name, recordId ]\n\t);\n\n\tconst { data: record, ...querySelectRest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecord( kind, name, recordId );\n\t\t},\n\t\t[ kind, name, recordId, options.enabled ]\n\t);\n\n\treturn {\n\t\trecord,\n\t\teditedRecord,\n\t\thasEdits,\n\t\t...querySelectRest,\n\t\t...mutations,\n\t};\n}\n\nexport function __experimentalUseEntityRecord(\n\tkind: string,\n\tname: string,\n\trecordId: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecord`, {\n\t\talternative: 'wp.data.useEntityRecord',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecord( kind, name, recordId, options );\n}\n"]}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import deprecated from '@wordpress/deprecated';
|
|
1
5
|
/**
|
|
2
6
|
* Internal dependencies
|
|
3
7
|
*/
|
|
8
|
+
|
|
4
9
|
import { store as coreStore } from '../';
|
|
5
10
|
import { Status } from './constants';
|
|
6
11
|
import useQuerySelect from './use-query-select';
|
|
@@ -34,6 +39,36 @@ import useQuerySelect from './use-query-select';
|
|
|
34
39
|
* // <PagesList />
|
|
35
40
|
* ```
|
|
36
41
|
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```js
|
|
44
|
+
* import { useResourcePermissions } from '@wordpress/core-data';
|
|
45
|
+
*
|
|
46
|
+
* function Page({ pageId }) {
|
|
47
|
+
* const {
|
|
48
|
+
* canCreate,
|
|
49
|
+
* canUpdate,
|
|
50
|
+
* canDelete,
|
|
51
|
+
* isResolving
|
|
52
|
+
* } = useResourcePermissions( 'pages', pageId );
|
|
53
|
+
*
|
|
54
|
+
* if ( isResolving ) {
|
|
55
|
+
* return 'Loading ...';
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* return (
|
|
59
|
+
* <div>
|
|
60
|
+
* {canCreate ? (<button>+ Create a new page</button>) : false}
|
|
61
|
+
* {canUpdate ? (<button>Edit page</button>) : false}
|
|
62
|
+
* {canDelete ? (<button>Delete page</button>) : false}
|
|
63
|
+
* // ...
|
|
64
|
+
* </div>
|
|
65
|
+
* );
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // Rendered in the application:
|
|
69
|
+
* // <Page pageId={ 15 } />
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
37
72
|
* In the above example, when `PagesList` is rendered into an
|
|
38
73
|
* application, the appropriate permissions and the resolution details will be retrieved from
|
|
39
74
|
* the store state using `canUser()`, or resolved if missing.
|
|
@@ -41,7 +76,7 @@ import useQuerySelect from './use-query-select';
|
|
|
41
76
|
* @return Entity records data.
|
|
42
77
|
* @template IdType
|
|
43
78
|
*/
|
|
44
|
-
export default function
|
|
79
|
+
export default function useResourcePermissions(resource, id) {
|
|
45
80
|
return useQuerySelect(resolve => {
|
|
46
81
|
const {
|
|
47
82
|
canUser
|
|
@@ -49,19 +84,33 @@ export default function __experimentalUseResourcePermissions(resource, id) {
|
|
|
49
84
|
const create = canUser('create', resource);
|
|
50
85
|
|
|
51
86
|
if (!id) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
87
|
+
const read = canUser('read', resource);
|
|
88
|
+
const isResolving = create.isResolving || read.isResolving;
|
|
89
|
+
const hasResolved = create.hasResolved && read.hasResolved;
|
|
90
|
+
let status = Status.Idle;
|
|
91
|
+
|
|
92
|
+
if (isResolving) {
|
|
93
|
+
status = Status.Resolving;
|
|
94
|
+
} else if (hasResolved) {
|
|
95
|
+
status = Status.Success;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
status,
|
|
100
|
+
isResolving,
|
|
101
|
+
hasResolved,
|
|
102
|
+
canCreate: create.hasResolved && create.data,
|
|
103
|
+
canRead: read.hasResolved && read.data
|
|
104
|
+
};
|
|
57
105
|
}
|
|
58
106
|
|
|
107
|
+
const read = canUser('read', resource, id);
|
|
59
108
|
const update = canUser('update', resource, id);
|
|
60
109
|
|
|
61
110
|
const _delete = canUser('delete', resource, id);
|
|
62
111
|
|
|
63
|
-
const isResolving = create.isResolving || update.isResolving || _delete.isResolving;
|
|
64
|
-
const hasResolved = create.hasResolved && update.hasResolved && _delete.hasResolved;
|
|
112
|
+
const isResolving = read.isResolving || create.isResolving || update.isResolving || _delete.isResolving;
|
|
113
|
+
const hasResolved = read.hasResolved && create.hasResolved && update.hasResolved && _delete.hasResolved;
|
|
65
114
|
let status = Status.Idle;
|
|
66
115
|
|
|
67
116
|
if (isResolving) {
|
|
@@ -70,13 +119,22 @@ export default function __experimentalUseResourcePermissions(resource, id) {
|
|
|
70
119
|
status = Status.Success;
|
|
71
120
|
}
|
|
72
121
|
|
|
73
|
-
return
|
|
122
|
+
return {
|
|
74
123
|
status,
|
|
75
124
|
isResolving,
|
|
125
|
+
hasResolved,
|
|
126
|
+
canRead: hasResolved && read.data,
|
|
76
127
|
canCreate: hasResolved && create.data,
|
|
77
128
|
canUpdate: hasResolved && update.data,
|
|
78
129
|
canDelete: hasResolved && _delete.data
|
|
79
|
-
}
|
|
130
|
+
};
|
|
80
131
|
}, [resource, id]);
|
|
81
132
|
}
|
|
133
|
+
export function __experimentalUseResourcePermissions(resource, id) {
|
|
134
|
+
deprecated(`wp.data.__experimentalUseResourcePermissions`, {
|
|
135
|
+
alternative: 'wp.data.useResourcePermissions',
|
|
136
|
+
since: '6.1'
|
|
137
|
+
});
|
|
138
|
+
return useResourcePermissions(resource, id);
|
|
139
|
+
}
|
|
82
140
|
//# sourceMappingURL=use-resource-permissions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"names":["store","coreStore","Status","useQuerySelect","
|
|
1
|
+
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-resource-permissions.ts"],"names":["deprecated","store","coreStore","Status","useQuerySelect","useResourcePermissions","resource","id","resolve","canUser","create","read","isResolving","hasResolved","status","Idle","Resolving","Success","canCreate","data","canRead","update","_delete","canUpdate","canDelete","__experimentalUseResourcePermissions","alternative","since"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;AACA,SAASC,MAAT,QAAuB,aAAvB;AACA,OAAOC,cAAP,MAA2B,oBAA3B;;AAiCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,sBAAT,CACdC,QADc,EAEdC,EAFc,EAG4B;AAC1C,SAAOH,cAAc,CAClBI,OAAF,IAAe;AACd,UAAM;AAAEC,MAAAA;AAAF,QAAcD,OAAO,CAAEN,SAAF,CAA3B;AACA,UAAMQ,MAAM,GAAGD,OAAO,CAAE,QAAF,EAAYH,QAAZ,CAAtB;;AACA,QAAK,CAAEC,EAAP,EAAY;AACX,YAAMI,IAAI,GAAGF,OAAO,CAAE,MAAF,EAAUH,QAAV,CAApB;AAEA,YAAMM,WAAW,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AhB;AACA;;AACD,WAAO;AACNH,MAAAA,MADM;AAENF,MAAAA,WAFM;AAGNC,MAAAA,WAHM;AAINO,MAAAA,OAAO,EAAEP,WAAW,IAAIF,IAAI,CAACQ,IAJvB;AAKND,MAAAA,SAAS,EAAEL,WAAW,IAAIH,MAAM,CAACS,IAL3B;AAMNI,MAAAA,SAAS,EAAEV,WAAW,IAAIQ,MAAM,CAACF,IAN3B;AAONK,MAAAA,SAAS,EAAEX,WAAW,IAAIS,OAAO,CAACH;AAP5B,KAAP;AASA,GAtDmB,EAuDpB,CAAEb,QAAF,EAAYC,EAAZ,CAvDoB,CAArB;AAyDA;AAED,OAAO,SAASkB,oCAAT,CACNnB,QADM,EAENC,EAFM,EAGL;AACDP,EAAAA,UAAU,CAAG,8CAAH,EAAkD;AAC3D0B,IAAAA,WAAW,EAAE,gCAD8C;AAE3DC,IAAAA,KAAK,EAAE;AAFoD,GAAlD,CAAV;AAIA,SAAOtB,sBAAsB,CAAEC,QAAF,EAAYC,EAAZ,CAA7B;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { store as coreStore } from '../';\nimport { Status } from './constants';\nimport useQuerySelect from './use-query-select';\n\ninterface GlobalResourcePermissionsResolution {\n\t/** Can the current user create new resources of this type? */\n\tcanCreate: boolean;\n}\ninterface SpecificResourcePermissionsResolution {\n\t/** Can the current user update resources of this type? */\n\tcanUpdate: boolean;\n\t/** Can the current user delete resources of this type? */\n\tcanDelete: boolean;\n}\ninterface ResolutionDetails {\n\t/** Resolution status */\n\tstatus: Status;\n\t/**\n\t * Is the data still being resolved?\n\t */\n\tisResolving: boolean;\n}\n\n/**\n * Is the data resolved by now?\n */\ntype HasResolved = boolean;\n\ntype ResourcePermissionsResolution< IdType > = [\n\tHasResolved,\n\tResolutionDetails &\n\t\tGlobalResourcePermissionsResolution &\n\t\t( IdType extends void ? SpecificResourcePermissionsResolution : {} )\n];\n\n/**\n * Resolves resource permissions.\n *\n * @param resource The resource in question, e.g. media.\n * @param id ID of a specific resource entry, if needed, e.g. 10.\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function PagesList() {\n * const { canCreate, isResolving } = useResourcePermissions( 'pages' );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PagesList />\n * ```\n *\n * @example\n * ```js\n * import { useResourcePermissions } from '@wordpress/core-data';\n *\n * function Page({ pageId }) {\n * const {\n * canCreate,\n * canUpdate,\n * canDelete,\n * isResolving\n * } = useResourcePermissions( 'pages', pageId );\n *\n * if ( isResolving ) {\n * return 'Loading ...';\n * }\n *\n * return (\n * <div>\n * {canCreate ? (<button>+ Create a new page</button>) : false}\n * {canUpdate ? (<button>Edit page</button>) : false}\n * {canDelete ? (<button>Delete page</button>) : false}\n * // ...\n * </div>\n * );\n * }\n *\n * // Rendered in the application:\n * // <Page pageId={ 15 } />\n * ```\n *\n * In the above example, when `PagesList` is rendered into an\n * application, the appropriate permissions and the resolution details will be retrieved from\n * the store state using `canUser()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template IdType\n */\nexport default function useResourcePermissions< IdType = void >(\n\tresource: string,\n\tid?: IdType\n): ResourcePermissionsResolution< IdType > {\n\treturn useQuerySelect(\n\t\t( resolve ) => {\n\t\t\tconst { canUser } = resolve( coreStore );\n\t\t\tconst create = canUser( 'create', resource );\n\t\t\tif ( ! id ) {\n\t\t\t\tconst read = canUser( 'read', resource );\n\n\t\t\t\tconst isResolving = create.isResolving || read.isResolving;\n\t\t\t\tconst hasResolved = create.hasResolved && read.hasResolved;\n\t\t\t\tlet status = Status.Idle;\n\t\t\t\tif ( isResolving ) {\n\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\tstatus,\n\t\t\t\t\tisResolving,\n\t\t\t\t\thasResolved,\n\t\t\t\t\tcanCreate: create.hasResolved && create.data,\n\t\t\t\t\tcanRead: read.hasResolved && read.data,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst read = canUser( 'read', resource, id );\n\t\t\tconst update = canUser( 'update', resource, id );\n\t\t\tconst _delete = canUser( 'delete', resource, id );\n\t\t\tconst isResolving =\n\t\t\t\tread.isResolving ||\n\t\t\t\tcreate.isResolving ||\n\t\t\t\tupdate.isResolving ||\n\t\t\t\t_delete.isResolving;\n\t\t\tconst hasResolved =\n\t\t\t\tread.hasResolved &&\n\t\t\t\tcreate.hasResolved &&\n\t\t\t\tupdate.hasResolved &&\n\t\t\t\t_delete.hasResolved;\n\n\t\t\tlet status = Status.Idle;\n\t\t\tif ( isResolving ) {\n\t\t\t\tstatus = Status.Resolving;\n\t\t\t} else if ( hasResolved ) {\n\t\t\t\tstatus = Status.Success;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus,\n\t\t\t\tisResolving,\n\t\t\t\thasResolved,\n\t\t\t\tcanRead: hasResolved && read.data,\n\t\t\t\tcanCreate: hasResolved && create.data,\n\t\t\t\tcanUpdate: hasResolved && update.data,\n\t\t\t\tcanDelete: hasResolved && _delete.data,\n\t\t\t};\n\t\t},\n\t\t[ resource, id ]\n\t);\n}\n\nexport function __experimentalUseResourcePermissions(\n\tresource: string,\n\tid?: unknown\n) {\n\tdeprecated( `wp.data.__experimentalUseResourcePermissions`, {\n\t\talternative: 'wp.data.useResourcePermissions',\n\t\tsince: '6.1',\n\t} );\n\treturn useResourcePermissions( resource, id );\n}\n"]}
|
package/build-module/index.js
CHANGED
|
@@ -89,9 +89,6 @@ const storeConfig = () => ({
|
|
|
89
89
|
export const store = createReduxStore(STORE_NAME, storeConfig());
|
|
90
90
|
register(store);
|
|
91
91
|
export { default as EntityProvider } from './entity-provider';
|
|
92
|
-
export { default as useEntityRecord } from './hooks/use-entity-record';
|
|
93
|
-
export { default as useEntityRecords } from './hooks/use-entity-records';
|
|
94
|
-
export { default as __experimentalUseResourcePermissions } from './hooks/use-resource-permissions';
|
|
95
92
|
export * from './entity-provider';
|
|
96
93
|
export * from './entity-types';
|
|
97
94
|
export * from './fetch';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/index.js"],"names":["createReduxStore","register","reducer","selectors","actions","resolvers","createLocksActions","rootEntitiesConfig","getMethodName","STORE_NAME","entitySelectors","reduce","result","entity","kind","name","state","key","query","getEntityRecord","getEntityRecords","entityResolvers","pluralMethodName","args","shouldInvalidate","action","entityActions","saveEntityRecord","deleteEntityRecord","storeConfig","store","default","EntityProvider"
|
|
1
|
+
{"version":3,"sources":["@wordpress/core-data/src/index.js"],"names":["createReduxStore","register","reducer","selectors","actions","resolvers","createLocksActions","rootEntitiesConfig","getMethodName","STORE_NAME","entitySelectors","reduce","result","entity","kind","name","state","key","query","getEntityRecord","getEntityRecords","entityResolvers","pluralMethodName","args","shouldInvalidate","action","entityActions","saveEntityRecord","deleteEntityRecord","storeConfig","store","default","EntityProvider"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gBAAT,EAA2BC,QAA3B,QAA2C,iBAA3C;AAEA;AACA;AACA;;AACA,OAAOC,OAAP,MAAoB,WAApB;AACA,OAAO,KAAKC,SAAZ,MAA2B,aAA3B;AACA,OAAO,KAAKC,OAAZ,MAAyB,WAAzB;AACA,OAAO,KAAKC,SAAZ,MAA2B,aAA3B;AACA,OAAOC,kBAAP,MAA+B,iBAA/B;AACA,SAASC,kBAAT,EAA6BC,aAA7B,QAAkD,YAAlD;AACA,SAASC,UAAT,QAA2B,QAA3B,C,CAEA;AACA;AACA;AACA;;AAEA,MAAMC,eAAe,GAAGH,kBAAkB,CAACI,MAAnB,CAA2B,CAAEC,MAAF,EAAUC,MAAV,KAAsB;AACxE,QAAM;AAAEC,IAAAA,IAAF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B,WAAW,EAAzB,CAA9B;AAEP5B,QAAQ,CAAE6B,KAAF,CAAR;AAEA,SAASC,OAAO,IAAIC,cAApB,QAA0C,mBAA1C;AACA,cAAc,mBAAd;AACA,cAAc,gBAAd;AACA,cAAc,SAAd;AACA,cAAc,SAAd","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as selectors from './selectors';\nimport * as actions from './actions';\nimport * as resolvers from './resolvers';\nimport createLocksActions from './locks/actions';\nimport { rootEntitiesConfig, getMethodName } from './entities';\nimport { STORE_NAME } from './name';\n\n// The entity selectors/resolvers and actions are shortcuts to their generic equivalents\n// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)\n// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy...\n// The \"kind\" and the \"name\" of the entity are combined to generate these shortcuts.\n\nconst entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => {\n\tconst { kind, name } = entity;\n\tresult[ getMethodName( kind, name ) ] = ( state, key, query ) =>\n\t\tselectors.getEntityRecord( state, kind, name, key, query );\n\tresult[ getMethodName( kind, name, 'get', true ) ] = ( state, query ) =>\n\t\tselectors.getEntityRecords( state, kind, name, query );\n\treturn result;\n}, {} );\n\nconst entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => {\n\tconst { kind, name } = entity;\n\tresult[ getMethodName( kind, name ) ] = ( key, query ) =>\n\t\tresolvers.getEntityRecord( kind, name, key, query );\n\tconst pluralMethodName = getMethodName( kind, name, 'get', true );\n\tresult[ pluralMethodName ] = ( ...args ) =>\n\t\tresolvers.getEntityRecords( kind, name, ...args );\n\tresult[ pluralMethodName ].shouldInvalidate = ( action ) =>\n\t\tresolvers.getEntityRecords.shouldInvalidate( action, kind, name );\n\treturn result;\n}, {} );\n\nconst entityActions = rootEntitiesConfig.reduce( ( result, entity ) => {\n\tconst { kind, name } = entity;\n\tresult[ getMethodName( kind, name, 'save' ) ] = ( key ) =>\n\t\tactions.saveEntityRecord( kind, name, key );\n\tresult[ getMethodName( kind, name, 'delete' ) ] = ( key, query ) =>\n\t\tactions.deleteEntityRecord( kind, name, key, query );\n\treturn result;\n}, {} );\n\nconst storeConfig = () => ( {\n\treducer,\n\tactions: { ...actions, ...entityActions, ...createLocksActions() },\n\tselectors: { ...selectors, ...entitySelectors },\n\tresolvers: { ...resolvers, ...entityResolvers },\n} );\n\n/**\n * Store definition for the code data namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, storeConfig() );\n\nregister( store );\n\nexport { default as EntityProvider } from './entity-provider';\nexport * from './entity-provider';\nexport * from './entity-types';\nexport * from './fetch';\nexport * from './hooks';\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { camelCase
|
|
4
|
+
import { camelCase } from 'change-case';
|
|
5
5
|
/**
|
|
6
6
|
* WordPress dependencies
|
|
7
7
|
*/
|
|
@@ -52,7 +52,8 @@ export const getCurrentUser = () => async _ref2 => {
|
|
|
52
52
|
* @param {string} name Entity name.
|
|
53
53
|
* @param {number|string} key Record's key
|
|
54
54
|
* @param {Object|undefined} query Optional object of query parameters to
|
|
55
|
-
* include with request.
|
|
55
|
+
* include with request. If requesting specific
|
|
56
|
+
* fields, fields must always include the ID.
|
|
56
57
|
*/
|
|
57
58
|
|
|
58
59
|
export const getEntityRecord = function (kind, name) {
|
|
@@ -64,10 +65,7 @@ export const getEntityRecord = function (kind, name) {
|
|
|
64
65
|
dispatch
|
|
65
66
|
} = _ref3;
|
|
66
67
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
67
|
-
const entityConfig = find(
|
|
68
|
-
kind,
|
|
69
|
-
name
|
|
70
|
-
});
|
|
68
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
71
69
|
|
|
72
70
|
if (!entityConfig || entityConfig !== null && entityConfig !== void 0 && entityConfig.__experimentalNoFetch) {
|
|
73
71
|
return;
|
|
@@ -83,7 +81,7 @@ export const getEntityRecord = function (kind, name) {
|
|
|
83
81
|
// records are stored by ID reference. Thus, fields must always include
|
|
84
82
|
// the ID.
|
|
85
83
|
query = { ...query,
|
|
86
|
-
_fields:
|
|
84
|
+
_fields: [...new Set([...(getNormalizedCommaSeparable(query._fields) || []), entityConfig.key || DEFAULT_ENTITY_KEY])].join()
|
|
87
85
|
};
|
|
88
86
|
} // Disable reason: While true that an early return could leave `path`
|
|
89
87
|
// unused, it's important that path is derived using the query prior to
|
|
@@ -135,7 +133,8 @@ export const getEditedEntityRecord = forwardResolver('getEntityRecord');
|
|
|
135
133
|
*
|
|
136
134
|
* @param {string} kind Entity kind.
|
|
137
135
|
* @param {string} name Entity name.
|
|
138
|
-
* @param {Object?} query Query Object.
|
|
136
|
+
* @param {Object?} query Query Object. If requesting specific fields, fields
|
|
137
|
+
* must always include the ID.
|
|
139
138
|
*/
|
|
140
139
|
|
|
141
140
|
export const getEntityRecords = function (kind, name) {
|
|
@@ -145,10 +144,7 @@ export const getEntityRecords = function (kind, name) {
|
|
|
145
144
|
dispatch
|
|
146
145
|
} = _ref4;
|
|
147
146
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
148
|
-
const entityConfig = find(
|
|
149
|
-
kind,
|
|
150
|
-
name
|
|
151
|
-
});
|
|
147
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
152
148
|
|
|
153
149
|
if (!entityConfig || entityConfig !== null && entityConfig !== void 0 && entityConfig.__experimentalNoFetch) {
|
|
154
150
|
return;
|
|
@@ -166,7 +162,7 @@ export const getEntityRecords = function (kind, name) {
|
|
|
166
162
|
// records are stored by ID reference. Thus, fields must always include
|
|
167
163
|
// the ID.
|
|
168
164
|
query = { ...query,
|
|
169
|
-
_fields:
|
|
165
|
+
_fields: [...new Set([...(getNormalizedCommaSeparable(query._fields) || []), entityConfig.key || DEFAULT_ENTITY_KEY])].join()
|
|
170
166
|
};
|
|
171
167
|
}
|
|
172
168
|
|
|
@@ -272,7 +268,7 @@ export const getEmbedPreview = url => async _ref6 => {
|
|
|
272
268
|
*/
|
|
273
269
|
|
|
274
270
|
export const canUser = (action, resource, id) => async _ref7 => {
|
|
275
|
-
var _response$headers;
|
|
271
|
+
var _response$headers, _allowHeader$includes;
|
|
276
272
|
|
|
277
273
|
let {
|
|
278
274
|
dispatch
|
|
@@ -308,8 +304,8 @@ export const canUser = (action, resource, id) => async _ref7 => {
|
|
|
308
304
|
|
|
309
305
|
|
|
310
306
|
const allowHeader = (_response$headers = response.headers) === null || _response$headers === void 0 ? void 0 : _response$headers.get('allow');
|
|
311
|
-
const key =
|
|
312
|
-
const isAllowed = includes(allowHeader, method);
|
|
307
|
+
const key = [action, resource, id].filter(Boolean).join('/');
|
|
308
|
+
const isAllowed = (allowHeader === null || allowHeader === void 0 ? void 0 : (_allowHeader$includes = allowHeader.includes) === null || _allowHeader$includes === void 0 ? void 0 : _allowHeader$includes.call(allowHeader, method)) || (allowHeader === null || allowHeader === void 0 ? void 0 : allowHeader.allow) === method;
|
|
313
309
|
dispatch.receiveUserPermission(key, isAllowed);
|
|
314
310
|
};
|
|
315
311
|
/**
|
|
@@ -326,10 +322,7 @@ export const canUserEditEntityRecord = (kind, name, recordId) => async _ref8 =>
|
|
|
326
322
|
dispatch
|
|
327
323
|
} = _ref8;
|
|
328
324
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
329
|
-
const entityConfig = find(
|
|
330
|
-
kind,
|
|
331
|
-
name
|
|
332
|
-
});
|
|
325
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
333
326
|
|
|
334
327
|
if (!entityConfig) {
|
|
335
328
|
return;
|
|
@@ -424,6 +417,8 @@ __experimentalGetTemplateForLink.shouldInvalidate = action => {
|
|
|
424
417
|
};
|
|
425
418
|
|
|
426
419
|
export const __experimentalGetCurrentGlobalStylesId = () => async _ref13 => {
|
|
420
|
+
var _activeThemes$, _activeThemes$$_links, _activeThemes$$_links2, _activeThemes$$_links3;
|
|
421
|
+
|
|
427
422
|
let {
|
|
428
423
|
dispatch,
|
|
429
424
|
resolveSelect
|
|
@@ -431,7 +426,7 @@ export const __experimentalGetCurrentGlobalStylesId = () => async _ref13 => {
|
|
|
431
426
|
const activeThemes = await resolveSelect.getEntityRecords('root', 'theme', {
|
|
432
427
|
status: 'active'
|
|
433
428
|
});
|
|
434
|
-
const globalStylesURL =
|
|
429
|
+
const globalStylesURL = activeThemes === null || activeThemes === void 0 ? void 0 : (_activeThemes$ = activeThemes[0]) === null || _activeThemes$ === void 0 ? void 0 : (_activeThemes$$_links = _activeThemes$._links) === null || _activeThemes$$_links === void 0 ? void 0 : (_activeThemes$$_links2 = _activeThemes$$_links['wp:user-global-styles']) === null || _activeThemes$$_links2 === void 0 ? void 0 : (_activeThemes$$_links3 = _activeThemes$$_links2[0]) === null || _activeThemes$$_links3 === void 0 ? void 0 : _activeThemes$$_links3.href;
|
|
435
430
|
|
|
436
431
|
if (globalStylesURL) {
|
|
437
432
|
const globalStylesObject = await apiFetch({
|
|
@@ -472,16 +467,19 @@ export const getBlockPatterns = () => async _ref16 => {
|
|
|
472
467
|
const restPatterns = await apiFetch({
|
|
473
468
|
path: '/wp/v2/block-patterns/patterns'
|
|
474
469
|
});
|
|
475
|
-
const patterns = map(
|
|
470
|
+
const patterns = restPatterns === null || restPatterns === void 0 ? void 0 : restPatterns.map(pattern => Object.fromEntries(Object.entries(pattern).map(_ref17 => {
|
|
471
|
+
let [key, value] = _ref17;
|
|
472
|
+
return [camelCase(key), value];
|
|
473
|
+
})));
|
|
476
474
|
dispatch({
|
|
477
475
|
type: 'RECEIVE_BLOCK_PATTERNS',
|
|
478
476
|
patterns
|
|
479
477
|
});
|
|
480
478
|
};
|
|
481
|
-
export const getBlockPatternCategories = () => async
|
|
479
|
+
export const getBlockPatternCategories = () => async _ref18 => {
|
|
482
480
|
let {
|
|
483
481
|
dispatch
|
|
484
|
-
} =
|
|
482
|
+
} = _ref18;
|
|
485
483
|
const categories = await apiFetch({
|
|
486
484
|
path: '/wp/v2/block-patterns/categories'
|
|
487
485
|
});
|