Skip to content

Commit

Permalink
Update hook to use correct ids for permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
louwie17 committed Dec 12, 2024
1 parent 5762893 commit 6972c79
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions packages/editor/src/components/post-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,47 @@ import { usePostActions } from './actions';
const { Menu, kebabCase } = unlock( componentsPrivateApis );

function useEditedEntityRecordsWithPermissions( postType, postIds ) {
const { items, permissions } = useSelect(
const entityConfig = useSelect(
( select ) =>
select( coreStore ).getEntityConfig( 'postType', postType ),
[ postType ]
);
const { items } = useSelect(
( select ) => {
const { getEditedEntityRecords, getEntityRecordsPermissions } =
unlock( select( coreStore ) );
const { getEditedEntityRecords } = unlock( select( coreStore ) );
return {
items: getEditedEntityRecords( 'postType', postType, postIds ),
permissions: getEntityRecordsPermissions(
'postType',
postType,
postIds
),
};
},
[ postIds, postType ]
);

const ids = useMemo(
() =>
items?.map(
// @ts-ignore
( record ) => record[ entityConfig?.key ?? 'id' ]
) ?? [],
[ items, entityConfig?.key ]
);

const permissions = useSelect(
( select ) => {
const { getEntityRecordsPermissions } = unlock(
select( coreStore )
);
return getEntityRecordsPermissions( 'postType', postType, ids );
},
[ ids, postType ]
);

return useMemo( () => {
return items.map( ( item, index ) => ( {
...item,
permissions: permissions[ index ],
} ) );
return (
items?.map( ( item, index ) => ( {
...item,
permissions: permissions[ index ],
} ) ) ?? []
);
}, [ items, permissions ] );
}

Expand Down

0 comments on commit 6972c79

Please sign in to comment.