Merge pull request #13902 from strapi/fix/ml-assetdialog-canread

AssetDialog: Properly take canRead permissions into account
This commit is contained in:
Gustav Hansen 2022-07-29 16:40:49 +02:00 committed by GitHub
commit b511597ea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View File

@ -26,12 +26,15 @@ const BackIcon = styled(Icon)`
} }
`; `;
export const DialogHeader = ({ currentFolder, onChangeFolder }) => { export const DialogHeader = ({ currentFolder, onChangeFolder, canRead }) => {
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const { data, isLoading } = useFolderStructure(); const { data, isLoading } = useFolderStructure({
enabled: canRead,
});
const folderMetadatas = !isLoading && findRecursiveFolderMetadatas(data[0], currentFolder); const folderMetadatas =
!isLoading && Array.isArray(data) && findRecursiveFolderMetadatas(data[0], currentFolder);
const folderLabel = const folderLabel =
folderMetadatas?.currentFolderLabel && folderMetadatas?.currentFolderLabel &&
(folderMetadatas.currentFolderLabel.length > 60 (folderMetadatas.currentFolderLabel.length > 60
@ -82,6 +85,7 @@ DialogHeader.defaultProps = {
}; };
DialogHeader.propTypes = { DialogHeader.propTypes = {
canRead: PropTypes.bool.isRequired,
currentFolder: PropTypes.number, currentFolder: PropTypes.number,
onChangeFolder: PropTypes.func, onChangeFolder: PropTypes.func,
}; };

View File

@ -107,7 +107,7 @@ export const AssetDialog = ({
if (isLoading) { if (isLoading) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy>
<DialogHeader /> <DialogHeader canRead={canRead} />
<LoadingBody justifyContent="center" paddingTop={4} paddingBottom={4}> <LoadingBody justifyContent="center" paddingTop={4} paddingBottom={4}>
<Loader> <Loader>
{formatMessage({ {formatMessage({
@ -124,7 +124,7 @@ export const AssetDialog = ({
if (hasError) { if (hasError) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title"> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title">
<DialogHeader /> <DialogHeader canRead={canRead} />
<AnErrorOccurred /> <AnErrorOccurred />
<DialogFooter onClose={onClose} /> <DialogFooter onClose={onClose} />
</ModalLayout> </ModalLayout>
@ -134,7 +134,7 @@ export const AssetDialog = ({
if (!canRead) { if (!canRead) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title"> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title">
<DialogHeader /> <DialogHeader canRead={canRead} />
<NoPermissions /> <NoPermissions />
<DialogFooter onClose={onClose} /> <DialogFooter onClose={onClose} />
</ModalLayout> </ModalLayout>
@ -179,7 +179,11 @@ export const AssetDialog = ({
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy={isLoading}> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy={isLoading}>
<DialogHeader currentFolder={queryObject?.folder} onChangeFolder={handleFolderChange} /> <DialogHeader
currentFolder={queryObject?.folder}
onChangeFolder={handleFolderChange}
canRead={canRead}
/>
<TabGroup <TabGroup
label={formatMessage({ label={formatMessage({

View File

@ -10,6 +10,7 @@ jest.mock('../../../hooks/useFolderStructure');
const setup = props => { const setup = props => {
const withDefaults = { const withDefaults = {
canRead: true,
currentFolder: null, currentFolder: null,
onChangeFolder: jest.fn(), onChangeFolder: jest.fn(),
...props, ...props,
@ -80,4 +81,16 @@ describe('Upload || components || DialogHeader', () => {
expect(queryByText('Cats')).not.toBeInTheDocument(); expect(queryByText('Cats')).not.toBeInTheDocument();
expect(screen.queryByLabelText('Go back')).not.toBeInTheDocument(); expect(screen.queryByLabelText('Go back')).not.toBeInTheDocument();
}); });
it('should not attempt to fetch the folder structure, if the user does not have permissions', () => {
const spy = jest.fn().mockReturnValueOnce({
isLoading: false,
error: null,
});
useFolderStructure.mockImplementation(spy);
setup({ canRead: false });
expect(spy).toHaveBeenCalledWith({ enabled: false });
});
}); });