From 96e435a9760a67d349ea8a97d9945e4e89a99efb Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 2 Sep 2021 19:52:34 +0200 Subject: [PATCH] rename files --- src/components/FileBrowser.tsx | 13 +++++++++---- src/functions/pathRename.ts | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/functions/pathRename.ts diff --git a/src/components/FileBrowser.tsx b/src/components/FileBrowser.tsx index 8d69035..7427600 100644 --- a/src/components/FileBrowser.tsx +++ b/src/components/FileBrowser.tsx @@ -15,6 +15,7 @@ import FileOpen from "./FileOpen" import FileUploadButton from "./FileUploadButton" import { ReactComponent as Spinner } from "./../assets/spinner.svg" import FileBrowserList from "./FileBrowserList" +import pathRename from "../functions/pathRename" function uriToPath(pathname:string) { // strip the "/f" from e.g. "/f/dir1/dir2" @@ -163,12 +164,16 @@ const FileBrowser: React.FC = (props) => { editId={editID} editEnable={editEnable} - onRenameDone={(id,changed,newName)=>{ + onRenameDone={async (id,changed,newName)=>{ setEditEnable(false) if (changed){ - console.debug("Changed: ",newName) - }else{ - console.debug("Not changed") + // TODO: maybe change the name on client so it seems more smooth rather then haveing it refetch + // TODO: input check & error handling + await moveMutation({variables:{ + src:id, + dest: pathRename(id,newName) + }}) + refetchDir() } }} /> diff --git a/src/functions/pathRename.ts b/src/functions/pathRename.ts new file mode 100644 index 0000000..435ba64 --- /dev/null +++ b/src/functions/pathRename.ts @@ -0,0 +1,11 @@ +function pathRename(id:string, newFilename: string): string { + const isDir = id.endsWith("/") + const parts = id.split("/") + if (!parts.length) + throw new Error("Maleformed id") + parts[parts.length - (isDir?2:1)] = newFilename + + return parts.join("/") +} + +export default pathRename