mirror of
https://github.com/strapi/strapi.git
synced 2025-09-23 07:22:51 +00:00
useModalQueryParams: Pass down query into useAssets and useFolders
This commit is contained in:
parent
84cac53266
commit
fc42a110be
@ -10,6 +10,7 @@ import styled from 'styled-components';
|
|||||||
import getTrad from '../../../utils/getTrad';
|
import getTrad from '../../../utils/getTrad';
|
||||||
import getAllowedFiles from '../../../utils/getAllowedFiles';
|
import getAllowedFiles from '../../../utils/getAllowedFiles';
|
||||||
import { AssetList } from '../../AssetList';
|
import { AssetList } from '../../AssetList';
|
||||||
|
import { FolderList } from '../../FolderList';
|
||||||
import { EmptyAssets } from '../../EmptyAssets';
|
import { EmptyAssets } from '../../EmptyAssets';
|
||||||
import { Filters } from './Filters';
|
import { Filters } from './Filters';
|
||||||
import PaginationFooter from './PaginationFooter';
|
import PaginationFooter from './PaginationFooter';
|
||||||
@ -39,6 +40,7 @@ export const BrowseStep = ({
|
|||||||
onChangePageSize,
|
onChangePageSize,
|
||||||
onChangeSearch,
|
onChangeSearch,
|
||||||
onChangeSort,
|
onChangeSort,
|
||||||
|
onChangeFolder,
|
||||||
onEditAsset,
|
onEditAsset,
|
||||||
onSelectAllAsset,
|
onSelectAllAsset,
|
||||||
onSelectAsset,
|
onSelectAsset,
|
||||||
@ -98,7 +100,19 @@ export const BrowseStep = ({
|
|||||||
</Box>
|
</Box>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{folders.length > 0 && <></>}
|
{folders.length > 0 && (
|
||||||
|
<FolderList
|
||||||
|
folders={folders}
|
||||||
|
size="S"
|
||||||
|
onClickFolder={onChangeFolder}
|
||||||
|
onEditFolder={null}
|
||||||
|
onSelectFolder={null}
|
||||||
|
title={formatMessage({
|
||||||
|
id: getTrad('list.folders.title'),
|
||||||
|
defaultMessage: 'Folders',
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{assets.length > 0 ? (
|
{assets.length > 0 ? (
|
||||||
<AssetList
|
<AssetList
|
||||||
@ -108,6 +122,10 @@ export const BrowseStep = ({
|
|||||||
onSelectAsset={onSelectAsset}
|
onSelectAsset={onSelectAsset}
|
||||||
selectedAssets={selectedAssets}
|
selectedAssets={selectedAssets}
|
||||||
onEditAsset={onEditAsset}
|
onEditAsset={onEditAsset}
|
||||||
|
title={formatMessage({
|
||||||
|
id: getTrad('list.assets.title'),
|
||||||
|
defaultMessage: 'Assets',
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Box paddingBottom={6}>
|
<Box paddingBottom={6}>
|
||||||
@ -154,6 +172,7 @@ BrowseStep.propTypes = {
|
|||||||
|
|
||||||
multiple: PropTypes.bool,
|
multiple: PropTypes.bool,
|
||||||
onChangeFilters: PropTypes.func.isRequired,
|
onChangeFilters: PropTypes.func.isRequired,
|
||||||
|
onChangeFolder: PropTypes.func.isRequired,
|
||||||
onChangePage: PropTypes.func.isRequired,
|
onChangePage: PropTypes.func.isRequired,
|
||||||
onChangePageSize: PropTypes.func.isRequired,
|
onChangePageSize: PropTypes.func.isRequired,
|
||||||
onChangeSort: PropTypes.func.isRequired,
|
onChangeSort: PropTypes.func.isRequired,
|
||||||
|
@ -12,6 +12,7 @@ import { Badge } from '@strapi/design-system/Badge';
|
|||||||
import { Loader } from '@strapi/design-system/Loader';
|
import { Loader } from '@strapi/design-system/Loader';
|
||||||
import { Stack } from '@strapi/design-system/Stack';
|
import { Stack } from '@strapi/design-system/Stack';
|
||||||
import { NoPermissions, AnErrorOccurred, useSelectionState } from '@strapi/helper-plugin';
|
import { NoPermissions, AnErrorOccurred, useSelectionState } from '@strapi/helper-plugin';
|
||||||
|
|
||||||
import getTrad from '../../utils/getTrad';
|
import getTrad from '../../utils/getTrad';
|
||||||
import { SelectedStep } from './SelectedStep';
|
import { SelectedStep } from './SelectedStep';
|
||||||
import { BrowseStep } from './BrowseStep';
|
import { BrowseStep } from './BrowseStep';
|
||||||
@ -48,7 +49,7 @@ export const AssetDialog = ({
|
|||||||
canDownload,
|
canDownload,
|
||||||
} = useMediaLibraryPermissions();
|
} = useMediaLibraryPermissions();
|
||||||
const [
|
const [
|
||||||
{ rawQuery, queryObject },
|
{ queryObject },
|
||||||
{
|
{
|
||||||
onChangeFilters,
|
onChangeFilters,
|
||||||
onChangePage,
|
onChangePage,
|
||||||
@ -62,12 +63,12 @@ export const AssetDialog = ({
|
|||||||
data: { pagination, results: assets } = {},
|
data: { pagination, results: assets } = {},
|
||||||
isLoading: isLoadingAssets,
|
isLoading: isLoadingAssets,
|
||||||
error: errorAssets,
|
error: errorAssets,
|
||||||
} = useAssets({ skipWhen: !canRead, rawQuery });
|
} = useAssets({ skipWhen: !canRead, query: queryObject });
|
||||||
const {
|
const {
|
||||||
data: { results: folders } = {},
|
data: { results: folders } = {},
|
||||||
isLoading: isLoadingFolders,
|
isLoading: isLoadingFolders,
|
||||||
error: errorFolders,
|
error: errorFolders,
|
||||||
} = useFolders();
|
} = useFolders({ query: queryObject });
|
||||||
|
|
||||||
const [selectedAssets, { selectOne, selectAll, selectOnly, setSelections }] = useSelectionState(
|
const [selectedAssets, { selectOne, selectAll, selectOnly, setSelections }] = useSelectionState(
|
||||||
['id'],
|
['id'],
|
||||||
@ -246,7 +247,7 @@ export const AssetDialog = ({
|
|||||||
<BrowseStep
|
<BrowseStep
|
||||||
allowedTypes={allowedTypes}
|
allowedTypes={allowedTypes}
|
||||||
assets={assets}
|
assets={assets}
|
||||||
folder={folders}
|
folders={folders}
|
||||||
onSelectAsset={handleSelectAsset}
|
onSelectAsset={handleSelectAsset}
|
||||||
selectedAssets={selectedAssets}
|
selectedAssets={selectedAssets}
|
||||||
multiple={multiple}
|
multiple={multiple}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import { stringify } from 'qs';
|
|
||||||
import { useHistory, useLocation } from 'react-router-dom';
|
|
||||||
import { Box } from '@strapi/design-system/Box';
|
import { Box } from '@strapi/design-system/Box';
|
||||||
import { KeyboardNavigable } from '@strapi/design-system/KeyboardNavigable';
|
import { KeyboardNavigable } from '@strapi/design-system/KeyboardNavigable';
|
||||||
import { Flex } from '@strapi/design-system/Flex';
|
import { Flex } from '@strapi/design-system/Flex';
|
||||||
@ -10,7 +8,6 @@ import { Grid, GridItem } from '@strapi/design-system/Grid';
|
|||||||
import { IconButton } from '@strapi/design-system/IconButton';
|
import { IconButton } from '@strapi/design-system/IconButton';
|
||||||
import { Typography } from '@strapi/design-system/Typography';
|
import { Typography } from '@strapi/design-system/Typography';
|
||||||
import { VisuallyHidden } from '@strapi/design-system/VisuallyHidden';
|
import { VisuallyHidden } from '@strapi/design-system/VisuallyHidden';
|
||||||
import { useQueryParams } from '@strapi/helper-plugin';
|
|
||||||
import Pencil from '@strapi/icons/Pencil';
|
import Pencil from '@strapi/icons/Pencil';
|
||||||
|
|
||||||
import { FolderCard, FolderCardBody, FolderCardCheckbox, FolderCardLink } from '../FolderCard';
|
import { FolderCard, FolderCardBody, FolderCardCheckbox, FolderCardLink } from '../FolderCard';
|
||||||
@ -31,12 +28,9 @@ export const FolderList = ({
|
|||||||
size,
|
size,
|
||||||
onSelectFolder,
|
onSelectFolder,
|
||||||
onEditFolder,
|
onEditFolder,
|
||||||
|
onClickFolder,
|
||||||
selectedFolders,
|
selectedFolders,
|
||||||
}) => {
|
}) => {
|
||||||
const history = useHistory();
|
|
||||||
const { pathname } = useLocation();
|
|
||||||
const [{ query }] = useQueryParams();
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<KeyboardNavigable tagName="article">
|
<KeyboardNavigable tagName="article">
|
||||||
{title && (
|
{title && (
|
||||||
@ -52,35 +46,46 @@ export const FolderList = ({
|
|||||||
const isSelected = !!selectedFolders.find(
|
const isSelected = !!selectedFolders.find(
|
||||||
currentFolder => currentFolder.id === folder.id
|
currentFolder => currentFolder.id === folder.id
|
||||||
);
|
);
|
||||||
const url = `${pathname}?${stringify(
|
|
||||||
{ ...query, folder: folder.id },
|
|
||||||
{ encode: false }
|
|
||||||
)}`;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<GridItem col={3} key={`folder-${folder.uid}`}>
|
<GridItem col={3} key={`folder-${folder.uid}`}>
|
||||||
<FolderCard
|
<FolderCard
|
||||||
ariaLabel={folder.name}
|
ariaLabel={folder.name}
|
||||||
id={`folder-${folder.uid}`}
|
id={`folder-${folder.uid}`}
|
||||||
onDoubleClick={() => history.push(url)}
|
onDoubleClick={event => {
|
||||||
|
event.preventDefault();
|
||||||
|
onClickFolder(folder);
|
||||||
|
}}
|
||||||
startAction={
|
startAction={
|
||||||
|
onSelectFolder && (
|
||||||
<FolderCardCheckbox
|
<FolderCardCheckbox
|
||||||
value={isSelected}
|
value={isSelected}
|
||||||
onChange={() => onSelectFolder({ ...folder, type: 'folder' })}
|
onChange={() => onSelectFolder({ ...folder, type: 'folder' })}
|
||||||
/>
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
cardActions={
|
||||||
|
onEditFolder && (
|
||||||
|
<IconButton icon={<Pencil />} onClick={() => onEditFolder(folder)} />
|
||||||
|
)
|
||||||
}
|
}
|
||||||
cardActions={<IconButton icon={<Pencil />} onClick={() => onEditFolder(folder)} />}
|
|
||||||
size={size}
|
size={size}
|
||||||
>
|
>
|
||||||
<FolderCardBody>
|
<FolderCardBody>
|
||||||
<FolderCardLink to={url}>
|
<FolderCardLink
|
||||||
|
to="/"
|
||||||
|
onClick={event => {
|
||||||
|
event.preventDefault();
|
||||||
|
onClickFolder(folder);
|
||||||
|
}}
|
||||||
|
>
|
||||||
<Flex as="h2" direction="column" alignItems="start">
|
<Flex as="h2" direction="column" alignItems="start">
|
||||||
<CardTitle>
|
<CardTitle>
|
||||||
{folder.name}
|
{folder.name}
|
||||||
<VisuallyHidden>:</VisuallyHidden>
|
<VisuallyHidden>:</VisuallyHidden>
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
|
|
||||||
<Typography as="span" textColor="neutral600" variant="pi">
|
<Typography as="span" textColor="neutral600" variant="pi" ellipsis>
|
||||||
{folder.children.count} folder, {folder.files.count} assets
|
{folder.children.count} folder, {folder.files.count} assets
|
||||||
</Typography>
|
</Typography>
|
||||||
</Flex>
|
</Flex>
|
||||||
@ -96,6 +101,8 @@ export const FolderList = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
FolderList.defaultProps = {
|
FolderList.defaultProps = {
|
||||||
|
onEditFolder: null,
|
||||||
|
onSelectFolder: null,
|
||||||
size: 'M',
|
size: 'M',
|
||||||
selectedFolders: [],
|
selectedFolders: [],
|
||||||
title: null,
|
title: null,
|
||||||
@ -105,7 +112,8 @@ FolderList.propTypes = {
|
|||||||
folders: PropTypes.arrayOf(FolderDefinition).isRequired,
|
folders: PropTypes.arrayOf(FolderDefinition).isRequired,
|
||||||
size: PropTypes.oneOf(['S', 'M']),
|
size: PropTypes.oneOf(['S', 'M']),
|
||||||
selectedFolders: PropTypes.array,
|
selectedFolders: PropTypes.array,
|
||||||
onEditFolder: PropTypes.func.isRequired,
|
onClickFolder: PropTypes.func.isRequired,
|
||||||
onSelectFolder: PropTypes.func.isRequired,
|
onEditFolder: PropTypes.func,
|
||||||
|
onSelectFolder: PropTypes.func,
|
||||||
title: PropTypes.string,
|
title: PropTypes.string,
|
||||||
};
|
};
|
||||||
|
@ -7,14 +7,11 @@ import { useIntl } from 'react-intl';
|
|||||||
import pluginId from '../pluginId';
|
import pluginId from '../pluginId';
|
||||||
import { axiosInstance, getRequestUrl } from '../utils';
|
import { axiosInstance, getRequestUrl } from '../utils';
|
||||||
|
|
||||||
export const useAssets = ({ skipWhen, query, rawQuery }) => {
|
export const useAssets = ({ skipWhen, query = {} }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const toggleNotification = useNotification();
|
const toggleNotification = useNotification();
|
||||||
const { notifyStatus } = useNotifyAT();
|
const { notifyStatus } = useNotifyAT();
|
||||||
const dataRequestURL = getRequestUrl('files');
|
const dataRequestURL = getRequestUrl('files');
|
||||||
|
|
||||||
const getAssets = async () => {
|
|
||||||
try {
|
|
||||||
const { folder, ...paramsExceptFolder } = query;
|
const { folder, ...paramsExceptFolder } = query;
|
||||||
const params = {
|
const params = {
|
||||||
...paramsExceptFolder,
|
...paramsExceptFolder,
|
||||||
@ -26,6 +23,9 @@ export const useAssets = ({ skipWhen, query, rawQuery }) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getAssets = async () => {
|
||||||
|
try {
|
||||||
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
|
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
|
||||||
|
|
||||||
notifyStatus(
|
notifyStatus(
|
||||||
@ -46,7 +46,7 @@ export const useAssets = ({ skipWhen, query, rawQuery }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data, error, isLoading } = useQuery([pluginId, 'assets', rawQuery], getAssets, {
|
const { data, error, isLoading } = useQuery([pluginId, 'assets', stringify(params)], getAssets, {
|
||||||
enabled: !skipWhen,
|
enabled: !skipWhen,
|
||||||
staleTime: 0,
|
staleTime: 0,
|
||||||
cacheTime: 0,
|
cacheTime: 0,
|
||||||
|
@ -1,21 +1,17 @@
|
|||||||
import { stringify } from 'qs';
|
import { stringify } from 'qs';
|
||||||
import { useQuery } from 'react-query';
|
import { useQuery } from 'react-query';
|
||||||
import { useNotifyAT } from '@strapi/design-system/LiveRegions';
|
import { useNotifyAT } from '@strapi/design-system/LiveRegions';
|
||||||
import { useNotification, useQueryParams } from '@strapi/helper-plugin';
|
import { useNotification } from '@strapi/helper-plugin';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
|
|
||||||
import pluginId from '../pluginId';
|
import pluginId from '../pluginId';
|
||||||
import { axiosInstance, getRequestUrl } from '../utils';
|
import { axiosInstance, getRequestUrl } from '../utils';
|
||||||
|
|
||||||
export const useFolders = ({ enabled = true }) => {
|
export const useFolders = ({ enabled = true, query = {} }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const toggleNotification = useNotification();
|
const toggleNotification = useNotification();
|
||||||
const { notifyStatus } = useNotifyAT();
|
const { notifyStatus } = useNotifyAT();
|
||||||
const [{ rawQuery, query }] = useQueryParams();
|
|
||||||
const dataRequestURL = getRequestUrl('folders');
|
const dataRequestURL = getRequestUrl('folders');
|
||||||
|
|
||||||
const fetchFolders = async () => {
|
|
||||||
try {
|
|
||||||
const { folder, ...paramsExceptFolder } = query;
|
const { folder, ...paramsExceptFolder } = query;
|
||||||
const params = {
|
const params = {
|
||||||
...paramsExceptFolder,
|
...paramsExceptFolder,
|
||||||
@ -30,6 +26,9 @@ export const useFolders = ({ enabled = true }) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchFolders = async () => {
|
||||||
|
try {
|
||||||
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
|
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
|
||||||
|
|
||||||
notifyStatus(
|
notifyStatus(
|
||||||
@ -50,11 +49,15 @@ export const useFolders = ({ enabled = true }) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data, error, isLoading } = useQuery([pluginId, 'folders', rawQuery], fetchFolders, {
|
const { data, error, isLoading } = useQuery(
|
||||||
|
[pluginId, 'folders', stringify(params)],
|
||||||
|
fetchFolders,
|
||||||
|
{
|
||||||
enabled,
|
enabled,
|
||||||
staleTime: 0,
|
staleTime: 0,
|
||||||
cacheTime: 0,
|
cacheTime: 0,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return { data, error, isLoading };
|
return { data, error, isLoading };
|
||||||
};
|
};
|
||||||
|
@ -45,7 +45,7 @@ const useModalQueryParams = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleChangeFolder = folder => {
|
const handleChangeFolder = folder => {
|
||||||
setQueryObject(prev => ({ ...prev, folder }));
|
setQueryObject(prev => ({ ...prev, folder: folder.id ?? null }));
|
||||||
};
|
};
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -56,10 +56,12 @@ export const MediaLibrary = () => {
|
|||||||
|
|
||||||
const { data: assetsData, isLoading: assetsLoading, errors: assetsError } = useAssets({
|
const { data: assetsData, isLoading: assetsLoading, errors: assetsError } = useAssets({
|
||||||
skipWhen: !canRead,
|
skipWhen: !canRead,
|
||||||
|
query,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data: foldersData, isLoading: foldersLoading, errors: foldersError } = useFolders({
|
const { data: foldersData, isLoading: foldersLoading, errors: foldersError } = useFolders({
|
||||||
enabled: assetsData?.pagination?.page === 1 && !isFiltering,
|
enabled: assetsData?.pagination?.page === 1 && !isFiltering,
|
||||||
|
query,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { data: folderStructure, isLoading: folderStructureIsLoading } = useFolderStructure();
|
const { data: folderStructure, isLoading: folderStructureIsLoading } = useFolderStructure();
|
||||||
@ -98,6 +100,13 @@ export const MediaLibrary = () => {
|
|||||||
toggleEditFolderDialog(payload);
|
toggleEditFolderDialog(payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleClickFolder = folder => {
|
||||||
|
setQuery({
|
||||||
|
...query,
|
||||||
|
folder: folder.id,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useFocusWhenNavigate();
|
useFocusWhenNavigate();
|
||||||
|
|
||||||
const folders = foldersData?.results;
|
const folders = foldersData?.results;
|
||||||
@ -213,6 +222,7 @@ export const MediaLibrary = () => {
|
|||||||
{folders?.length > 0 && !isFiltering && (
|
{folders?.length > 0 && !isFiltering && (
|
||||||
<FolderList
|
<FolderList
|
||||||
folders={folders}
|
folders={folders}
|
||||||
|
onClickFolder={handleClickFolder}
|
||||||
onEditFolder={handleEditFolder}
|
onEditFolder={handleEditFolder}
|
||||||
onSelectFolder={selectOne}
|
onSelectFolder={selectOne}
|
||||||
selectedFolders={selected.filter(({ type }) => type === 'folder')}
|
selectedFolders={selected.filter(({ type }) => type === 'folder')}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user