124 lines
3.0 KiB
TypeScript
124 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 { randomPublicationTitle } from "app/lib/shortStoryTitleGenerator"
|
|
import { ComponentProps } from "react"
|
|
import { Genre } from "@prisma/client"
|
|
import GenrePicker from "./genrePicker"
|
|
import { pubSchema } from "./schemas"
|
|
import { useRouter } from "next/navigation"
|
|
import { Ban } from "lucide-react"
|
|
|
|
export default function PubForm({ genres, createPub, className, closeDialog }: ComponentProps<"div"> & { genres: Array<Genre>, createPub: (data: any) => void, closeDialog: () => void }) {
|
|
const form = useForm<z.infer<typeof pubSchema>>({
|
|
resolver: zodResolver(pubSchema),
|
|
defaultValues: {
|
|
title: "",
|
|
link: "",
|
|
query_after_days: 30,
|
|
genres: []
|
|
},
|
|
})
|
|
|
|
const router = useRouter()
|
|
|
|
async function onSubmit(values: z.infer<typeof pubSchema>) {
|
|
try {
|
|
const res = await createPub(values)
|
|
if (!res) throw new Error("something went wrong")
|
|
toast({ title: "Successfully submitted:", description: values.title })
|
|
router.refresh()
|
|
closeDialog()
|
|
} catch (error) {
|
|
toast({
|
|
title: "Oh dear... ",
|
|
description: error.message
|
|
})
|
|
}
|
|
}
|
|
|
|
function onErrors(errors) {
|
|
toast({
|
|
description: (
|
|
<Ban />
|
|
),
|
|
})
|
|
console.log(JSON.stringify(errors))
|
|
}
|
|
|
|
const exampleTitle = randomPublicationTitle()
|
|
const exampleUrl = "www." +
|
|
exampleTitle.replace(/ /g, '')
|
|
.toLowerCase() +
|
|
".com"
|
|
return (
|
|
<div className={className}>
|
|
<Form {...form}>
|
|
<form id="pubform" onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
|
|
<FormField
|
|
control={form.control}
|
|
name="title"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Title</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder={exampleTitle} {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="link"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Website</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder={exampleUrl} {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
|
|
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 max-w-full h-fit">
|
|
<GenrePicker genres={genres} form={form} />
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="query_after_days"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-col">
|
|
<FormLabel className="h-5">Query after (days)</FormLabel>
|
|
<FormControl>
|
|
<Input className=" w-24" type="number" step={5} min={30} {...field}></Input>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</div>
|
|
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
|
|
)
|
|
}
|