"use client" import { z } from "zod" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { toast } from "@/components/ui/use-toast" import { ComponentProps } from "react" import { Genre, Story } from "@prisma/client" import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator" import GenrePicker from "./genrePicker" export const formSchema = z.object({ id: z.number().optional(), title: z.string().min(2).max(50), word_count: z.coerce.number().min(100), genres: z.array(z.number()) }) export default function StoryForm({ genres, createStory, className, existingData }: ComponentProps<"div"> & { genres: Array, createStory: (data: any) => void, existingData: Story & { genres: number[] } | null }) { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { id: existingData?.id, title: existingData?.title ?? "", word_count: existingData?.word_count ?? 500, genres: existingData?.genres ?? [] }, }) async function onSubmit(values: z.infer) { try { const res = await createStory(values) //server actions return undefined if middleware authentication fails if (res === undefined) throw new Error("something went wrong") toast({ title: "Successfully submitted:", description: values.title, }) } catch (error) { toast({ title: "UH-OH", description: error.message }) } window.location.reload() } function onErrors(errors) { toast({ title: "You have errors", description: (
					{JSON.stringify(errors, null, 2)}
				
), }) console.log(JSON.stringify(errors)) } return (
( Title )} />
( Word count )} />
) }