Handle new file rename/move API
This commit is contained in:
parent
2653321fc2
commit
121f163b81
3 changed files with 22 additions and 20 deletions
|
@ -1,16 +1,13 @@
|
||||||
import http from '@/api/http';
|
import http from '@/api/http';
|
||||||
|
|
||||||
interface Data {
|
interface Data {
|
||||||
renameFrom: string;
|
to: string;
|
||||||
renameTo: string;
|
from: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (uuid: string, directory: string, files: Data[]): Promise<void> => {
|
export default (uuid: string, directory: string, files: Data[]): Promise<void> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
http.put(`/api/client/servers/${uuid}/files/rename`, {
|
http.put(`/api/client/servers/${uuid}/files/rename`, { root: directory, files })
|
||||||
root: directory,
|
|
||||||
files: files.map(f => ({ from: f.renameFrom, to: f.renameTo })),
|
|
||||||
})
|
|
||||||
.then(() => resolve())
|
.then(() => resolve())
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
});
|
||||||
|
|
|
@ -117,7 +117,7 @@ export default ({ file }: { file: FileObject }) => {
|
||||||
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
|
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
|
||||||
<FontAwesomeIcon icon={faEllipsisH}/>
|
<FontAwesomeIcon icon={faEllipsisH}/>
|
||||||
<RenameFileModal
|
<RenameFileModal
|
||||||
file={file}
|
files={[ file.name ]}
|
||||||
visible={!!modal}
|
visible={!!modal}
|
||||||
useMoveTerminology={modal === 'move'}
|
useMoveTerminology={modal === 'move'}
|
||||||
onDismissed={() => setModal(null)}
|
onDismissed={() => setModal(null)}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import Field from '@/components/elements/Field';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import renameFiles from '@/api/server/files/renameFiles';
|
import renameFiles from '@/api/server/files/renameFiles';
|
||||||
import { ServerContext } from '@/state/server';
|
import { ServerContext } from '@/state/server';
|
||||||
import { FileObject } from '@/api/server/files/loadDirectory';
|
|
||||||
import tw from 'twin.macro';
|
import tw from 'twin.macro';
|
||||||
import Button from '@/components/elements/Button';
|
import Button from '@/components/elements/Button';
|
||||||
import useServer from '@/plugins/useServer';
|
import useServer from '@/plugins/useServer';
|
||||||
|
@ -16,9 +15,9 @@ interface FormikValues {
|
||||||
name: string;
|
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 { uuid } = useServer();
|
||||||
const { mutate } = useFileManagerSwr();
|
const { mutate } = useFileManagerSwr();
|
||||||
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
||||||
|
@ -28,18 +27,24 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
|
||||||
clearFlashes('files');
|
clearFlashes('files');
|
||||||
|
|
||||||
const len = name.split('/').length;
|
const len = name.split('/').length;
|
||||||
if (!useMoveTerminology && len === 1) {
|
if (files.length === 1) {
|
||||||
// Rename the file within this directory.
|
if (!useMoveTerminology && len === 1) {
|
||||||
mutate(files => files.map(f => f.uuid === file.uuid ? { ...f, name } : f), false);
|
// Rename the file within this directory.
|
||||||
} else if ((useMoveTerminology || len > 1) && file.uuid.length) {
|
mutate(data => data.map(f => f.name === files[0] ? { ...f, name } : f), false);
|
||||||
// Remove the file from this directory since they moved it elsewhere.
|
} else if ((useMoveTerminology || len > 1)) {
|
||||||
mutate(files => files.filter(f => f.uuid !== file.uuid), false);
|
// 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);
|
let data;
|
||||||
const renameTo = join(directory, name);
|
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())
|
.then(() => props.onDismissed())
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
mutate();
|
mutate();
|
||||||
|
@ -49,7 +54,7 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Formik onSubmit={submit} initialValues={{ name: file.name }}>
|
<Formik onSubmit={submit} initialValues={{ name: files.length > 1 ? '' : (files[0] || '') }}>
|
||||||
{({ isSubmitting, values }) => (
|
{({ isSubmitting, values }) => (
|
||||||
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
|
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
|
||||||
<Form css={tw`m-0`}>
|
<Form css={tw`m-0`}>
|
||||||
|
|
Loading…
Reference in a new issue