2024-06-13 19:52:44 +00:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import { z } from "zod"
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
|
|
import { useForm } from "react-hook-form"
|
|
|
|
import { Genre } from "@prisma/client"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormDescription,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormLabel,
|
|
|
|
FormMessage,
|
|
|
|
} from "@/components/ui/form"
|
2024-06-12 15:15:22 +00:00
|
|
|
import { Input } from "@/components/ui/input"
|
2024-06-13 19:52:44 +00:00
|
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
2024-06-14 20:41:41 +00:00
|
|
|
import { toast } from "@/components/ui/use-toast"
|
2024-06-16 15:16:43 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
Popover,
|
|
|
|
PopoverContent,
|
|
|
|
PopoverTrigger,
|
|
|
|
} from "@/components/ui/popover"
|
|
|
|
import { cn } from "@/lib/utils"
|
|
|
|
import { Badge } from "lucide-react"
|
2024-06-16 21:08:17 +00:00
|
|
|
import GenresTrigger from "./genresTrigger"
|
|
|
|
import GenreCheckbox from "./genreCheckbox"
|
2024-06-17 08:39:17 +00:00
|
|
|
import GenrePicker from "./genrePicker"
|
|
|
|
import { useRef } from "react"
|
2024-06-13 19:52:44 +00:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
title: z.string().min(2).max(50),
|
2024-06-14 20:41:41 +00:00
|
|
|
word_count: z.coerce.number(),
|
|
|
|
genres: z.array(z.number())
|
2024-06-13 19:52:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default function StoryForm({ genres }) {
|
|
|
|
// 1. Define your form.
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
|
|
|
title: "",
|
|
|
|
word_count: 0,
|
2024-06-14 20:41:41 +00:00
|
|
|
genres: []
|
2024-06-13 19:52:44 +00:00
|
|
|
},
|
|
|
|
})
|
2024-06-17 08:39:17 +00:00
|
|
|
const ref = useRef(null)
|
2024-06-13 19:52:44 +00:00
|
|
|
// 2. Define a submit handler.
|
|
|
|
function onSubmit(values: z.infer<typeof formSchema>) {
|
|
|
|
// Do something with the form values.
|
|
|
|
// ✅ This will be type-safe and validated.
|
2024-06-14 20:41:41 +00:00
|
|
|
toast({
|
|
|
|
title: "You submitted the following values:",
|
|
|
|
description: (
|
|
|
|
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
|
|
|
<code className="text-white">{JSON.stringify(values, null, 2)}</code>
|
|
|
|
</pre>
|
|
|
|
),
|
|
|
|
})
|
2024-06-13 19:52:44 +00:00
|
|
|
console.log(values)
|
|
|
|
}
|
2024-06-14 20:41:41 +00:00
|
|
|
|
|
|
|
|
2024-06-16 15:16:43 +00:00
|
|
|
|
2024-06-14 20:41:41 +00:00
|
|
|
function onErrors(errors) {
|
|
|
|
toast({
|
|
|
|
title: "You have errors",
|
|
|
|
description: (
|
|
|
|
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
|
|
|
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
|
|
|
|
</pre>
|
|
|
|
),
|
|
|
|
})
|
|
|
|
console.log(JSON.stringify(errors))
|
|
|
|
}
|
|
|
|
|
2024-06-16 15:16:43 +00:00
|
|
|
|
2024-06-13 19:52:44 +00:00
|
|
|
return (
|
|
|
|
<Form {...form}>
|
2024-06-14 20:41:41 +00:00
|
|
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
2024-06-13 19:52:44 +00:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="title"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Title</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input placeholder="title goes here..." {...field} />
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
2024-06-16 15:16:43 +00:00
|
|
|
|
2024-06-13 19:52:44 +00:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="word_count"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Word count</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input type="number" step={500} min={0} {...field}></Input>
|
|
|
|
</FormControl>
|
2024-06-14 20:41:41 +00:00
|
|
|
<FormMessage />
|
2024-06-13 19:52:44 +00:00
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
2024-06-16 15:16:43 +00:00
|
|
|
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="genres"
|
|
|
|
render={({ field }) => (
|
2024-06-17 08:39:17 +00:00
|
|
|
<GenrePicker genres={genres} value={field.value} form={form} ref={ref} />
|
2024-06-16 15:16:43 +00:00
|
|
|
)}
|
|
|
|
/>
|
2024-06-14 20:41:41 +00:00
|
|
|
|
2024-06-13 19:52:44 +00:00
|
|
|
<Button type="submit">Submit</Button>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
)
|
2024-06-11 17:14:30 +00:00
|
|
|
}
|