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

265 lines
7.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"
2024-06-14 09:42:31 +00:00
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import { Calendar } from "@/components/ui/calendar"
import { CalendarIcon } from "@radix-ui/react-icons"
import { cn } from "@/lib/utils"
import { format } from "date-fns"
import {
Form,
FormItem,
FormLabel,
FormField,
FormControl,
FormDescription,
FormMessage
} from "@/components/ui/form"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
2024-06-14 20:41:41 +00:00
import { useState } from "react"
2024-07-24 20:41:46 +00:00
import { createSub } from "app/lib/create"
2024-09-19 09:37:01 +00:00
import { subSchema } from "./schemas"
2024-09-25 10:23:16 +00:00
import { useRouter } from "next/navigation"
2024-09-25 16:48:03 +00:00
import { Ban } from "lucide-react"
2024-09-19 09:37:01 +00:00
export type SubForm = z.infer<typeof subSchema>
export default function SubmissionForm({ stories, pubs, responses, defaults, closeDialog }: { stories: any, pubs: any, responses: any, defaults?: any, closeDialog: () => void }) {
2024-09-19 09:37:01 +00:00
const form = useForm<z.infer<typeof subSchema>>({
resolver: zodResolver(subSchema),
2024-06-14 20:41:41 +00:00
defaultValues: {
responseId: responses[0].id,
...defaults
2024-06-14 20:41:41 +00:00
}
2024-06-14 09:25:18 +00:00
})
2024-06-14 20:41:41 +00:00
const [isSubCalendarOpen, setIsSubCalendarOpen] = useState(false);
const [isRespCalendarOpen, setIsRespCalendarOpen] = useState(false);
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-09-25 10:23:16 +00:00
const router = useRouter()
2024-06-14 09:25:18 +00:00
2024-09-19 09:37:01 +00:00
async function onSubmit(values: z.infer<typeof subSchema>) {
try {
const res = await createSub(values)
2024-09-25 16:48:03 +00:00
if (!res) throw new Error("something went wrong")
2024-09-19 09:37:01 +00:00
toast({ title: "Successfully created new submission!" })
2024-09-25 16:48:03 +00:00
router.refresh()
closeDialog()
2024-09-19 09:37:01 +00:00
} catch (error) {
toast({
title: "UH-OH",
description: error.message
})
}
}
2024-06-14 09:25:18 +00:00
function onErrors(errors) {
toast({
description: (
2024-09-25 16:48:03 +00:00
<Ban />
2024-06-14 09:25:18 +00:00
),
})
console.log(JSON.stringify(errors))
}
return (
<Form {...form}>
<form id="subform" onSubmit={form.handleSubmit(onSubmit, onErrors)} className="space-y-2 md:space-y-8 text-xs">
<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>
<FormDescription className="text-xs md:text-base">The piece you submitted</FormDescription>
2024-06-14 09:25:18 +00:00
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="pubId"
render={({ field }) => (
<FormItem>
<FormLabel className="text-sm md:text-base">Publication</FormLabel>
2024-06-14 09:25:18 +00:00
<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 className="text-xs md:text-base">The market you sent it to</FormDescription>
<FormMessage />
</FormItem>
)}
/>
2024-06-14 09:42:31 +00:00
<FormField
control={form.control}
name="submitted"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="text-sm md:text-base">Date of submission</FormLabel>
<Popover modal={true} open={isSubCalendarOpen} onOpenChange={setIsSubCalendarOpen}>
2024-06-14 09:42:31 +00:00
<PopoverTrigger asChild>
<FormControl>
<Button
variant={"outline"}
className={cn(
"w-[240px] pl-3 text-left font-normal",
!field.value && "text-muted-foreground"
)}
>
{field.value ? (
format(field.value, "PPP")
) : (
<span>Pick a date</span>
)}
<CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={field.value}
2024-06-14 20:41:41 +00:00
onSelect={(e) => { field.onChange(e); setIsSubCalendarOpen(false); }}
2024-06-14 09:42:31 +00:00
disabled={(date) =>
date > new Date() || date < new Date("1900-01-01")
}
initialFocus
/>
</PopoverContent>
</Popover>
<FormDescription className="text-xs md:text-base">
2024-06-14 09:42:31 +00:00
The date you sent it
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
2024-06-14 20:41:41 +00:00
<FormField
control={form.control}
name="responded"
render={({ field }) => (
<FormItem className="flex flex-col">
<FormLabel className="text-sm md:text-base">Date of response</FormLabel>
<Popover modal={true} open={isRespCalendarOpen} onOpenChange={setIsRespCalendarOpen}>
2024-06-14 20:41:41 +00:00
<PopoverTrigger asChild>
<FormControl>
<Button
variant={"outline"}
className={cn(
"w-[240px] pl-3 text-left font-normal",
!field.value && "text-muted-foreground"
)}
>
{field.value ? (
format(field.value, "PPP")
) : (
<span>Pick a date</span>
)}
<CalendarIcon className="ml-auto h-4 w-4 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<Calendar
mode="single"
selected={field.value}
onSelect={(e) => { field.onChange(e); setIsRespCalendarOpen(false); }}
disabled={(date) =>
date > new Date() || date < new Date("1900-01-01")
}
initialFocus
/>
</PopoverContent>
</Popover>
<FormDescription className="text-xs md:text-base">
2024-06-14 20:41:41 +00:00
The date they wrote back
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
2024-06-14 09:42:31 +00:00
2024-06-14 20:41:41 +00:00
<FormField
control={form.control}
name="responseId"
render={({ field }) => (
<FormItem>
<FormLabel className="text-sm md:text-base">Response</FormLabel>
2024-06-14 20:41:41 +00:00
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue>
<p>{responses?.find(e => e.id === Number(field.value))?.response ?? null}</p>
</SelectValue>
</SelectTrigger>
</FormControl>
<SelectContent>
2024-06-14 20:41:41 +00:00
{reponsesSelectItems}
</SelectContent>
</Select>
<FormDescription className="text-xs md:text-base">The market you sent it to</FormDescription>
2024-06-14 20:41:41 +00:00
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
)
2024-06-12 09:00:59 +00:00
}