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 {
|
|
|
|
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"
|
2024-06-13 19:52:44 +00:00
|
|
|
|
2024-07-01 15:23:48 +00:00
|
|
|
export const formSchema = z.object({
|
2024-06-29 15:21:56 +00:00
|
|
|
id: z.number().optional(),
|
2024-06-13 19:52:44 +00:00
|
|
|
title: z.string().min(2).max(50),
|
2024-06-17 10:41:21 +00:00
|
|
|
word_count: z.coerce.number().min(100),
|
2024-06-14 20:41:41 +00:00
|
|
|
genres: z.array(z.number())
|
2024-06-13 19:52:44 +00:00
|
|
|
})
|
|
|
|
|
2024-09-26 10:37:52 +00:00
|
|
|
export default function StoryForm({ genres, dbAction, className, closeDialog, defaults }: ComponentProps<"div"> & { genres: Array<Genre>, dbAction: (data: any) => Promise<{ success: string }>, className: string, closeDialog: () => void, defaults?: Story }) {
|
2024-06-13 19:52:44 +00:00
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
2024-09-26 10:37:52 +00:00
|
|
|
title: defaults?.title ?? "",
|
|
|
|
word_count: defaults?.word_count ?? 500,
|
2024-09-25 10:23:16 +00:00
|
|
|
genres: []
|
2024-06-13 19:52:44 +00:00
|
|
|
},
|
|
|
|
})
|
2024-09-26 10:37:52 +00:00
|
|
|
console.log("DEFAULTS: " + defaults)
|
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 {
|
2024-09-26 10:37:52 +00:00
|
|
|
const res = await dbAction(values)
|
2024-09-18 09:56:08 +00:00
|
|
|
//server actions return undefined if middleware authentication fails
|
2024-09-26 10:37:52 +00:00
|
|
|
if (!res.success) throw new Error("something went wrong")
|
|
|
|
toast({ title: "Success!", description: res.success })
|
2024-09-25 10:23:16 +00:00
|
|
|
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-13 19:52:44 +00:00
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
|
2024-06-13 19:52:44 +00:00
|
|
|
return (
|
2024-06-24 10:29:02 +00:00
|
|
|
<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">
|
2024-06-24 10:29:02 +00:00
|
|
|
<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} />
|
2024-06-24 10:29:02 +00:00
|
|
|
</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
|
|
|
|
2024-06-24 10:29:02 +00:00
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</div>
|
2024-06-13 19:52:44 +00:00
|
|
|
)
|
2024-06-11 17:14:30 +00:00
|
|
|
}
|