Skip to content

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

add delete document function

Marc Seitz4131ec2b9492
diff --git a/components/shared/icons/more-vertical.tsx b/components/shared/icons/more-vertical.tsx
new file mode 100644
index 00000000..2791c20b
--- /dev/null
+++ b/components/shared/icons/more-vertical.tsx
@@ -0,0 +1,20 @@
+export default function MoreVertical({ className }: { className?: string }) {
+ return (
+ <svg
+ width="14"
+ height="14"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ shapeRendering="geometricPrecision"
+ className={className}
+ >
+ <circle cx="12" cy="12" r="1" />
+ <circle cx="12" cy="5" r="1" />
+ <circle cx="12" cy="19" r="1" />
+ </svg>
+ );
+}
\ No newline at end of file
diff --git a/pages/documents/[id].tsx b/pages/documents/[id].tsx
index 9eea885f..4c515117 100644
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@ -8,19 +8,26 @@ import LinkSheet from "@/components/links/link-sheet";
import Image from "next/image";
import LinksTable from "@/components/links/links-table";
import VisitorsTable from "@/components/visitors/visitors-table";
-import { useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import LoadingSpinner from "@/components/ui/loading-spinner";
import { toast } from "sonner";
+import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
+import { useRouter } from "next/router";
+import MoreVertical from "@/components/shared/icons/more-vertical";
export default function DocumentPage() {
- const { document, primaryVersion, error } = useDocument();
+ const { document: prismaDocument, primaryVersion, error } = useDocument();
+ const router = useRouter();
const [isLinkSheetOpen, setIsLinkSheetOpen] = useState<boolean>(false);
const [isEditingName, setIsEditingName] = useState<boolean>(false);
+ const [isFirstClick, setIsFirstClick] = useState<boolean>(false);
+ const [menuOpen, setMenuOpen] = useState<boolean>(false);
const nameRef = useRef<HTMLHeadingElement>(null);
const enterPressedRef = useRef<boolean>(false);
+ const dropdownRef = useRef<HTMLDivElement | null>(null);
const handleNameSubmit = async () => {
if (enterPressedRef.current) {
@@ -30,9 +37,9 @@ export default function DocumentPage() {
if (nameRef.current && isEditingName) {
const newName = nameRef.current.innerText;
- if (newName !== document!.name) {
+ if (newName !== prismaDocument!.name) {
const response = await fetch(
- `/api/documents/${document!.id}/update-name`,
+ `/api/documents/${prismaDocument!.id}/update-name`,
{
method: "POST",
headers: {
@@ -56,6 +63,71 @@ export default function DocumentPage() {
}
};
+
+ useEffect(() => {
+ function handleClickOutside(event: { target: any; }) {
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
+ setMenuOpen(false);
+ setIsFirstClick(false);
+ }
+ }
+
+ document.addEventListener("mousedown", handleClickOutside);
+ return () => {
+ document.removeEventListener("mousedown", handleClickOutside);
+ };
+ }, []);
+
+ const handleDeleteDocument = async (documentId: string) => {
+ // Prevent the first click from deleting the document
+ if (!isFirstClick) {
+ setIsFirstClick(true);
+ return;
+ }
+
+ const response = await fetch(`/api/documents/${documentId}`, {
+ method: "DELETE",
+ });
+
+ if (response.ok) {
+ setIsFirstClick(false);
+ setMenuOpen(false);
+ router.push("/documents")
+ toast.success("Document deleted successfully.");
+ } else {
+ const { message } = await response.json();
+ toast.error(message);
+ }
+ }
+
+ const handleMenuStateChange = (open: boolean ) => {
+ if (isFirstClick) {
+ setMenuOpen(true); // Keep the dropdown open on the first click
+ return;
+ }
+
+ // If the menu is closed, reset the isFirstClick state
+ if (!open) {
+ setIsFirstClick(false);
+ setMenuOpen(false); // Ensure the dropdown is closed
+ } else {
+ setMenuOpen(true); // Open the dropdown
+ }
+ };
+
+ const handleButtonClick = (event: any, documentId: string) => {
+ event.stopPropagation();
+ event.preventDefault();
+
+ if (isFirstClick) {
+ handleDeleteDocument(documentId);
+ setIsFirstClick(false);
+ setMenuOpen(false); // Close the dropdown after deleting
+ } else {
+ setIsFirstClick(true);
+ }
+ };
+
const preventEnterAndSubmit = (
event: React.KeyboardEvent<HTMLHeadingElement>
) => {
@@ -77,7 +149,7 @@ export default function DocumentPage() {
return (
<AppLayout>
<div>
- {document && primaryVersion ? (
+ {prismaDocument && primaryVersion ? (
<>
{/* Heading */}
<div className="flex flex-col items-start justify-between gap-x-8 gap-y-4 p-4 sm:flex-row sm:items-center sm:m-4">
@@ -101,7 +173,7 @@ export default function DocumentPage() {
onBlur={handleNameSubmit}
onKeyDown={preventEnterAndSubmit}
title="Click to edit"
- dangerouslySetInnerHTML={{ __html: document.name }}
+ dangerouslySetInnerHTML={{ __html: prismaDocument.name }}
/>
{isEditingName && (
<p className="text-sm text-muted-foreground mt-1">
@@ -111,14 +183,38 @@ export default function DocumentPage() {
</div>
</div>
</div>
- <Button onClick={() => setIsLinkSheetOpen(true)}>
- Create Link
- </Button>
+ <div className="flex items-center gap-x-4">
+ <DropdownMenu
+ open={menuOpen}
+ onOpenChange={handleMenuStateChange}
+ >
+ <DropdownMenuTrigger asChild>
+ <Button variant="ghost" className="h-8 w-8 p-0">
+ <span className="sr-only">Open menu</span>
+ <MoreVertical className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" ref={dropdownRef}>
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DropdownMenuItem
+ className="text-destructive focus:bg-destructive focus:text-destructive-foreground"
+ onClick={(event) =>
+ handleButtonClick(event, prismaDocument.id)
+ }
+ >
+ {isFirstClick ? "Really delete?" : "Delete document"}
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ <Button onClick={() => setIsLinkSheetOpen(true)}>
+ Create Link
+ </Button>
+ </div>
</div>
{/* Stats */}
- {document.numPages !== null && (
+ {prismaDocument.numPages !== null && (
<StatsChart
- documentId={document.id}
+ documentId={prismaDocument.id}
totalPages={primaryVersion.numPages!}
/>
)}