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

159 lines
3.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 { Button } from "@/components/ui/button"
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 {
Popover,
PopoverContent,
} from "@/components/ui/popover"
import GenresTrigger from "./genresTrigger"
import GenreCheckbox from "./genreCheckbox"
2024-06-24 16:29:51 +00:00
import { randomPublicationTitle } from "app/lib/shortStoryTitleGenerator"
const formSchema = z.object({
title: z.string().min(2).max(50),
link: z.string(),
query_after_days: z.number().min(30),
genres: z.array(z.number()),
})
2024-06-17 21:23:09 +00:00
export default function PubForm({ genres, createPub }) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
title: "",
link: "",
query_after_days: 30,
genres: []
},
})
function onSubmit(values: z.infer<typeof formSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(values, null, 2)}</code>
</pre>
),
})
2024-06-17 21:23:09 +00:00
createPub(values)
console.log(values)
}
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))
}
2024-06-24 16:29:51 +00:00
const exampleTitle = randomPublicationTitle()
const exampleUrl = "www." +
exampleTitle.replace(/ /g, '')
.toLowerCase() +
".com"
return (
<Form {...form}>
2024-06-17 11:12:55 +00:00
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>Title</FormLabel>
<FormControl>
2024-06-24 16:29:51 +00:00
<Input placeholder={exampleTitle} {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="link"
render={({ field }) => (
<FormItem>
<FormLabel>Website</FormLabel>
<FormControl>
2024-06-24 16:29:51 +00:00
<Input placeholder={exampleUrl} {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
2024-06-24 16:29:51 +00:00
<div className="inline-flex flex-wrap w-full gap-x-16 gap-y-8 max-w-full h-fit">
<FormField
control={form.control}
name="genres"
render={({ field }) => (
<FormItem className="flex flex-col flex-wrap">
<FormLabel className="h-5">Genres</FormLabel>
<Popover>
<GenresTrigger value={field.value} genres={genres} />
<PopoverContent align="start">
{genres.map((item) => (
<FormField
key={item.id}
control={form.control}
name="genres"
render={({ field }) => {
return (
<GenreCheckbox field={field} item={item} />
)
}}
/>
))}
</PopoverContent>
</Popover>
<FormMessage />
</FormItem>
)}
/>
2024-06-24 16:29:51 +00:00
<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>
<Button type="submit">Submit</Button>
</form>
</Form>
)
2024-06-12 09:00:59 +00:00
}