Handle new file rename/move API

This commit is contained in:
Dane Everitt 2020-07-11 16:20:42 -07:00
parent 2653321fc2
commit 121f163b81
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 22 additions and 20 deletions

View file

@ -1,16 +1,13 @@
import http from '@/api/http';
interface Data {
renameFrom: string;
renameTo: string;
to: string;
from: string;
}
export default (uuid: string, directory: string, files: Data[]): Promise<void> => {
return new Promise((resolve, reject) => {
http.put(`/api/client/servers/${uuid}/files/rename`, {
root: directory,
files: files.map(f => ({ from: f.renameFrom, to: f.renameTo })),
})
http.put(`/api/client/servers/${uuid}/files/rename`, { root: directory, files })
.then(() => resolve())
.catch(reject);
});

View file

@ -117,7 +117,7 @@ export default ({ file }: { file: FileObject }) => {
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
<FontAwesomeIcon icon={faEllipsisH}/>
<RenameFileModal
file={file}
files={[ file.name ]}
visible={!!modal}
useMoveTerminology={modal === 'move'}
onDismissed={() => setModal(null)}

View file

@ -5,7 +5,6 @@ import Field from '@/components/elements/Field';
import { join } from 'path';
import renameFiles from '@/api/server/files/renameFiles';
import { ServerContext } from '@/state/server';
import { FileObject } from '@/api/server/files/loadDirectory';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import useServer from '@/plugins/useServer';
@ -16,9 +15,9 @@ interface FormikValues {
name: string;
}
type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean };
type Props = RequiredModalProps & { files: string[]; useMoveTerminology?: boolean };
export default ({ file, useMoveTerminology, ...props }: Props) => {
export default ({ files, useMoveTerminology, ...props }: Props) => {
const { uuid } = useServer();
const { mutate } = useFileManagerSwr();
const { clearFlashes, clearAndAddHttpError } = useFlash();
@ -28,18 +27,24 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
clearFlashes('files');
const len = name.split('/').length;
if (!useMoveTerminology && len === 1) {
// Rename the file within this directory.
mutate(files => files.map(f => f.uuid === file.uuid ? { ...f, name } : f), false);
} else if ((useMoveTerminology || len > 1) && file.uuid.length) {
// Remove the file from this directory since they moved it elsewhere.
mutate(files => files.filter(f => f.uuid !== file.uuid), false);
if (files.length === 1) {
if (!useMoveTerminology && len === 1) {
// Rename the file within this directory.
mutate(data => data.map(f => f.name === files[0] ? { ...f, name } : f), false);
} else if ((useMoveTerminology || len > 1)) {
// Remove the file from this directory since they moved it elsewhere.
mutate(data => data.filter(f => f.name !== files[0]), false);
}
}
const renameFrom = join(directory, file.name);
const renameTo = join(directory, name);
let data;
if (useMoveTerminology && files.length > 1) {
data = files.map(f => ({ from: f, to: join(name, f) }));
} else {
data = files.map(f => ({ from: f, to: name }));
}
renameFiles(uuid, directory, [ { renameFrom, renameTo } ])
renameFiles(uuid, directory, data)
.then(() => props.onDismissed())
.catch(error => {
mutate();
@ -49,7 +54,7 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
};
return (
<Formik onSubmit={submit} initialValues={{ name: file.name }}>
<Formik onSubmit={submit} initialValues={{ name: files.length > 1 ? '' : (files[0] || '') }}>
{({ isSubmitting, values }) => (
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
<Form css={tw`m-0`}>