useFolderStructure: Move root folder creation into hook

This commit is contained in:
Gustav Hansen 2022-05-05 22:42:35 +02:00
parent 4b7abb9edc
commit 4a33486ec6
6 changed files with 63 additions and 46 deletions

View File

@ -27,7 +27,7 @@ import * as yup from 'yup';
import { PreviewBox } from './PreviewBox'; import { PreviewBox } from './PreviewBox';
import { ContextInfo } from '../ContextInfo'; import { ContextInfo } from '../ContextInfo';
import { getTrad } from '../../utils'; import { getTrad, findRecursiveFolderByValue } from '../../utils';
import formatBytes from '../../utils/formatBytes'; import formatBytes from '../../utils/formatBytes';
import { useEditAsset } from '../../hooks/useEditAsset'; import { useEditAsset } from '../../hooks/useEditAsset';
import { ReplaceMediaButton } from './ReplaceMediaButton'; import { ReplaceMediaButton } from './ReplaceMediaButton';
@ -47,6 +47,7 @@ export const EditAssetDialog = ({
canCopyLink, canCopyLink,
canDownload, canDownload,
trackedLocation, trackedLocation,
folderStructure,
}) => { }) => {
const { formatMessage, formatDate } = useIntl(); const { formatMessage, formatDate } = useIntl();
const submitButtonRef = useRef(null); const submitButtonRef = useRef(null);
@ -94,18 +95,21 @@ export const EditAssetDialog = ({
} }
}; };
const activeFolderId = asset?.folder?.id;
const initialFormData = { const initialFormData = {
name: asset.name, name: asset.name,
alternativeText: asset.alternativeText || '', alternativeText: asset.alternativeText || '',
caption: asset.caption || '', caption: asset.caption || '',
parent: { parent: {
value: null, value: activeFolderId ?? null,
// TODO label:
label: 'Media Library', findRecursiveFolderByValue(folderStructure, activeFolderId)?.label ??
...asset?.folder?.parent, folderStructure[0].label,
}, },
}; };
console.log(asset);
const handleClose = values => { const handleClose = values => {
if (!isEqual(initialFormData, values)) { if (!isEqual(initialFormData, values)) {
handleConfirmClose(); handleConfirmClose();
@ -225,7 +229,7 @@ export const EditAssetDialog = ({
/> />
<Stack spacing={1}> <Stack spacing={1}>
<FieldLabel> <FieldLabel for="asset-folder">
{formatMessage({ {formatMessage({
id: getTrad('form.input.label.file-location'), id: getTrad('form.input.label.file-location'),
defaultMessage: 'Location', defaultMessage: 'Location',
@ -235,10 +239,18 @@ export const EditAssetDialog = ({
<SelectTree <SelectTree
name="parent" name="parent"
defaultValue={values.parent} defaultValue={values.parent}
options={[]} options={folderStructure}
onChange={value => { onChange={value => {
setFieldValue('parent', value); setFieldValue('parent', value);
}} }}
menuPortalTarget={document.querySelector('body')}
inputId="asset-folder"
{...(errors.parent
? {
'aria-errormessage': 'folder-parent-error',
'aria-invalid': true,
}
: {})}
/> />
</Stack> </Stack>
</Stack> </Stack>
@ -298,5 +310,6 @@ EditAssetDialog.propTypes = {
canCopyLink: PropTypes.bool.isRequired, canCopyLink: PropTypes.bool.isRequired,
canDownload: PropTypes.bool.isRequired, canDownload: PropTypes.bool.isRequired,
onClose: PropTypes.func.isRequired, onClose: PropTypes.func.isRequired,
folderStructure: PropTypes.array.isRequired,
trackedLocation: PropTypes.string, trackedLocation: PropTypes.string,
}; };

View File

@ -19,7 +19,7 @@ import { Typography } from '@strapi/design-system/Typography';
import { VisuallyHidden } from '@strapi/design-system/VisuallyHidden'; import { VisuallyHidden } from '@strapi/design-system/VisuallyHidden';
import { Form, useNotification, getAPIInnerErrors, useQueryParams } from '@strapi/helper-plugin'; import { Form, useNotification, getAPIInnerErrors, useQueryParams } from '@strapi/helper-plugin';
import { getTrad } from '../../utils'; import { getTrad, findRecursiveFolderByValue } from '../../utils';
import { useEditFolder } from '../../hooks/useEditFolder'; import { useEditFolder } from '../../hooks/useEditFolder';
import { ContextInfo } from '../ContextInfo'; import { ContextInfo } from '../ContextInfo';
import SelectTree from '../SelectTree'; import SelectTree from '../SelectTree';
@ -34,51 +34,23 @@ const folderSchema = yup.object({
.nullable(true), .nullable(true),
}); });
function findByValue(data, value) { export const EditFolderDialog = ({ onClose, folder, folderStructure }) => {
let result;
function iter(a) {
if (a.value === value) {
result = a;
return true;
}
return Array.isArray(a.children) && a.children.some(iter);
}
data.some(iter);
return result;
}
export const EditFolderDialog = ({ onClose, folder, folderStructure: remoteFolderStructure }) => {
const submitButtonRef = useRef(null); const submitButtonRef = useRef(null);
const { formatMessage, formatDate } = useIntl(); const { formatMessage, formatDate } = useIntl();
const { editFolder, isLoading } = useEditFolder(); const { editFolder, isLoading } = useEditFolder();
const toggleNotification = useNotification(); const toggleNotification = useNotification();
const [{ query }] = useQueryParams(); const [{ query }] = useQueryParams();
const rootFolder = {
value: null,
label: formatMessage({
id: getTrad('form.input.label.folder-location-default-label'),
defaultMessage: 'Media Library',
}),
children: [],
};
const folderStructure = [ const activeFolderId = parseInt(
{ folder?.parent?.id ?? query?.folder ?? folderStructure[0].value,
...rootFolder, 10
children: remoteFolderStructure, );
},
];
const activeFolderId = parseInt(folder?.parent?.id ?? query?.folder ?? rootFolder.value, 10);
const initialFormData = Object.assign({}, folder, { const initialFormData = Object.assign({}, folder, {
parent: { parent: {
value: activeFolderId, value: activeFolderId,
label: findByValue(folderStructure, activeFolderId)?.label ?? rootFolder.label, label:
findRecursiveFolderByValue(folderStructure, activeFolderId)?.label ??
folderStructure[0].label,
}, },
}); });

View File

@ -1,7 +1,8 @@
import { useQuery } from 'react-query'; import { useQuery } from 'react-query';
import { useIntl } from 'react-intl';
import pluginId from '../pluginId'; import pluginId from '../pluginId';
import { axiosInstance, getRequestUrl } from '../utils'; import { axiosInstance, getRequestUrl, getTrad } from '../utils';
import { recursiveRenameKeys } from './utils/rename-keys'; import { recursiveRenameKeys } from './utils/rename-keys';
const FIELD_MAPPING = { const FIELD_MAPPING = {
@ -10,6 +11,7 @@ const FIELD_MAPPING = {
}; };
export const useFolderStructure = ({ enabled = true } = {}) => { export const useFolderStructure = ({ enabled = true } = {}) => {
const { formatMessage } = useIntl();
const dataRequestURL = getRequestUrl('folder-structure'); const dataRequestURL = getRequestUrl('folder-structure');
const fetchFolderStructure = async () => { const fetchFolderStructure = async () => {
@ -17,7 +19,18 @@ export const useFolderStructure = ({ enabled = true } = {}) => {
data: { data }, data: { data },
} = await axiosInstance.get(dataRequestURL); } = await axiosInstance.get(dataRequestURL);
return data.map(f => recursiveRenameKeys(f, key => FIELD_MAPPING?.[key] ?? key)); const children = data.map(f => recursiveRenameKeys(f, key => FIELD_MAPPING?.[key] ?? key));
return [
{
value: null,
label: formatMessage({
id: getTrad('form.input.label.folder-location-default-label'),
defaultMessage: 'Media Library',
}),
children,
},
];
}; };
const { data, error, isLoading } = useQuery( const { data, error, isLoading } = useQuery(

View File

@ -302,6 +302,7 @@ export const MediaLibrary = () => {
canUpdate={canUpdate} canUpdate={canUpdate}
canCopyLink={canCopyLink} canCopyLink={canCopyLink}
canDownload={canDownload} canDownload={canDownload}
folderStructure={folderStructure}
trackedLocation="upload" trackedLocation="upload"
/> />
)} )}

View File

@ -0,0 +1,17 @@
export default function findRecursiveFolderByValue(data, value) {
let result;
function iter(a) {
if (a.value === value) {
result = a;
return true;
}
return Array.isArray(a.children) && a.children.some(iter);
}
data.some(iter);
return result;
}

View File

@ -2,4 +2,5 @@ export { default as axiosInstance } from './axiosInstance';
export { default as formatBytes } from './formatBytes'; export { default as formatBytes } from './formatBytes';
export { default as getRequestUrl } from './getRequestUrl'; export { default as getRequestUrl } from './getRequestUrl';
export { default as getTrad } from './getTrad'; export { default as getTrad } from './getTrad';
export { default as findRecursiveFolderByValue } from './findRecursiveFolderByValue';
export * from './formatDuration'; export * from './formatDuration';