Skip to content

Commit

Permalink
Make useResourcePermissions return a tuple and make hasResolved the f…
Browse files Browse the repository at this point in the history
…irst item of the tuple forces the users to think about that variable and it's not easy to accidentally miss it.
  • Loading branch information
adamziel committed Feb 16, 2022
1 parent caa48a7 commit 12c7433
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
64 changes: 36 additions & 28 deletions packages/core-data/src/hooks/test/use-resource-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,28 @@ describe( 'useResourcePermissions', () => {
<TestComponent />
</RegistryProvider>
);
expect( data ).toEqual( {
status: 'IDLE',
isResolving: false,
hasResolved: false,
canCreate: false,
} );
expect( data ).toEqual( [
false,
{
status: 'IDLE',
isResolving: false,
canCreate: false,
},
] );

// Required to make sure no updates happen outside of act()
await act( async () => {
jest.advanceTimersByTime( 1 );
} );

expect( data ).toEqual( {
status: 'SUCCESS',
isResolving: false,
hasResolved: true,
canCreate: true,
} );
expect( data ).toEqual( [
true,
{
status: 'SUCCESS',
isResolving: false,
canCreate: true,
},
] );
} );

it( 'retrieves the relevant permissions for a resource with a key', async () => {
Expand All @@ -83,27 +87,31 @@ describe( 'useResourcePermissions', () => {
<TestComponent />
</RegistryProvider>
);
expect( data ).toEqual( {
status: 'IDLE',
isResolving: false,
hasResolved: false,
canCreate: false,
canUpdate: false,
canDelete: false,
} );
expect( data ).toEqual( [
false,
{
status: 'IDLE',
isResolving: false,
canCreate: false,
canUpdate: false,
canDelete: false,
},
] );

// Required to make sure no updates happen outside of act()
await act( async () => {
jest.advanceTimersByTime( 1 );
} );

expect( data ).toEqual( {
status: 'SUCCESS',
isResolving: false,
hasResolved: true,
canCreate: true,
canUpdate: false,
canDelete: false,
} );
expect( data ).toEqual( [
true,
{
status: 'SUCCESS',
isResolving: false,
canCreate: true,
canUpdate: false,
canDelete: false,
},
] );
} );
} );
30 changes: 17 additions & 13 deletions packages/core-data/src/hooks/use-resource-permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ export default function __experimentalUseResourcePermissions< IdType = void >(
const { canUser } = resolve( coreStore );
const create = canUser( 'create', resource );
if ( ! id ) {
return {
status: create.status,
isResolving: create.isResolving,
hasResolved: create.hasResolved,
canCreate: create.hasResolved && create.data,
};
return [
create.hasResolved,
{
status: create.status,
isResolving: create.isResolving,
canCreate: create.hasResolved && create.data,
},
];
}

const update = canUser( 'update', resource, id );
Expand All @@ -98,14 +100,16 @@ export default function __experimentalUseResourcePermissions< IdType = void >(
} else if ( hasResolved ) {
status = Status.Success;
}
return {
status,
isResolving,
return [
hasResolved,
canCreate: hasResolved && create.data,
canUpdate: hasResolved && update.data,
canDelete: hasResolved && _delete.data,
};
{
status,
isResolving,
canCreate: hasResolved && create.data,
canUpdate: hasResolved && update.data,
canDelete: hasResolved && _delete.data,
},
];
},
[ resource, id ]
);
Expand Down

0 comments on commit 12c7433

Please sign in to comment.