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

140 lines
3.4 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"
2024-06-14 09:25:18 +00:00
import { toast } from "@/components/ui/use-toast"
import {
Form,
FormItem,
FormLabel,
FormField,
FormControl,
FormDescription,
FormMessage
} from "@/components/ui/form"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { ItemText, SelectItemIndicator, SelectItemText } from "@radix-ui/react-select"
2024-06-14 09:25:18 +00:00
const FormSchema = z.object({
storyId: z.string(),
pubId: z.string(),
// submitted: z.date(),
// responded: z.date(),
// responseId: z.string()
})
export default function SubmissionForm({ stories, pubs, responses }) {
2024-06-14 09:25:18 +00:00
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
})
const storiesSelectItems = stories.map(e => (
2024-06-14 09:25:18 +00:00
<SelectItem value={e.id.toString()} key={e.title}>
{e.title}
</SelectItem>
))
const pubsSelectItems = pubs.map(e => (
<SelectItem value={e.id}>
{e.title}
</SelectItem>
))
const reponsesSelectItems = responses.map(e => (
<SelectItem value={e.id}>
{e.response}
</SelectItem>
))
2024-06-14 09:25:18 +00:00
// 2. Define a submit handler.
2024-06-14 09:25:18 +00:00
function onSubmit(values: z.infer<typeof FormSchema>) {
// Do something with the form values.
// ✅ This will be type-safe and validated.
2024-06-14 09:25:18 +00:00
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>
),
})
console.log(values)
}
2024-06-14 09:25:18 +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 (
<Form {...form}>
2024-06-14 09:25:18 +00:00
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-8">
<FormField
control={form.control}
name="storyId"
render={({ field }) => (
<FormItem>
<FormLabel>Story</FormLabel>
2024-06-14 09:25:18 +00:00
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
2024-06-14 09:25:18 +00:00
<SelectValue placeholder="Select something">
<p>{stories?.find(e => e.id === Number(field.value))?.title ?? null}</p>
</SelectValue>
</SelectTrigger>
</FormControl>
<SelectContent>
{storiesSelectItems}
</SelectContent>
</Select>
2024-06-14 09:25:18 +00:00
<FormDescription>The piece you submitted</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="pubId"
render={({ field }) => (
<FormItem>
<FormLabel>Publication</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select something">
<p>{pubs?.find(e => e.id === Number(field.value))?.title ?? null}</p>
</SelectValue>
</SelectTrigger>
</FormControl>
<SelectContent>
{pubsSelectItems}
</SelectContent>
</Select>
<FormDescription>The market you sent it to</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
)
2024-06-12 09:00:59 +00:00
}