Skip to content

Commit

Permalink
Make 'canUserEditEntityRecord' forwarded selector/resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jul 10, 2024
1 parent b964ddd commit a74fd72
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 73 deletions.
11 changes: 1 addition & 10 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,7 @@ export const canUser =
export const canUserEditEntityRecord =
( kind, name, recordId ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);
if ( ! entityConfig ) {
return;
}

const resource = entityConfig.__unstable_rest_base;
await dispatch( canUser( 'update', resource, recordId ) );
await dispatch( canUser( 'update', { kind, name, id: recordId } ) );
};

/**
Expand Down
8 changes: 1 addition & 7 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,13 +1180,7 @@ export function canUserEditEntityRecord(
name: string,
recordId: EntityRecordKey
): boolean | undefined {
const entityConfig = getEntityConfig( state, kind, name );
if ( ! entityConfig ) {
return false;
}
const resource = entityConfig.__unstable_rest_base;

return canUser( state, 'update', resource, recordId );
return canUser( state, 'update', { kind, name, id: recordId } );
}

/**
Expand Down
56 changes: 0 additions & 56 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getEmbedPreview,
isPreviewEmbedFallback,
canUser,
canUserEditEntityRecord,
getAutosave,
getAutosaves,
getCurrentUser,
Expand Down Expand Up @@ -722,61 +721,6 @@ describe( 'canUser', () => {
} );
} );

describe( 'canUserEditEntityRecord', () => {
it( 'returns false by default', () => {
const state = deepFreeze( {
userPermissions: {},
entities: { records: {} },
} );
expect( canUserEditEntityRecord( state, 'postType', 'post' ) ).toBe(
false
);
} );

it( 'returns whether the user can edit', () => {
const state = deepFreeze( {
userPermissions: {
'create/posts': false,
'update/posts/1': true,
},
entities: {
config: [
{
kind: 'postType',
name: 'post',
__unstable_rest_base: 'posts',
},
],
records: {
root: {
postType: {
queriedData: {
items: {
default: {
post: {
slug: 'post',
__unstable: 'posts',
},
},
},
itemIsComplete: {
default: {
post: true,
},
},
queries: {},
},
},
},
},
},
} );
expect(
canUserEditEntityRecord( state, 'postType', 'post', '1' )
).toBe( true );
} );
} );

describe( 'getAutosave', () => {
const testAutosave = {
author: 1,
Expand Down

0 comments on commit a74fd72

Please sign in to comment.