diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 7d0740dea58a72..571cbf7f494536 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -368,63 +368,59 @@ export const getEntityRecords = meta ); - // When requesting all fields, the list of results can be used to resolve - // the `getEntityRecord` and `canUser` selectors in addition to `getEntityRecords`. - // See https://github.com/WordPress/gutenberg/pull/26575 - // See https://github.com/WordPress/gutenberg/pull/64504 - // See https://github.com/WordPress/gutenberg/pull/70738 - if ( ! query.context ) { - const targetHints = records - .filter( - ( record ) => - !! record?.[ key ] && - !! record?._links?.self?.[ 0 ]?.targetHints - ?.allow - ) - .map( ( record ) => ( { - id: record[ key ], - permissions: getUserPermissionsFromAllowHeader( - record._links.self[ 0 ].targetHints.allow - ), - } ) ); - - const canUserResolutionsArgs = []; - const receiveUserPermissionArgs = {}; - for ( const targetHint of targetHints ) { - for ( const action of ALLOWED_RESOURCE_ACTIONS ) { - canUserResolutionsArgs.push( [ - action, - { kind, name, id: targetHint.id }, - ] ); - - receiveUserPermissionArgs[ - getUserPermissionCacheKey( action, { - kind, - name, - id: targetHint.id, - } ) - ] = targetHint.permissions[ action ]; - } - } + const targetHints = records + .filter( + ( record ) => + !! record?.[ key ] && + !! record?._links?.self?.[ 0 ]?.targetHints?.allow + ) + .map( ( record ) => ( { + id: record[ key ], + permissions: getUserPermissionsFromAllowHeader( + record._links.self[ 0 ].targetHints.allow + ), + } ) ); - if ( targetHints.length > 0 ) { - dispatch.receiveUserPermissions( - receiveUserPermissionArgs - ); - dispatch.finishResolutions( - 'canUser', - canUserResolutionsArgs - ); - } + const canUserResolutionsArgs = []; + const receiveUserPermissionArgs = {}; + for ( const targetHint of targetHints ) { + for ( const action of ALLOWED_RESOURCE_ACTIONS ) { + canUserResolutionsArgs.push( [ + action, + { kind, name, id: targetHint.id }, + ] ); - if ( ! query?._fields ) { - dispatch.finishResolutions( - 'getEntityRecord', - getResolutionsArgs( records ) - ); + receiveUserPermissionArgs[ + getUserPermissionCacheKey( action, { + kind, + name, + id: targetHint.id, + } ) + ] = targetHint.permissions[ action ]; } } + if ( targetHints.length > 0 ) { + dispatch.receiveUserPermissions( + receiveUserPermissionArgs + ); + dispatch.finishResolutions( + 'canUser', + canUserResolutionsArgs + ); + } + + // When requesting all fields, the list of results can be used to resolve + // the `getEntityRecord` selector in addition to `getEntityRecords`. + // See https://github.com/WordPress/gutenberg/pull/26575 + // Todo https://github.com/WordPress/gutenberg/issues/26629 + if ( ! query?._fields && ! query.context ) { + dispatch.finishResolutions( + 'getEntityRecord', + getResolutionsArgs( records ) + ); + } + dispatch.__unstableReleaseStoreLock( lock ); } ); } catch ( e ) {