Skip to content

Commit

Permalink
Some work on paperless delete buttong
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jul 25, 2024
1 parent 46b2f93 commit 46b8e50
Showing 1 changed file with 60 additions and 25 deletions.
85 changes: 60 additions & 25 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ import OpenInternalLink from "./internal-link";
import OpenExternalLink from "./external-link";
import type { UsersTableType } from "@/server/db/schema";
import { Download } from "lucide-react";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { toast } from "sonner";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -100,34 +112,24 @@ const fetchUserData = async (): Promise<UsersTableType> => {
async function deleteDocument(documentId: number) {
const userData = await getUserData();
if (!userData) {
console.error("Error getting user data");
return;
throw new Error("User data not found");
}
const body = {
documents: [documentId],
method: "delete",
};
try {
const response = await fetch(
`${userData.paperlessURL}/api/documents/bulk_edit/ `,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${userData.paperlessToken}`,
},
body: JSON.stringify(body),
const response = await fetch(
`${userData.paperlessURL}/api/documents/bulk_edit/ `,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Token ${userData.paperlessToken}`,
},
);
if (!response.ok) {
console.error("Failed to delete document");
return;
}
console.log("Document deleted successfully");
} catch (error) {
console.error("Error deleting document:", error);
return null;
}
body: JSON.stringify(body),
},
);
return response;
}

function DocumentViewer(props: { id: number }) {
Expand Down Expand Up @@ -228,9 +230,42 @@ function DocumentViewer(props: { id: number }) {
>
Open
</OpenExternalLink>
<Button className="mr-2 w-24" variant="destructive">
Delete
</Button>
<AlertDialog>
<AlertDialogTrigger>
<Button className="w-24" variant="destructive">
Delete
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete
the recording.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={async () => {
const response = await deleteDocument(props.id);
if (response.ok) {
toast("Pdf deleted", {
description: "The recording has been deleted.",
});
} else {
toast("Error deleting pdf", {
description:
"An error occurred while deleting the recording.",
});
}
}}
>
Continue
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</div>
</div>
Expand Down

0 comments on commit 46b8e50

Please sign in to comment.