/
new-book-form.tsx
223 lines (213 loc) · 6.56 KB
/
new-book-form.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { Wand2 } from "lucide-react";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Separator } from "@/components/ui/separator";
import { Textarea } from "@/components/ui/textarea";
import { useToast } from "@/components/ui/use-toast";
import { useUser } from "@clerk/nextjs";
import { ImageUpload } from "@/components/image-upload";
const formSchema = z.object({
name: z.string().min(1, {
message: "Name is required.",
}),
author: z.string().min(1, {
message: "Name is required.",
}),
description: z.string().min(1, {
message: "Description is required.",
}),
src: z.string().min(1, {
message: "Image is required.",
}),
user_id: z.string().min(1, {
message: "User id is required.",
}),
user_name: z.string().min(1, {
message: "User name is required.",
}),
});
export const NewBookForm = ({}) => {
const { toast } = useToast();
const router = useRouter();
const { user } = useUser();
//console.log(user);
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
name: "",
description: "",
src: "",
author: "",
user_id: user?.id ?? "Anomynous",
user_name: user?.firstName ?? "Anomynous",
},
});
const isLoading = form.formState.isSubmitting;
const onSubmit = async (values: z.infer<typeof formSchema>) => {
try {
//console.log(values);
const response = await fetch(
`${process.env.NEXT_PUBLIC_OUTERBASE_SECRET}/publishABook`,
{
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
user_id: values.user_id,
description: values.description,
author: values.author,
name: values.name,
user_name: values.user_name,
src: values.src,
}),
}
);
// //console.log(response);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error.description);
}
// await fetch(`${process.env.NEXT_PUBLIC_OUTERBASE_SECRET}/semdMail`, {
// method: "POST",
// headers: {
// "content-type": "application/json",
// },
// body: JSON.stringify({
// user_id: values.user_id,
// author: values.author,
// name: values.name,
// user_name: values.user_name,
// }),
// });
toast({
description: "Success.",
duration: 3000,
});
router.refresh();
router.push("/book");
} catch (error) {
toast({
variant: "destructive",
description: "Outerbase Error" || "Something went wrong.",
duration: 3000,
});
}
};
return (
<div className="h-full p-4 space-y-2 max-w-3xl mx-auto">
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-8 pb-10"
>
<div className="space-y-2 w-full col-span-2">
<div>
<h3 className="text-lg font-medium">General Information</h3>
<p className="text-sm text-muted-foreground">
Add a book to begin discussion
</p>
</div>
<Separator className="bg-primary/10" />
</div>
<FormField
name="src"
render={({ field }) => (
<FormItem className="flex flex-col items-center justify-center space-y-4 col-span-2">
<FormControl>
<ImageUpload
disabled={isLoading}
onChange={field.onChange}
value={field.value}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="grid grid-cols-1 gap-4">
<FormField
name="name"
control={form.control}
render={({ field }) => (
<FormItem className="col-span-2 md:col-span-1">
<FormLabel>Name</FormLabel>
<FormControl>
<Input
disabled={isLoading}
placeholder="The Wonderful Wizard of Oz"
{...field}
/>
</FormControl>
<FormDescription>Name of the book.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
name="author"
control={form.control}
render={({ field }) => (
<FormItem className="col-span-2 md:col-span-1">
<FormLabel>Author's name</FormLabel>
<FormControl>
<Input
disabled={isLoading}
placeholder="L. Frank Baum"
{...field}
/>
</FormControl>
<FormDescription>Author of the book.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
name="description"
control={form.control}
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea
disabled={isLoading}
placeholder="Brief description about the book"
{...field}
/>
</FormControl>
<FormDescription>
Short description for the Book
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="space-y-2 w-full">
<Separator className="bg-primary/10" />
</div>
<div className="w-full flex justify-center">
<Button size="lg" disabled={isLoading}>
Create a book
<Wand2 className="w-4 h-4 ml-2" />
</Button>
</div>
</form>
</Form>
</div>
);
};