Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat(dataroom): add home folder to move folder modal

Marc Seitz1995db5440ea
diff --git a/components/datarooms/folders/selection-tree.tsx b/components/datarooms/folders/selection-tree.tsx
index 1252d19b..19f9a917 100644
--- a/components/datarooms/folders/selection-tree.tsx
+++ b/components/datarooms/folders/selection-tree.tsx
@@ -80,16 +80,27 @@ const SidebarFoldersSelectipon = ({
return [];
}, [folders]);
+ // Create a virtual "Home" folder
+ const homeFolder: Partial<DataroomFolderWithDocuments> = {
+ // @ts-ignore
+ id: null,
+ name: "Home",
+ path: "/",
+ childFolders: nestedFolders,
+ documents: [],
+ };
+
return (
<FileTree>
- {nestedFolders.map((folder) => (
- <FolderComponentSelection
- key={folder.id}
- folder={folder}
- selectedFolder={selectedFolder}
- setSelectedFolder={setSelectedFolder}
- />
- ))}
+ {/* {nestedFolders.map((folder) => ( */}
+ <FolderComponentSelection
+ // key={folder.id}
+ // @ts-ignore
+ folder={homeFolder}
+ selectedFolder={selectedFolder}
+ setSelectedFolder={setSelectedFolder}
+ />
+ {/* ))} */}
</FileTree>
);
};
diff --git a/components/datarooms/move-dataroom-folder-modal.tsx b/components/datarooms/move-dataroom-folder-modal.tsx
index 51574b04..0d367412 100644
--- a/components/datarooms/move-dataroom-folder-modal.tsx
+++ b/components/datarooms/move-dataroom-folder-modal.tsx
@@ -50,11 +50,30 @@ export function MoveToDataroomFolderModal({
if (!selectedFolder) return;
setLoading(true);
+
+ const key = `/api/teams/${teamId}/datarooms/${dataroomId}${currentPath !== "" ? `/folders/documents/${currentPath}` : "/documents"}`;
+
+ mutate(
+ key,
+ (documents: any[] | undefined) => {
+ if (!documents) return documents;
+
+ // Filter out the document that are being moved
+ const updatedDocuments = documents.filter(
+ (doc) => doc.id !== documentId,
+ );
+
+ // Return the updated list of documents
+ return updatedDocuments;
+ },
+ false,
+ );
+
try {
const response = await fetch(
`/api/teams/${teamId}/datarooms/${dataroomId}/documents/${documentId}`,
{
- method: "PUT",
+ method: "PATCH",
headers: {
"Content-Type": "application/json",
},
@@ -72,23 +91,23 @@ export function MoveToDataroomFolderModal({
return;
}
- const { newPath, oldPath } = (await response.json()) as {
+ const { newPath } = (await response.json()) as {
newPath: string;
- oldPath: string;
};
- toast.success("Document moved successfully!");
-
- mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders`);
- mutate(`/api/teams/${teamId}/datarooms/${dataroomId}/folders${oldPath}`);
+ // update current folder
+ mutate(key);
+ // update new folder (or home)
mutate(
- `/api/teams/${teamId}/datarooms/${dataroomId}/folders${newPath}`,
- ).then(() => {
- router.push(`/datarooms/${dataroomId}/documents${newPath}`);
- });
+ `/api/teams/${teamId}/datarooms/${dataroomId}${newPath ? `/folders/documents${newPath}` : "/documents"}`,
+ );
+
+ toast.success("Document moved successfully!");
} catch (error) {
console.error("Error moving document", error);
- toast.error("Failed to move document. Try again.");
+ toast.error("Failed to move document");
+ // Revert the UI back to the previous state
+ mutate(key);
} finally {
setLoading(false);
setOpen(false);
diff --git a/components/documents/move-folder-modal.tsx b/components/documents/move-folder-modal.tsx
index 73383ea4..6e0cecd2 100644
--- a/components/documents/move-folder-modal.tsx
+++ b/components/documents/move-folder-modal.tsx
@@ -17,7 +17,7 @@ import {
DialogTitle,
} from "@/components/ui/dialog";
-export type TSelectedFolder = { id: string; name: string } | null;
+export type TSelectedFolder = { id: string | null; name: string } | null;
export function MoveToFolderModal({
open,
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
index e9d8a784..8432eaa0 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/documents/[documentId]/index.ts
@@ -10,8 +10,8 @@ export default async function handle(
req: NextApiRequest,
res: NextApiResponse,
) {
- if (req.method === "PUT") {
- // PUT /api/teams/:teamId/datarooms/:id/documents/:documentId
+ if (req.method === "PATCH") {
+ // PATCH /api/teams/:teamId/datarooms/:id/documents/:documentId
const session = await getServerSession(req, res, authOptions);
if (!session) {
res.status(401).end("Unauthorized");
@@ -128,8 +128,8 @@ export default async function handle(
return res.status(204).end(); // No Content
} catch (error) {}
} else {
- // We only allow GET, PUT and DELETE requests
- res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
+ // We only allow PATCH and DELETE requests
+ res.setHeader("Allow", ["PATCH", "DELETE"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}