Skip to content

Commit 121f163

Browse files
committed
Handle new file rename/move API
1 parent 2653321 commit 121f163

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

resources/scripts/api/server/files/renameFiles.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import http from '@/api/http';
22

33
interface Data {
4-
renameFrom: string;
5-
renameTo: string;
4+
to: string;
5+
from: string;
66
}
77

88
export default (uuid: string, directory: string, files: Data[]): Promise<void> => {
99
return new Promise((resolve, reject) => {
10-
http.put(`/api/client/servers/${uuid}/files/rename`, {
11-
root: directory,
12-
files: files.map(f => ({ from: f.renameFrom, to: f.renameTo })),
13-
})
10+
http.put(`/api/client/servers/${uuid}/files/rename`, { root: directory, files })
1411
.then(() => resolve())
1512
.catch(reject);
1613
});

resources/scripts/components/server/files/FileDropdownMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export default ({ file }: { file: FileObject }) => {
117117
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
118118
<FontAwesomeIcon icon={faEllipsisH}/>
119119
<RenameFileModal
120-
file={file}
120+
files={[ file.name ]}
121121
visible={!!modal}
122122
useMoveTerminology={modal === 'move'}
123123
onDismissed={() => setModal(null)}

resources/scripts/components/server/files/RenameFileModal.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Field from '@/components/elements/Field';
55
import { join } from 'path';
66
import renameFiles from '@/api/server/files/renameFiles';
77
import { ServerContext } from '@/state/server';
8-
import { FileObject } from '@/api/server/files/loadDirectory';
98
import tw from 'twin.macro';
109
import Button from '@/components/elements/Button';
1110
import useServer from '@/plugins/useServer';
@@ -16,9 +15,9 @@ interface FormikValues {
1615
name: string;
1716
}
1817

19-
type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean };
18+
type Props = RequiredModalProps & { files: string[]; useMoveTerminology?: boolean };
2019

21-
export default ({ file, useMoveTerminology, ...props }: Props) => {
20+
export default ({ files, useMoveTerminology, ...props }: Props) => {
2221
const { uuid } = useServer();
2322
const { mutate } = useFileManagerSwr();
2423
const { clearFlashes, clearAndAddHttpError } = useFlash();
@@ -28,18 +27,24 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
2827
clearFlashes('files');
2928

3029
const len = name.split('/').length;
31-
if (!useMoveTerminology && len === 1) {
32-
// Rename the file within this directory.
33-
mutate(files => files.map(f => f.uuid === file.uuid ? { ...f, name } : f), false);
34-
} else if ((useMoveTerminology || len > 1) && file.uuid.length) {
35-
// Remove the file from this directory since they moved it elsewhere.
36-
mutate(files => files.filter(f => f.uuid !== file.uuid), false);
30+
if (files.length === 1) {
31+
if (!useMoveTerminology && len === 1) {
32+
// Rename the file within this directory.
33+
mutate(data => data.map(f => f.name === files[0] ? { ...f, name } : f), false);
34+
} else if ((useMoveTerminology || len > 1)) {
35+
// Remove the file from this directory since they moved it elsewhere.
36+
mutate(data => data.filter(f => f.name !== files[0]), false);
37+
}
3738
}
3839

39-
const renameFrom = join(directory, file.name);
40-
const renameTo = join(directory, name);
40+
let data;
41+
if (useMoveTerminology && files.length > 1) {
42+
data = files.map(f => ({ from: f, to: join(name, f) }));
43+
} else {
44+
data = files.map(f => ({ from: f, to: name }));
45+
}
4146

42-
renameFiles(uuid, directory, [ { renameFrom, renameTo } ])
47+
renameFiles(uuid, directory, data)
4348
.then(() => props.onDismissed())
4449
.catch(error => {
4550
mutate();
@@ -49,7 +54,7 @@ export default ({ file, useMoveTerminology, ...props }: Props) => {
4954
};
5055

5156
return (
52-
<Formik onSubmit={submit} initialValues={{ name: file.name }}>
57+
<Formik onSubmit={submit} initialValues={{ name: files.length > 1 ? '' : (files[0] || '') }}>
5358
{({ isSubmitting, values }) => (
5459
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
5560
<Form css={tw`m-0`}>

0 commit comments

Comments
 (0)