subman-nextjs/src/app/ui/forms/story.tsx

123 lines
3.0 KiB
TypeScript
Raw Normal View History

"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"
2024-06-12 15:15:22 +00:00
import { Input } from "@/components/ui/input"
2024-06-14 20:41:41 +00:00
import { toast } from "@/components/ui/use-toast"
2024-06-16 15:16:43 +00:00
2024-06-26 10:07:03 +00:00
import { ComponentProps } from "react"
2024-06-29 15:21:56 +00:00
import { Genre, Story } from "@prisma/client"
2024-06-24 16:29:51 +00:00
import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator"
2024-06-26 17:32:18 +00:00
import GenrePicker from "./genrePicker"
export const formSchema = z.object({
2024-06-29 15:21:56 +00:00
id: z.number().optional(),
title: z.string().min(2).max(50),
word_count: z.coerce.number().min(100),
2024-06-14 20:41:41 +00:00
genres: z.array(z.number())
})
2024-06-29 15:21:56 +00:00
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: {
2024-06-29 15:21:56 +00:00
id: existingData?.id,
title: existingData?.title ?? "",
word_count: existingData?.word_count ?? 500,
genres: existingData?.genres ?? []
},
})
2024-06-29 15:21:56 +00:00
2024-09-18 09:56:08 +00:00
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
})
}
2024-09-19 09:37:01 +00:00
window.location.reload()
}
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))
}
return (
<div className={className}>
<Form {...form}>
2024-06-26 17:32:18 +00:00
<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>
2024-06-24 16:29:51 +00:00
<Input placeholder={randomStoryTitle()} {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
2024-06-16 15:16:43 +00:00
2024-06-24 16:29:51 +00:00
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 items-baseline max-w-full">
2024-06-26 17:32:18 +00:00
<GenrePicker
genres={genres}
form={form}
2024-06-24 16:29:51 +00:00
/>
<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>
2024-06-24 16:29:51 +00:00
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
2024-06-24 16:50:16 +00:00
2024-06-16 15:16:43 +00:00
2024-06-14 20:41:41 +00:00
</form>
</Form>
</div>
)
2024-06-11 17:14:30 +00:00
}