-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteCard.jsx
142 lines (132 loc) · 4.29 KB
/
NoteCard.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use client";
import Doc from "./logo/Doc";
import Options from "./logo/Options";
import Link from "next/link";
import { useToast } from "@/components/ui/use-toast";
import { deleteFiles } from "@/src/queries";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { useState } from "react";
import { useDeleteNoteMutation } from "@/data/notes";
const NoteCard = ({ id, name, url }) => {
const { toast } = useToast();
const deleteMutation = useDeleteNoteMutation();
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
function deleteNote() {
if (!url) {
toast({
variant: "destructive",
title: "🚧 Error",
description: "No file found to delete",
});
}
deleteFiles(url);
deleteMutation.mutate(id, {
onSuccess: () => {
toast({
title: "✅ Note deleted",
description: "The note has been successfully deleted",
});
},
});
}
const clickDelete = (event) => {
event.stopPropagation();
setIsDeleteDialogOpen(true);
};
function downloadPDF() {
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const url = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", `${name}.pdf`);
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
});
}
return (
<div className="w-48 h-auto pb-4 pt-2 bg-secondary border-[1.5px] border-border rounded-lg">
<div className="h-auto w-full border-b-[1.5px] border-white/0 flex items-center justify-between px-4 py-2">
<span className="w-full h-auto text-nowrap overflow-x-auto">
{name}
</span>
<DropdownMenu>
<DropdownMenuTrigger className="hover:bg-util rounded-full p-[1.75px] flex justify-end">
<Options />
</DropdownMenuTrigger>
<DropdownMenuContent className="bg-secondary border-[1.5px] border-border rounded-lg">
<DropdownMenuLabel>Editing {name}</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem className="hover:bg-util cursor-pointer">
<Link href={url} target="_blank">
Open in browser
</Link>
</DropdownMenuItem>
<DropdownMenuItem
onClick={downloadPDF}
className="hover:bg-util cursor-pointer"
>
Download
</DropdownMenuItem>
<DropdownMenuItem
onClick={clickDelete}
className="hover:bg-util cursor-pointer"
>
Delete
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className=" w-full h-5/6 pb-5 px-3">
<div className="bg-util w-full text-text/50 h-full rounded-md flex items-center justify-center">
<Doc />
</div>
</div>
{isDeleteDialogOpen && (
<AlertDialog
open={isDeleteDialogOpen}
onOpenChange={setIsDeleteDialogOpen}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone. This will permanently delete the
file named {name}. Are you sure you want to delete this file?
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel className="border-border">
Cancel
</AlertDialogCancel>
<AlertDialogAction onClick={deleteNote} className="bg-primary/50">
Continue
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)}
</div>
);
};
export default NoteCard;