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 { 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 =
folderMetadatas?.currentFolderLabel &&
(folderMetadatas.currentFolderLabel.length > 60
@ -82,6 +85,7 @@ DialogHeader.defaultProps = {
};
DialogHeader.propTypes = {
canRead: PropTypes.bool.isRequired,
currentFolder: PropTypes.number,
onChangeFolder: PropTypes.func,
};

View File

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

View File

@ -10,6 +10,7 @@ jest.mock('../../../hooks/useFolderStructure');
const setup = props => {
const withDefaults = {
canRead: true,
currentFolder: null,
onChangeFolder: jest.fn(),
...props,
@ -80,4 +81,16 @@ describe('Upload || components || DialogHeader', () => {
expect(queryByText('Cats')).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 });
});
});