forked from rahulsainlll/git-trace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-bookmark.tsx
125 lines (117 loc) · 3.61 KB
/
new-bookmark.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
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
import { useState } from "react";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Bookmark } from "lucide-react";
import axios from "axios";
import SubmitButton from "@/components/submit-btn";
const NewBookmarkBtn = ({
name,
url,
description,
}: {
name: string;
url: string;
description?: string;
}) => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const [isDialogOpen, setIsDialogOpen] = useState(false);
const { data: session } = useSession();
const router = useRouter();
const handleBookmarkSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setLoading(true);
setError(null);
const formData = new FormData(e.currentTarget);
try {
await axios.post("/api/bookmarks", {
name: formData.get("name"),
url: formData.get("url"),
description: formData.get("description"),
});
setSuccess(true);
} catch (error) {
console.error("Failed to create bookmark:", error);
setError("There was an issue creating the bookmark. Please try again.");
} finally {
setLoading(false);
}
};
const handleButtonClick = () => {
if (!session?.user) {
router.push("/auth/signin");
} else {
setIsDialogOpen(true);
}
};
if (success) {
router.push("/dashboard");
}
return (
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<DialogTrigger asChild>
<Button className="rounded-full" onClick={handleButtonClick}>
<Bookmark className="w-4 h-4" />
</Button>
</DialogTrigger>
{session?.user && (
<DialogContent className="sm:max-w-[425px] rounded-md">
<DialogHeader>
<DialogTitle>New Bookmark</DialogTitle>
<DialogDescription>
Create a new bookmark to get started
</DialogDescription>
</DialogHeader>
<form className="flex gap-4 flex-col" onSubmit={handleBookmarkSubmit}>
<div className="grid sm:grid-cols-2 gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="name">Name</Label>
<Input
id="name"
name="name"
placeholder="Project Name"
defaultValue={name}
required
/>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="url">URL</Label>
<Input
id="url"
name="url"
placeholder="https://example.com"
defaultValue={url}
required
/>
</div>
</div>
<div className="flex flex-col gap-2">
<Label htmlFor="description">Description</Label>
<Textarea
name="description"
id="description"
placeholder="Description (optional)"
defaultValue={description}
/>
</div>
<SubmitButton loading={loading} />
</form>
</DialogContent>
)}
</Dialog>
);
};
export default NewBookmarkBtn;