123 lines
3.0 KiB
TypeScript
123 lines
3.0 KiB
TypeScript
"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<Genre>, createStory: (data: any) => void, existingData: Story & { genres: number[] } | null }) {
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
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<typeof formSchema>) {
|
|
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: (
|
|
<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))
|
|
}
|
|
|
|
return (
|
|
<div className={className}>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8" id="storyform">
|
|
<FormField
|
|
control={form.control}
|
|
name="title"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Title</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder={randomStoryTitle()} {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 items-baseline max-w-full">
|
|
|
|
<GenrePicker
|
|
genres={genres}
|
|
form={form}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="word_count"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<FormLabel className="h-5">Word count</FormLabel>
|
|
<FormControl>
|
|
<Input className=" w-24" type="number" step={500} min={1} {...field}></Input>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
)
|
|
}
|