@wordpress/core-data 4.13.0 → 5.0.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 +12 -0
- package/README.md +148 -65
- package/build/entities.js +38 -51
- 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 +10 -6
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +2 -0
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js +5 -2
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/hooks/use-resource-permissions.js +74 -11
- package/build/hooks/use-resource-permissions.js.map +1 -1
- package/build/index.js +1 -30
- package/build/index.js.map +1 -1
- package/build/resolvers.js +59 -39
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +9 -46
- package/build/selectors.js.map +1 -1
- package/build-module/entities.js +38 -52
- 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 +10 -6
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +2 -0
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js +4 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/hooks/use-resource-permissions.js +70 -10
- package/build-module/hooks/use-resource-permissions.js.map +1 -1
- package/build-module/index.js +0 -5
- package/build-module/index.js.map +1 -1
- package/build-module/resolvers.js +59 -39
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +7 -44
- package/build-module/selectors.js.map +1 -1
- package/build-types/actions.d.ts +188 -0
- package/build-types/actions.d.ts.map +1 -0
- package/build-types/batch/create-batch.d.ts +71 -0
- package/build-types/batch/create-batch.d.ts.map +1 -0
- package/build-types/batch/default-processor.d.ts +11 -0
- package/build-types/batch/default-processor.d.ts.map +1 -0
- package/build-types/batch/index.d.ts +3 -0
- package/build-types/batch/index.d.ts.map +1 -0
- package/build-types/entities.d.ts +128 -0
- package/build-types/entities.d.ts.map +1 -0
- package/build-types/entity-provider.d.ts +68 -0
- package/build-types/entity-provider.d.ts.map +1 -0
- package/build-types/entity-types/attachment.d.ts +121 -0
- package/build-types/entity-types/attachment.d.ts.map +1 -0
- package/build-types/entity-types/base-entity-records.d.ts +37 -0
- package/build-types/entity-types/base-entity-records.d.ts.map +1 -0
- package/build-types/entity-types/comment.d.ts +82 -0
- package/build-types/entity-types/comment.d.ts.map +1 -0
- package/build-types/entity-types/helpers.d.ts +123 -0
- package/build-types/entity-types/helpers.d.ts.map +1 -0
- package/build-types/entity-types/index.d.ts +64 -0
- package/build-types/entity-types/index.d.ts.map +1 -0
- package/build-types/entity-types/menu-location.d.ts +25 -0
- package/build-types/entity-types/menu-location.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu-item.d.ts +88 -0
- package/build-types/entity-types/nav-menu-item.d.ts.map +1 -0
- package/build-types/entity-types/nav-menu.d.ts +45 -0
- package/build-types/entity-types/nav-menu.d.ts.map +1 -0
- package/build-types/entity-types/page.d.ts +120 -0
- package/build-types/entity-types/page.d.ts.map +1 -0
- package/build-types/entity-types/plugin.d.ts +62 -0
- package/build-types/entity-types/plugin.d.ts.map +1 -0
- package/build-types/entity-types/post.d.ts +128 -0
- package/build-types/entity-types/post.d.ts.map +1 -0
- package/build-types/entity-types/settings.d.ts +89 -0
- package/build-types/entity-types/settings.d.ts.map +1 -0
- package/build-types/entity-types/sidebar.d.ts +55 -0
- package/build-types/entity-types/sidebar.d.ts.map +1 -0
- package/build-types/entity-types/taxonomy.d.ts +83 -0
- package/build-types/entity-types/taxonomy.d.ts.map +1 -0
- package/build-types/entity-types/theme.d.ts +206 -0
- package/build-types/entity-types/theme.d.ts.map +1 -0
- package/build-types/entity-types/type.d.ts +71 -0
- package/build-types/entity-types/type.d.ts.map +1 -0
- package/build-types/entity-types/user.d.ts +93 -0
- package/build-types/entity-types/user.d.ts.map +1 -0
- package/build-types/entity-types/widget-type.d.ts +33 -0
- package/build-types/entity-types/widget-type.d.ts.map +1 -0
- package/build-types/entity-types/widget.d.ts +59 -0
- package/build-types/entity-types/widget.d.ts.map +1 -0
- package/build-types/entity-types/wp-template-part.d.ts +80 -0
- package/build-types/entity-types/wp-template-part.d.ts.map +1 -0
- package/build-types/entity-types/wp-template.d.ts +80 -0
- package/build-types/entity-types/wp-template.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts +139 -0
- package/build-types/fetch/__experimental-fetch-link-suggestions.d.ts.map +1 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts +35 -0
- package/build-types/fetch/__experimental-fetch-url-data.d.ts.map +1 -0
- package/build-types/fetch/index.d.ts +3 -0
- package/build-types/fetch/index.d.ts.map +1 -0
- package/build-types/hooks/constants.d.ts +7 -0
- package/build-types/hooks/constants.d.ts.map +1 -0
- package/build-types/hooks/index.d.ts +4 -0
- package/build-types/hooks/index.d.ts.map +1 -0
- package/build-types/hooks/memoize.d.ts +3 -0
- package/build-types/hooks/memoize.d.ts.map +1 -0
- package/build-types/hooks/use-entity-record.d.ts +124 -0
- package/build-types/hooks/use-entity-record.d.ts.map +1 -0
- package/build-types/hooks/use-entity-records.d.ts +49 -0
- package/build-types/hooks/use-entity-records.d.ts.map +1 -0
- package/build-types/hooks/use-query-select.d.ts +46 -0
- package/build-types/hooks/use-query-select.d.ts.map +1 -0
- package/build-types/hooks/use-resource-permissions.d.ts +99 -0
- package/build-types/hooks/use-resource-permissions.d.ts.map +1 -0
- package/build-types/index.d.ts +131 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/locks/actions.d.ts +7 -0
- package/build-types/locks/actions.d.ts.map +1 -0
- package/build-types/locks/engine.d.ts +5 -0
- package/build-types/locks/engine.d.ts.map +1 -0
- package/build-types/locks/reducer.d.ts +10 -0
- package/build-types/locks/reducer.d.ts.map +1 -0
- package/build-types/locks/selectors.d.ts +5 -0
- package/build-types/locks/selectors.d.ts.map +1 -0
- package/build-types/locks/utils.d.ts +8 -0
- package/build-types/locks/utils.d.ts.map +1 -0
- package/build-types/name.d.ts +8 -0
- package/build-types/name.d.ts.map +1 -0
- package/build-types/queried-data/actions.d.ts +32 -0
- package/build-types/queried-data/actions.d.ts.map +1 -0
- package/build-types/queried-data/get-query-parts.d.ts +60 -0
- package/build-types/queried-data/get-query-parts.d.ts.map +1 -0
- package/build-types/queried-data/index.d.ts +4 -0
- package/build-types/queried-data/index.d.ts.map +1 -0
- package/build-types/queried-data/reducer.d.ts +54 -0
- package/build-types/queried-data/reducer.d.ts.map +1 -0
- package/build-types/queried-data/selectors.d.ts +16 -0
- package/build-types/queried-data/selectors.d.ts.map +1 -0
- package/build-types/reducer.d.ts +156 -0
- package/build-types/reducer.d.ts.map +1 -0
- package/build-types/resolvers.d.ts +75 -0
- package/build-types/resolvers.d.ts.map +1 -0
- package/build-types/selectors.d.ts +524 -0
- package/build-types/selectors.d.ts.map +1 -0
- package/build-types/types.d.ts +4 -0
- package/build-types/types.d.ts.map +1 -0
- package/build-types/utils/conservative-map-item.d.ts +12 -0
- package/build-types/utils/conservative-map-item.d.ts.map +1 -0
- package/build-types/utils/forward-resolver.d.ts +10 -0
- package/build-types/utils/forward-resolver.d.ts.map +1 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts +12 -0
- package/build-types/utils/get-normalized-comma-separable.d.ts.map +1 -0
- package/build-types/utils/if-matching-action.d.ts +14 -0
- package/build-types/utils/if-matching-action.d.ts.map +1 -0
- package/build-types/utils/index.d.ts +9 -0
- package/build-types/utils/index.d.ts.map +1 -0
- package/build-types/utils/is-raw-attribute.d.ts +10 -0
- package/build-types/utils/is-raw-attribute.d.ts.map +1 -0
- package/build-types/utils/on-sub-key.d.ts +4 -0
- package/build-types/utils/on-sub-key.d.ts.map +1 -0
- package/build-types/utils/replace-action.d.ts +13 -0
- package/build-types/utils/replace-action.d.ts.map +1 -0
- package/build-types/utils/with-weak-map-cache.d.ts +12 -0
- package/build-types/utils/with-weak-map-cache.d.ts.map +1 -0
- package/package.json +13 -11
- package/src/entities.js +325 -0
- package/src/entity-types/attachment.ts +3 -4
- package/src/entity-types/comment.ts +3 -4
- package/src/entity-types/index.ts +31 -88
- package/src/entity-types/menu-location.ts +3 -4
- package/src/entity-types/nav-menu-item.ts +3 -4
- package/src/entity-types/nav-menu.ts +3 -3
- package/src/entity-types/page.ts +3 -3
- package/src/entity-types/plugin.ts +3 -3
- package/src/entity-types/post.ts +3 -3
- package/src/entity-types/settings.ts +3 -3
- package/src/entity-types/sidebar.ts +3 -4
- package/src/entity-types/taxonomy.ts +3 -4
- package/src/entity-types/theme.ts +7 -3
- package/src/entity-types/type.ts +3 -3
- package/src/entity-types/user.ts +3 -3
- package/src/entity-types/widget-type.ts +3 -4
- package/src/entity-types/widget.ts +3 -3
- package/src/entity-types/wp-template-part.ts +3 -4
- package/src/entity-types/wp-template.ts +3 -4
- package/src/hooks/index.ts +4 -0
- package/src/hooks/test/use-entity-record.js +41 -1
- package/src/hooks/test/use-resource-permissions.js +32 -36
- package/src/hooks/use-entity-record.ts +18 -6
- package/src/hooks/use-entity-records.ts +2 -0
- package/src/hooks/use-query-select.ts +4 -1
- package/src/hooks/use-resource-permissions.ts +84 -20
- package/src/index.js +0 -5
- package/src/resolvers.js +80 -45
- package/src/selectors.ts +202 -341
- package/src/test/resolvers.js +118 -4
- package/tsconfig.json +21 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/build/entity-types/entities.js +0 -6
- package/build/entity-types/entities.js.map +0 -1
- package/build-module/entity-types/entities.js +0 -2
- package/build-module/entity-types/entities.js.map +0 -1
- package/src/entities.ts +0 -548
- package/src/entity-types/entities.ts +0 -130
|
@@ -14,6 +14,8 @@ import { store as coreStore } from '../';
|
|
|
14
14
|
/**
|
|
15
15
|
* Resolves the specified entity record.
|
|
16
16
|
*
|
|
17
|
+
* @since 6.1.0 Introduced in WordPress core.
|
|
18
|
+
*
|
|
17
19
|
* @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
|
|
18
20
|
* @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
|
|
19
21
|
* @param recordId ID of the requested entity record.
|
|
@@ -42,8 +44,8 @@ import { store as coreStore } from '../';
|
|
|
42
44
|
*
|
|
43
45
|
* @example
|
|
44
46
|
* ```js
|
|
45
|
-
* import { useState } from '@wordpress/data';
|
|
46
47
|
* import { useDispatch } from '@wordpress/data';
|
|
48
|
+
* import { useCallback } from '@wordpress/element';
|
|
47
49
|
* import { __ } from '@wordpress/i18n';
|
|
48
50
|
* import { TextControl } from '@wordpress/components';
|
|
49
51
|
* import { store as noticeStore } from '@wordpress/notices';
|
|
@@ -51,17 +53,19 @@ import { store as coreStore } from '../';
|
|
|
51
53
|
*
|
|
52
54
|
* function PageRenameForm( { id } ) {
|
|
53
55
|
* const page = useEntityRecord( 'postType', 'page', id );
|
|
54
|
-
* const [ title, setTitle ] = useState( () => page.record.title.rendered );
|
|
55
56
|
* const { createSuccessNotice, createErrorNotice } =
|
|
56
57
|
* useDispatch( noticeStore );
|
|
57
58
|
*
|
|
59
|
+
* const setTitle = useCallback( ( title ) => {
|
|
60
|
+
* page.edit( { title } );
|
|
61
|
+
* }, [ page.edit ] );
|
|
62
|
+
*
|
|
58
63
|
* if ( page.isResolving ) {
|
|
59
64
|
* return 'Loading...';
|
|
60
65
|
* }
|
|
61
66
|
*
|
|
62
67
|
* async function onRename( event ) {
|
|
63
68
|
* event.preventDefault();
|
|
64
|
-
* page.edit( { title } );
|
|
65
69
|
* try {
|
|
66
70
|
* await page.save();
|
|
67
71
|
* createSuccessNotice( __( 'Page renamed.' ), {
|
|
@@ -76,7 +80,7 @@ import { store as coreStore } from '../';
|
|
|
76
80
|
* <form onSubmit={ onRename }>
|
|
77
81
|
* <TextControl
|
|
78
82
|
* label={ __( 'Name' ) }
|
|
79
|
-
* value={ title }
|
|
83
|
+
* value={ page.editedRecord.title }
|
|
80
84
|
* onChange={ setTitle }
|
|
81
85
|
* />
|
|
82
86
|
* <button type="submit">{ __( 'Save' ) }</button>
|
|
@@ -117,8 +121,8 @@ export default function useEntityRecord(kind, name, recordId) {
|
|
|
117
121
|
editedRecord,
|
|
118
122
|
hasEdits
|
|
119
123
|
} = useSelect(select => ({
|
|
120
|
-
editedRecord: select(coreStore).getEditedEntityRecord(),
|
|
121
|
-
hasEdits: select(coreStore).hasEditsForEntityRecord()
|
|
124
|
+
editedRecord: select(coreStore).getEditedEntityRecord(kind, name, recordId),
|
|
125
|
+
hasEdits: select(coreStore).hasEditsForEntityRecord(kind, name, recordId)
|
|
122
126
|
}), [kind, name, recordId]);
|
|
123
127
|
const {
|
|
124
128
|
data: record,
|
|
@@ -1 +1 @@
|
|
|
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,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,
|
|
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;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,E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AV;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 * @since 6.1.0 Introduced in WordPress core.\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"]}
|
|
@@ -13,6 +13,8 @@ const EMPTY_ARRAY = [];
|
|
|
13
13
|
/**
|
|
14
14
|
* Resolves the specified entity records.
|
|
15
15
|
*
|
|
16
|
+
* @since 6.1.0 Introduced in WordPress core.
|
|
17
|
+
*
|
|
16
18
|
* @param kind Kind of the entity, e.g. `root` or a `postType`. See rootEntitiesConfig in ../entities.ts for a list of available kinds.
|
|
17
19
|
* @param name Name of the entity, e.g. `plugin` or a `post`. See rootEntitiesConfig in ../entities.ts for a list of available names.
|
|
18
20
|
* @param queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"names":["addQueryArgs","deprecated","useQuerySelect","store","coreStore","EMPTY_ARRAY","useEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","data","records","rest","query","getEntityRecords","__experimentalUseEntityRecords","alternative","since"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAT,QAA6B,gBAA7B;AACA,OAAOC,UAAP,MAAuB,uBAAvB;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,oBAA3B;AACA,SAASC,KAAK,IAAIC,SAAlB,QAAmC,KAAnC;AAWA,MAAMC,WAAW,GAAG,EAApB;AAEA;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,gBAAT,CACdC,IADc,EAEdC,IAFc,EAK0B;AAAA,MAFxCC,SAEwC,uEAFD,EAEC;AAAA,MADxCC,OACwC,uEADrB;AAAEC,IAAAA,OAAO,EAAE;AAAX,GACqB;AACxC;AACA;AACA;AACA;AACA,QAAMC,aAAa,GAAGZ,YAAY,CAAE,EAAF,EAAMS,SAAN,CAAlC;AAEA,QAAM;AAAEI,IAAAA,IAAI,EAAEC,OAAR;AAAiB,OAAGC;AAApB,MAA6Bb,cAAc,CAC9Cc,KAAF,IAAa;AACZ,QAAK,CAAEN,OAAO,CAACC,OAAf,EAAyB;AACxB,aAAO;AACN;AACAE,QAAAA,IAAI,EAAER;AAFA,OAAP;AAIA;;AACD,WAAOW,KAAK,CAAEZ,SAAF,CAAL,CAAmBa,gBAAnB,CAAqCV,IAArC,EAA2CC,IAA3C,EAAiDC,SAAjD,CAAP;AACA,GAT+C,EAUhD,CAAEF,IAAF,EAAQC,IAAR,EAAcI,aAAd,EAA6BF,OAAO,CAACC,OAArC,CAVgD,CAAjD;AAaA,SAAO;AACNG,IAAAA,OADM;AAEN,OAAGC;AAFG,GAAP;AAIA;AAED,OAAO,SAASG,8BAAT,CACNX,IADM,EAENC,IAFM,EAGNC,SAHM,EAINC,OAJM,EAKL;AACDT,EAAAA,UAAU,CAAG,wCAAH,EAA4C;AACrDkB,IAAAA,WAAW,EAAE,0BADwC;AAErDC,IAAAA,KAAK,EAAE;AAF8C,GAA5C,CAAV;AAIA,SAAOd,gBAAgB,CAAEC,IAAF,EAAQC,IAAR,EAAcC,SAAd,EAAyBC,OAAzB,CAAvB;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options, EntityRecordResolution } from './use-entity-record';\n\ntype EntityRecordsResolution< RecordType > = Omit<\n\tEntityRecordResolution< RecordType >,\n\t'record'\n> & {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n};\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\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 queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-entity-records.ts"],"names":["addQueryArgs","deprecated","useQuerySelect","store","coreStore","EMPTY_ARRAY","useEntityRecords","kind","name","queryArgs","options","enabled","queryAsString","data","records","rest","query","getEntityRecords","__experimentalUseEntityRecords","alternative","since"],"mappings":"AAA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A4C;AACrDkB,IAAAA,WAAW,EAAE,0BADwC;AAErDC,IAAAA,KAAK,EAAE;AAF8C,GAA5C,CAAV;AAIA,SAAOd,gBAAgB,CAAEC,IAAF,EAAQC,IAAR,EAAcC,SAAd,EAAyBC,OAAzB,CAAvB;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { addQueryArgs } from '@wordpress/url';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport useQuerySelect from './use-query-select';\nimport { store as coreStore } from '../';\nimport type { Options, EntityRecordResolution } from './use-entity-record';\n\ntype EntityRecordsResolution< RecordType > = Omit<\n\tEntityRecordResolution< RecordType >,\n\t'record'\n> & {\n\t/** The requested entity record */\n\trecords: RecordType[] | null;\n};\n\nconst EMPTY_ARRAY = [];\n\n/**\n * Resolves the specified entity records.\n *\n * @since 6.1.0 Introduced in WordPress core.\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 queryArgs Optional HTTP query description for how to fetch the data, passed to the requested API endpoint.\n * @param options Optional hook options.\n * @example\n * ```js\n * import { useEntityRecord } from '@wordpress/core-data';\n *\n * function PageTitlesList() {\n * const { records, isResolving } = useEntityRecords( 'postType', 'page' );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return (\n * <ul>\n * {records.map(( page ) => (\n * <li>{ page.title }</li>\n * ))}\n * </ul>\n * );\n * }\n *\n * // Rendered in the application:\n * // <PageTitlesList />\n * ```\n *\n * In the above example, when `PageTitlesList` is rendered into an\n * application, the list of records and the resolution details will be retrieved from\n * the store state using `getEntityRecords()`, or resolved if missing.\n *\n * @return Entity records data.\n * @template RecordType\n */\nexport default function useEntityRecords< RecordType >(\n\tkind: string,\n\tname: string,\n\tqueryArgs: Record< string, unknown > = {},\n\toptions: Options = { enabled: true }\n): EntityRecordsResolution< RecordType > {\n\t// Serialize queryArgs to a string that can be safely used as a React dep.\n\t// We can't just pass queryArgs as one of the deps, because if it is passed\n\t// as an object literal, then it will be a different object on each call even\n\t// if the values remain the same.\n\tconst queryAsString = addQueryArgs( '', queryArgs );\n\n\tconst { data: records, ...rest } = useQuerySelect(\n\t\t( query ) => {\n\t\t\tif ( ! options.enabled ) {\n\t\t\t\treturn {\n\t\t\t\t\t// Avoiding returning a new reference on every execution.\n\t\t\t\t\tdata: EMPTY_ARRAY,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn query( coreStore ).getEntityRecords( kind, name, queryArgs );\n\t\t},\n\t\t[ kind, name, queryAsString, options.enabled ]\n\t);\n\n\treturn {\n\t\trecords,\n\t\t...rest,\n\t};\n}\n\nexport function __experimentalUseEntityRecords(\n\tkind: string,\n\tname: string,\n\tqueryArgs: any,\n\toptions: any\n) {\n\tdeprecated( `wp.data.__experimentalUseEntityRecords`, {\n\t\talternative: 'wp.data.useEntityRecords',\n\t\tsince: '6.1',\n\t} );\n\treturn useEntityRecords( kind, name, queryArgs, options );\n}\n"]}
|
|
@@ -14,6 +14,9 @@ export const META_SELECTORS = ['getIsResolving', 'hasStartedResolution', 'hasFin
|
|
|
14
14
|
* Like useSelect, but the selectors return objects containing
|
|
15
15
|
* both the original data AND the resolution info.
|
|
16
16
|
*
|
|
17
|
+
* @since 6.1.0 Introduced in WordPress core.
|
|
18
|
+
* @private
|
|
19
|
+
*
|
|
17
20
|
* @param {Function} mapQuerySelect see useSelect
|
|
18
21
|
* @param {Array} deps see useSelect
|
|
19
22
|
*
|
|
@@ -50,7 +53,7 @@ export const META_SELECTORS = ['getIsResolving', 'hasStartedResolution', 'hasFin
|
|
|
50
53
|
*
|
|
51
54
|
* @return {QuerySelectResponse} Queried data.
|
|
52
55
|
*/
|
|
53
|
-
export default function
|
|
56
|
+
export default function useQuerySelect(mapQuerySelect, deps) {
|
|
54
57
|
return useSelect((select, registry) => {
|
|
55
58
|
const resolve = store => enrichSelectors(select(store));
|
|
56
59
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-query-select.ts"],"names":["useSelect","memoize","Status","META_SELECTORS","
|
|
1
|
+
{"version":3,"sources":["@wordpress/core-data/src/hooks/use-query-select.ts"],"names":["useSelect","memoize","Status","META_SELECTORS","useQuerySelect","mapQuerySelect","deps","select","registry","resolve","store","enrichSelectors","selectors","resolvers","selectorName","includes","Object","defineProperty","get","getIsResolving","hasFinishedResolution","args","isResolving","hasResolved","data","status","Resolving","Success","Error","Idle"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,iBAA1B;AAEA;AACA;AACA;;AACA,OAAOC,OAAP,MAAoB,WAApB;AACA,SAASC,MAAT,QAAuB,aAAvB;AAEA,OAAO,MAAMC,cAAc,GAAG,CAC7B,gBAD6B,EAE7B,sBAF6B,EAG7B,uBAH6B,EAI7B,aAJ6B,EAK7B,oBAL6B,CAAvB;;AAsBP;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,cAAT,CAAyBC,cAAzB,EAAyCC,IAAzC,EAAgD;AAC9D,SAAON,SAAS,CAAE,CAAEO,MAAF,EAAUC,QAAV,KAAwB;AACzC,UAAMC,OAAO,GAAKC,KAAF,IAAaC,eAAe,CAAEJ,MAAM,CAAEG,KAAF,CAAR,CAA5C;;AACA,WAAOL,cAAc,CAAEI,OAAF,EAAWD,QAAX,CAArB;AACA,GAHe,EAGbF,IAHa,CAAhB;AAIA;;AAYD;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMK,eAAe,GAAGV,OAAO,CAAMW,SAAF,IAAiB;AACnD,QAAMC,SAAS,GAAG,EAAlB;;AACA,OAAM,MAAMC,YAAZ,IAA4BF,SAA5B,EAAwC;AACvC,QAAKT,cAAc,CAACY,QAAf,CAAyBD,YAAzB,CAAL,EAA+C;AAC9C;AACA;;AACDE,IAAAA,MAAM,CAACC,cAAP,CAAuBJ,SAAvB,EAAkCC,YAAlC,EAAgD;AAC/CI,MAAAA,GAAG,EACF,MACA,YAA0B;AACzB,cAAM;AAAEC,UAAAA,cAAF;AAAkBC,UAAAA;AAAlB,YAA4CR,SAAlD;;AADyB,0CAArBS,IAAqB;AAArBA,UAAAA,IAAqB;AAAA;;AAEzB,cAAMC,WAAW,GAAG,CAAC,CAAEH,cAAc,CAAEL,YAAF,EAAgBO,IAAhB,CAArC;AACA,cAAME,WAAW,GAChB,CAAED,WAAF,IACAF,qBAAqB,CAAEN,YAAF,EAAgBO,IAAhB,CAFtB;AAGA,cAAMG,IAAI,GAAGZ,SAAS,CAAEE,YAAF,CAAT,CAA2B,GAAGO,IAA9B,CAAb;AAEA,YAAII,MAAJ;;AACA,YAAKH,WAAL,EAAmB;AAClBG,UAAAA,MAAM,GAAGvB,MAAM,CAACwB,SAAhB;AACA,SAFD,MAEO,IAAKH,WAAL,EAAmB;AACzB,cAAKC,IAAL,EAAY;AACXC,YAAAA,MAAM,GAAGvB,MAAM,CAACyB,OAAhB;AACA,WAFD,MAEO;AACNF,YAAAA,MAAM,GAAGvB,MAAM,CAAC0B,KAAhB;AACA;AACD,SANM,MAMA;AACNH,UAAAA,MAAM,GAAGvB,MAAM,CAAC2B,IAAhB;AACA;;AAED,eAAO;AACNL,UAAAA,IADM;AAENC,UAAAA,MAFM;AAGNH,UAAAA,WAHM;AAINC,UAAAA;AAJM,SAAP;AAMA;AA9B6C,KAAhD;AAgCA;;AACD,SAAOV,SAAP;AACA,CAxC8B,CAA/B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport memoize from './memoize';\nimport { Status } from './constants';\n\nexport const META_SELECTORS = [\n\t'getIsResolving',\n\t'hasStartedResolution',\n\t'hasFinishedResolution',\n\t'isResolving',\n\t'getCachedResolvers',\n];\n\ninterface QuerySelectResponse< Data > {\n\t/** the requested selector return value */\n\tdata: Data;\n\n\t/** is the record still being resolved? Via the `getIsResolving` meta-selector */\n\tisResolving: boolean;\n\n\t/** was the resolution started? Via the `hasStartedResolution` meta-selector */\n\thasStarted: boolean;\n\n\t/** has the resolution finished? Via the `hasFinishedResolution` meta-selector. */\n\thasResolved: boolean;\n}\n\n/**\n * Like useSelect, but the selectors return objects containing\n * both the original data AND the resolution info.\n *\n * @since 6.1.0 Introduced in WordPress core.\n * @private\n *\n * @param {Function} mapQuerySelect see useSelect\n * @param {Array} deps see useSelect\n *\n * @example\n * ```js\n * import { useQuerySelect } from '@wordpress/data';\n * import { store as coreDataStore } from '@wordpress/core-data';\n *\n * function PageTitleDisplay( { id } ) {\n * const { data: page, isResolving } = useQuerySelect( ( query ) => {\n * return query( coreDataStore ).getEntityRecord( 'postType', 'page', id )\n * }, [ id ] );\n *\n * if ( isResolving ) {\n * return 'Loading...';\n * }\n *\n * return page.title;\n * }\n *\n * // Rendered in the application:\n * // <PageTitleDisplay id={ 10 } />\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 the `mapSelect` callback on `useQuerySelect`.\n *\n * If the id prop changes then any page in the state for that id is\n * retrieved. If the id prop doesn't change and other props are passed in\n * that do change, the title will not change because the dependency is just\n * the id.\n * @see useSelect\n *\n * @return {QuerySelectResponse} Queried data.\n */\nexport default function useQuerySelect( mapQuerySelect, deps ) {\n\treturn useSelect( ( select, registry ) => {\n\t\tconst resolve = ( store ) => enrichSelectors( select( store ) );\n\t\treturn mapQuerySelect( resolve, registry );\n\t}, deps );\n}\n\ninterface EnrichedSelectors {\n\t< Selectors extends Record< string, ( ...args: any[] ) => any > >(\n\t\tselectors: Selectors\n\t): {\n\t\t[ Selector in keyof Selectors ]: (\n\t\t\t...args: Parameters< Selectors[ Selector ] >\n\t\t) => QuerySelectResponse< ReturnType< Selectors[ Selector ] > >;\n\t};\n}\n\n/**\n * Transform simple selectors into ones that return an object with the\n * original return value AND the resolution info.\n *\n * @param {Object} selectors Selectors to enrich\n * @return {EnrichedSelectors} Enriched selectors\n */\nconst enrichSelectors = memoize( ( ( selectors ) => {\n\tconst resolvers = {};\n\tfor ( const selectorName in selectors ) {\n\t\tif ( META_SELECTORS.includes( selectorName ) ) {\n\t\t\tcontinue;\n\t\t}\n\t\tObject.defineProperty( resolvers, selectorName, {\n\t\t\tget:\n\t\t\t\t() =>\n\t\t\t\t( ...args: unknown[] ) => {\n\t\t\t\t\tconst { getIsResolving, hasFinishedResolution } = selectors;\n\t\t\t\t\tconst isResolving = !! getIsResolving( selectorName, args );\n\t\t\t\t\tconst hasResolved =\n\t\t\t\t\t\t! isResolving &&\n\t\t\t\t\t\thasFinishedResolution( selectorName, args );\n\t\t\t\t\tconst data = selectors[ selectorName ]( ...args );\n\n\t\t\t\t\tlet status;\n\t\t\t\t\tif ( isResolving ) {\n\t\t\t\t\t\tstatus = Status.Resolving;\n\t\t\t\t\t} else if ( hasResolved ) {\n\t\t\t\t\t\tif ( data ) {\n\t\t\t\t\t\t\tstatus = Status.Success;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tstatus = Status.Error;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstatus = Status.Idle;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata,\n\t\t\t\t\t\tstatus,\n\t\t\t\t\t\tisResolving,\n\t\t\t\t\t\thasResolved,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t} );\n\t}\n\treturn resolvers;\n} ) as EnrichedSelectors );\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';
|
|
@@ -8,6 +13,8 @@ import useQuerySelect from './use-query-select';
|
|
|
8
13
|
/**
|
|
9
14
|
* Resolves resource permissions.
|
|
10
15
|
*
|
|
16
|
+
* @since 6.1.0 Introduced in WordPress core.
|
|
17
|
+
*
|
|
11
18
|
* @param resource The resource in question, e.g. media.
|
|
12
19
|
* @param id ID of a specific resource entry, if needed, e.g. 10.
|
|
13
20
|
*
|
|
@@ -34,6 +41,36 @@ import useQuerySelect from './use-query-select';
|
|
|
34
41
|
* // <PagesList />
|
|
35
42
|
* ```
|
|
36
43
|
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* import { useResourcePermissions } from '@wordpress/core-data';
|
|
47
|
+
*
|
|
48
|
+
* function Page({ pageId }) {
|
|
49
|
+
* const {
|
|
50
|
+
* canCreate,
|
|
51
|
+
* canUpdate,
|
|
52
|
+
* canDelete,
|
|
53
|
+
* isResolving
|
|
54
|
+
* } = useResourcePermissions( 'pages', pageId );
|
|
55
|
+
*
|
|
56
|
+
* if ( isResolving ) {
|
|
57
|
+
* return 'Loading ...';
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* return (
|
|
61
|
+
* <div>
|
|
62
|
+
* {canCreate ? (<button>+ Create a new page</button>) : false}
|
|
63
|
+
* {canUpdate ? (<button>Edit page</button>) : false}
|
|
64
|
+
* {canDelete ? (<button>Delete page</button>) : false}
|
|
65
|
+
* // ...
|
|
66
|
+
* </div>
|
|
67
|
+
* );
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* // Rendered in the application:
|
|
71
|
+
* // <Page pageId={ 15 } />
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
37
74
|
* In the above example, when `PagesList` is rendered into an
|
|
38
75
|
* application, the appropriate permissions and the resolution details will be retrieved from
|
|
39
76
|
* the store state using `canUser()`, or resolved if missing.
|
|
@@ -41,7 +78,7 @@ import useQuerySelect from './use-query-select';
|
|
|
41
78
|
* @return Entity records data.
|
|
42
79
|
* @template IdType
|
|
43
80
|
*/
|
|
44
|
-
export default function
|
|
81
|
+
export default function useResourcePermissions(resource, id) {
|
|
45
82
|
return useQuerySelect(resolve => {
|
|
46
83
|
const {
|
|
47
84
|
canUser
|
|
@@ -49,19 +86,33 @@ export default function __experimentalUseResourcePermissions(resource, id) {
|
|
|
49
86
|
const create = canUser('create', resource);
|
|
50
87
|
|
|
51
88
|
if (!id) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
89
|
+
const read = canUser('read', resource);
|
|
90
|
+
const isResolving = create.isResolving || read.isResolving;
|
|
91
|
+
const hasResolved = create.hasResolved && read.hasResolved;
|
|
92
|
+
let status = Status.Idle;
|
|
93
|
+
|
|
94
|
+
if (isResolving) {
|
|
95
|
+
status = Status.Resolving;
|
|
96
|
+
} else if (hasResolved) {
|
|
97
|
+
status = Status.Success;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
status,
|
|
102
|
+
isResolving,
|
|
103
|
+
hasResolved,
|
|
104
|
+
canCreate: create.hasResolved && create.data,
|
|
105
|
+
canRead: read.hasResolved && read.data
|
|
106
|
+
};
|
|
57
107
|
}
|
|
58
108
|
|
|
109
|
+
const read = canUser('read', resource, id);
|
|
59
110
|
const update = canUser('update', resource, id);
|
|
60
111
|
|
|
61
112
|
const _delete = canUser('delete', resource, id);
|
|
62
113
|
|
|
63
|
-
const isResolving = create.isResolving || update.isResolving || _delete.isResolving;
|
|
64
|
-
const hasResolved = create.hasResolved && update.hasResolved && _delete.hasResolved;
|
|
114
|
+
const isResolving = read.isResolving || create.isResolving || update.isResolving || _delete.isResolving;
|
|
115
|
+
const hasResolved = read.hasResolved && create.hasResolved && update.hasResolved && _delete.hasResolved;
|
|
65
116
|
let status = Status.Idle;
|
|
66
117
|
|
|
67
118
|
if (isResolving) {
|
|
@@ -70,13 +121,22 @@ export default function __experimentalUseResourcePermissions(resource, id) {
|
|
|
70
121
|
status = Status.Success;
|
|
71
122
|
}
|
|
72
123
|
|
|
73
|
-
return
|
|
124
|
+
return {
|
|
74
125
|
status,
|
|
75
126
|
isResolving,
|
|
127
|
+
hasResolved,
|
|
128
|
+
canRead: hasResolved && read.data,
|
|
76
129
|
canCreate: hasResolved && create.data,
|
|
77
130
|
canUpdate: hasResolved && update.data,
|
|
78
131
|
canDelete: hasResolved && _delete.data
|
|
79
|
-
}
|
|
132
|
+
};
|
|
80
133
|
}, [resource, id]);
|
|
81
134
|
}
|
|
135
|
+
export function __experimentalUseResourcePermissions(resource, id) {
|
|
136
|
+
deprecated(`wp.data.__experimentalUseResourcePermissions`, {
|
|
137
|
+
alternative: 'wp.data.useResourcePermissions',
|
|
138
|
+
since: '6.1'
|
|
139
|
+
});
|
|
140
|
+
return useResourcePermissions(resource, id);
|
|
141
|
+
}
|
|
82
142
|
//# 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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,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 * @since 6.1.0 Introduced in WordPress core.\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
|
@@ -81,17 +81,12 @@ const storeConfig = () => ({
|
|
|
81
81
|
* Store definition for the code data namespace.
|
|
82
82
|
*
|
|
83
83
|
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
|
|
84
|
-
*
|
|
85
|
-
* @type {Object}
|
|
86
84
|
*/
|
|
87
85
|
|
|
88
86
|
|
|
89
87
|
export const store = createReduxStore(STORE_NAME, storeConfig());
|
|
90
88
|
register(store);
|
|
91
89
|
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
90
|
export * from './entity-provider';
|
|
96
91
|
export * from './entity-types';
|
|
97
92
|
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;AAAQC,IAAAA;AAAR,MAAiBF,MAAvB;;AACAD,EAAAA,MAAM,CAAEJ,aAAa,CAAEM,IAAF,EAAQC,IAAR,CAAf,CAAN,GAAwC,CAAEC,KAAF,EAASC,GAAT,EAAcC,KAAd,KACvCf,SAAS,CAACgB,eAAV,CAA2BH,KAA3B,EAAkCF,IAAlC,EAAwCC,IAAxC,EAA8CE,GAA9C,EAAmDC,KAAnD,CADD;;AAEAN,EAAAA,MAAM,CAAEJ,aAAa,CAAEM,IAAF,EAAQC,IAAR,EAAc,KAAd,EAAqB,IAArB,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;;AACAD,EAAAA,MAAM,CAAEJ,aAAa,CAAEM,IAAF,EAAQC,IAAR,EAAc,MAAd,CAAf,CAAN,GAAkDE,GAAF,IAC/Cb,OAAO,CAACuB,gBAAR,CAA0Bb,IAA1B,EAAgCC,IAAhC,EAAsCE,GAAtC,CADD;;AAEAL,EAAAA,MAAM,CAAEJ,aAAa,CAAEM,IAAF,EAAQC,IAAR,EAAc,QAAd,CAAf,CAAN,GAAkD,CAAEE,GAAF,EAAOC,KAAP,KACjDd,OAAO,CAACwB,kBAAR,CAA4Bd,IAA5B,EAAkCC,IAAlC,EAAwCE,GAAxC,EAA6CC,KAA7C,CADD;;AAEA,SAAON,MAAP;AACA,CAPqB,EAOnB,EAPmB,CAAtB;;AASA,MAAMiB,WAAW,GAAG,OAAQ;AAC3B3B,EAAAA,OAD2B;AAE3BE,EAAAA,OAAO,EAAE,EAAE,GAAGA,OAAL;AAAc,OAAGsB,aAAjB;AAAgC,OAAGpB,kBAAkB;AAArD,GAFkB;AAG3BH,EAAAA,SAAS,EAAE,EAAE,GAAGA,SAAL;AAAgB,OAAGO;AAAnB,GAHgB;AAI3BL,EAAAA,SAAS,EAAE,EAAE,GAAGA,SAAL;AAAgB,OAAGgB;AAAnB;AAJgB,CAAR,CAApB;AAOA;AACA;AACA;AACA;AACA;;;AACA,OAAO,MAAMS,KAAK,GAAG9B,gBAAgB,CAAES,UAAF,EAAcoB,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 */\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
|
*/
|
|
@@ -65,10 +65,7 @@ export const getEntityRecord = function (kind, name) {
|
|
|
65
65
|
dispatch
|
|
66
66
|
} = _ref3;
|
|
67
67
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
68
|
-
const entityConfig = find(
|
|
69
|
-
kind,
|
|
70
|
-
name
|
|
71
|
-
});
|
|
68
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
72
69
|
|
|
73
70
|
if (!entityConfig || entityConfig !== null && entityConfig !== void 0 && entityConfig.__experimentalNoFetch) {
|
|
74
71
|
return;
|
|
@@ -84,7 +81,7 @@ export const getEntityRecord = function (kind, name) {
|
|
|
84
81
|
// records are stored by ID reference. Thus, fields must always include
|
|
85
82
|
// the ID.
|
|
86
83
|
query = { ...query,
|
|
87
|
-
_fields:
|
|
84
|
+
_fields: [...new Set([...(getNormalizedCommaSeparable(query._fields) || []), entityConfig.key || DEFAULT_ENTITY_KEY])].join()
|
|
88
85
|
};
|
|
89
86
|
} // Disable reason: While true that an early return could leave `path`
|
|
90
87
|
// unused, it's important that path is derived using the query prior to
|
|
@@ -147,10 +144,7 @@ export const getEntityRecords = function (kind, name) {
|
|
|
147
144
|
dispatch
|
|
148
145
|
} = _ref4;
|
|
149
146
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
150
|
-
const entityConfig = find(
|
|
151
|
-
kind,
|
|
152
|
-
name
|
|
153
|
-
});
|
|
147
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
154
148
|
|
|
155
149
|
if (!entityConfig || entityConfig !== null && entityConfig !== void 0 && entityConfig.__experimentalNoFetch) {
|
|
156
150
|
return;
|
|
@@ -168,7 +162,7 @@ export const getEntityRecords = function (kind, name) {
|
|
|
168
162
|
// records are stored by ID reference. Thus, fields must always include
|
|
169
163
|
// the ID.
|
|
170
164
|
query = { ...query,
|
|
171
|
-
_fields:
|
|
165
|
+
_fields: [...new Set([...(getNormalizedCommaSeparable(query._fields) || []), entityConfig.key || DEFAULT_ENTITY_KEY])].join()
|
|
172
166
|
};
|
|
173
167
|
}
|
|
174
168
|
|
|
@@ -267,36 +261,47 @@ export const getEmbedPreview = url => async _ref6 => {
|
|
|
267
261
|
* Checks whether the current user can perform the given action on the given
|
|
268
262
|
* REST resource.
|
|
269
263
|
*
|
|
270
|
-
* @param {string}
|
|
271
|
-
*
|
|
272
|
-
* @param {string} resource
|
|
273
|
-
* @param {?string} id
|
|
264
|
+
* @param {string} requestedAction Action to check. One of: 'create', 'read', 'update',
|
|
265
|
+
* 'delete'.
|
|
266
|
+
* @param {string} resource REST resource to check, e.g. 'media' or 'posts'.
|
|
267
|
+
* @param {?string} id ID of the rest resource to check.
|
|
274
268
|
*/
|
|
275
269
|
|
|
276
|
-
export const canUser = (
|
|
270
|
+
export const canUser = (requestedAction, resource, id) => async _ref7 => {
|
|
277
271
|
var _response$headers;
|
|
278
272
|
|
|
279
273
|
let {
|
|
280
|
-
dispatch
|
|
274
|
+
dispatch,
|
|
275
|
+
registry
|
|
281
276
|
} = _ref7;
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
277
|
+
const {
|
|
278
|
+
hasStartedResolution
|
|
279
|
+
} = registry.select(STORE_NAME);
|
|
280
|
+
const resourcePath = id ? `${resource}/${id}` : resource;
|
|
281
|
+
const retrievedActions = ['create', 'read', 'update', 'delete'];
|
|
282
|
+
|
|
283
|
+
if (!retrievedActions.includes(requestedAction)) {
|
|
284
|
+
throw new Error(`'${requestedAction}' is not a valid action.`);
|
|
285
|
+
} // Prevent resolving the same resource twice.
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
for (const relatedAction of retrievedActions) {
|
|
289
|
+
if (relatedAction === requestedAction) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
289
292
|
|
|
290
|
-
|
|
291
|
-
|
|
293
|
+
const isAlreadyResolving = hasStartedResolution('canUser', [relatedAction, resource, id]);
|
|
294
|
+
|
|
295
|
+
if (isAlreadyResolving) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
292
298
|
}
|
|
293
299
|
|
|
294
|
-
const path = id ? `/wp/v2/${resource}/${id}` : `/wp/v2/${resource}`;
|
|
295
300
|
let response;
|
|
296
301
|
|
|
297
302
|
try {
|
|
298
303
|
response = await apiFetch({
|
|
299
|
-
path
|
|
304
|
+
path: `/wp/v2/${resourcePath}`,
|
|
300
305
|
method: 'OPTIONS',
|
|
301
306
|
parse: false
|
|
302
307
|
});
|
|
@@ -310,9 +315,22 @@ export const canUser = (action, resource, id) => async _ref7 => {
|
|
|
310
315
|
|
|
311
316
|
|
|
312
317
|
const allowHeader = (_response$headers = response.headers) === null || _response$headers === void 0 ? void 0 : _response$headers.get('allow');
|
|
313
|
-
const
|
|
314
|
-
const
|
|
315
|
-
|
|
318
|
+
const allowedMethods = (allowHeader === null || allowHeader === void 0 ? void 0 : allowHeader.allow) || allowHeader || '';
|
|
319
|
+
const permissions = {};
|
|
320
|
+
const methods = {
|
|
321
|
+
create: 'POST',
|
|
322
|
+
read: 'GET',
|
|
323
|
+
update: 'PUT',
|
|
324
|
+
delete: 'DELETE'
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
for (const [actionName, methodName] of Object.entries(methods)) {
|
|
328
|
+
permissions[actionName] = allowedMethods.includes(methodName);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
for (const action of retrievedActions) {
|
|
332
|
+
dispatch.receiveUserPermission(`${action}/${resourcePath}`, permissions[action]);
|
|
333
|
+
}
|
|
316
334
|
};
|
|
317
335
|
/**
|
|
318
336
|
* Checks whether the current user can perform the given action on the given
|
|
@@ -328,10 +346,7 @@ export const canUserEditEntityRecord = (kind, name, recordId) => async _ref8 =>
|
|
|
328
346
|
dispatch
|
|
329
347
|
} = _ref8;
|
|
330
348
|
const configs = await dispatch(getOrLoadEntitiesConfig(kind));
|
|
331
|
-
const entityConfig = find(
|
|
332
|
-
kind,
|
|
333
|
-
name
|
|
334
|
-
});
|
|
349
|
+
const entityConfig = configs.find(config => config.name === name && config.kind === kind);
|
|
335
350
|
|
|
336
351
|
if (!entityConfig) {
|
|
337
352
|
return;
|
|
@@ -426,6 +441,8 @@ __experimentalGetTemplateForLink.shouldInvalidate = action => {
|
|
|
426
441
|
};
|
|
427
442
|
|
|
428
443
|
export const __experimentalGetCurrentGlobalStylesId = () => async _ref13 => {
|
|
444
|
+
var _activeThemes$, _activeThemes$$_links, _activeThemes$$_links2, _activeThemes$$_links3;
|
|
445
|
+
|
|
429
446
|
let {
|
|
430
447
|
dispatch,
|
|
431
448
|
resolveSelect
|
|
@@ -433,7 +450,7 @@ export const __experimentalGetCurrentGlobalStylesId = () => async _ref13 => {
|
|
|
433
450
|
const activeThemes = await resolveSelect.getEntityRecords('root', 'theme', {
|
|
434
451
|
status: 'active'
|
|
435
452
|
});
|
|
436
|
-
const globalStylesURL =
|
|
453
|
+
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;
|
|
437
454
|
|
|
438
455
|
if (globalStylesURL) {
|
|
439
456
|
const globalStylesObject = await apiFetch({
|
|
@@ -474,16 +491,19 @@ export const getBlockPatterns = () => async _ref16 => {
|
|
|
474
491
|
const restPatterns = await apiFetch({
|
|
475
492
|
path: '/wp/v2/block-patterns/patterns'
|
|
476
493
|
});
|
|
477
|
-
const patterns = map(
|
|
494
|
+
const patterns = restPatterns === null || restPatterns === void 0 ? void 0 : restPatterns.map(pattern => Object.fromEntries(Object.entries(pattern).map(_ref17 => {
|
|
495
|
+
let [key, value] = _ref17;
|
|
496
|
+
return [camelCase(key), value];
|
|
497
|
+
})));
|
|
478
498
|
dispatch({
|
|
479
499
|
type: 'RECEIVE_BLOCK_PATTERNS',
|
|
480
500
|
patterns
|
|
481
501
|
});
|
|
482
502
|
};
|
|
483
|
-
export const getBlockPatternCategories = () => async
|
|
503
|
+
export const getBlockPatternCategories = () => async _ref18 => {
|
|
484
504
|
let {
|
|
485
505
|
dispatch
|
|
486
|
-
} =
|
|
506
|
+
} = _ref18;
|
|
487
507
|
const categories = await apiFetch({
|
|
488
508
|
path: '/wp/v2/block-patterns/categories'
|
|
489
509
|
});
|