forked from rahulsainlll/git-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteBookmarkButton.tsx
79 lines (74 loc) · 2.08 KB
/
DeleteBookmarkButton.tsx
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
"use client";
import { useState } from "react";
import axios from "axios";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Loader } from "lucide-react";
interface DeleteBookmarkButtonProps {
bookmarkId: string;
}
export default function DeleteBookmarkButton({
bookmarkId,
}: DeleteBookmarkButtonProps) {
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const handleDelete = async () => {
setLoading(true);
try {
await axios.delete(`/api/bookmarks/${bookmarkId}`);
window.location.reload();
} catch (error) {
console.error("Failed to delete bookmark:", error);
// Optionally, show an error message to the user
} finally {
setLoading(false);
}
};
return (
<>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button onClick={() => setOpen(true)} disabled={loading}>
{loading ? <Loader className="animate-spin w-4 h-4" /> : "Delete"}
</Button>
</DialogTrigger>
<DialogContent className="dialog-content">
<DialogHeader>
<DialogTitle>Confirm Deletion</DialogTitle>
<DialogDescription>
Are you sure you want to delete this bookmark? This action cannot
be undone.
</DialogDescription>
</DialogHeader>
<div className="flex gap-4">
<Button
onClick={handleDelete}
variant="destructive"
disabled={loading}
>
{loading ? (
<Loader className="animate-spin w-4 h-4" />
) : (
"Confirm"
)}
</Button>
<Button
onClick={() => setOpen(false)}
variant="outline"
disabled={loading}
>
Cancel
</Button>
</div>
</DialogContent>
</Dialog>
</>
);
}