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

116 lines
2.8 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-09-25 16:48:03 +00:00
import { ComponentProps, SetStateAction } 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"
2024-09-25 10:23:16 +00:00
import { useRouter } from "next/navigation"
2024-09-25 16:48:03 +00:00
import { Ban, Cross } from "lucide-react"
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-09-25 16:48:03 +00:00
export default function StoryForm({ genres, createStory, className, closeDialog }: ComponentProps<"div"> & { genres: Array<Genre>, createStory: (data: any) => void, className: string, closeDialog: () => void }) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
2024-09-25 10:23:16 +00:00
word_count: 500,
genres: []
},
})
2024-06-29 15:21:56 +00:00
2024-09-25 10:23:16 +00:00
const router = useRouter()
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
2024-09-25 10:23:16 +00:00
if (!res) throw new Error("something went wrong")
toast({ title: "Sucessfully submitted:", description: values.title })
router.refresh()
2024-09-25 16:48:03 +00:00
closeDialog()
2024-09-18 09:56:08 +00:00
} catch (error) {
toast({
2024-09-25 10:23:16 +00:00
title: "Oh dear... ",
2024-09-18 09:56:08 +00:00
description: error.message
})
}
}
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({
2024-09-25 16:48:03 +00:00
description: (<Ban />)
2024-06-14 20:41:41 +00:00
})
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>
2024-09-25 16:48:03 +00:00
<Input className=" w-24" type="number" step={500} {...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
}