From 3f22b2ce82730e97a7ed2b54dffc9a36ec642d00 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 20 Sep 2024 15:46:26 +0200 Subject: [PATCH] update sub server actions etc --- prisma/dev.db | Bin 69632 -> 69632 bytes src/app/lib/update.ts | 27 +++- src/app/submission/edit.tsx | 3 +- src/app/submission/page.tsx | 2 +- src/app/ui/forms/editSub.tsx | 265 +++++++++++++++++++++++++++++++++++ 5 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 src/app/ui/forms/editSub.tsx diff --git a/prisma/dev.db b/prisma/dev.db index 8207543e5a866f41705483f6c27a23dc0aa7ee18..a38ac4143ce1f4ba29ebb3d7fa8bd6b03f09671d 100644 GIT binary patch delta 183 zcmZozz|ydQWr8$g%|sbz#+r=@8UBn)n@{@73ovfk{4!p8fdm)(F=oy<{z5Jrjtg9! z9L*eN>@PXr^Q&x?VO-75#4WM?Bs=3&M&3pqaZUzC#zx*oUPk%pj694@+uw3BYBRDh zFtG4Uci>|5o}SLl*bCwI@iF#pZ{=Z}&m`up!~xdEz$nWfl2NJR)*rF^O delta 187 zcmZozz|ydQWr8$g=|mZ4#?p-m8UBoln@{@73ouUG{4!p8fdnV}F-Gn$+^JkP92dAc zIhr}l*k5wK=a<+j!?>EAi8F5dNp{Amj1t@u3= { + "use server" + try { + subSchema.parse(data) + const res = await prisma.sub.update({ where: { id: data.id }, data }) + revalidatePath("/submission") + return res + } catch (error) { + console.error(error) + return undefined + } + +} + + + + + + + + diff --git a/src/app/submission/edit.tsx b/src/app/submission/edit.tsx index 2ae4c96..0d951b0 100644 --- a/src/app/submission/edit.tsx +++ b/src/app/submission/edit.tsx @@ -5,6 +5,7 @@ import { Button } from "@/components/ui/button"; import { ComponentProps } from "react"; import { Pub, Response, Story } from "@prisma/client"; import SubmissionForm, { SubForm } from "app/ui/forms/sub"; +import EditSubmissionForm from "app/ui/forms/editSub"; @@ -16,7 +17,7 @@ export default function EditSubmissionDialog({ stories, pubs, responses, default Edit Submission Change response status, edit dates etc - + diff --git a/src/app/submission/page.tsx b/src/app/submission/page.tsx index 12ba067..d43977f 100644 --- a/src/app/submission/page.tsx +++ b/src/app/submission/page.tsx @@ -3,7 +3,6 @@ import { DataTable } from "app/ui/tables/data-table" import { columns } from "./columns" import { Pub, Response, Story, Sub } from "@prisma/client" import CreateSubmissionDialog from "./create" -import { Trash2 } from "lucide-react" export type SubComplete = Sub & { pub: Pub, @@ -11,6 +10,7 @@ export type SubComplete = Sub & { response: Response } export default async function Page() { + const subs: Array = await getSubsComplete() const stories = await getStories() const pubs = await getPubs() diff --git a/src/app/ui/forms/editSub.tsx b/src/app/ui/forms/editSub.tsx new file mode 100644 index 0000000..4c1df4c --- /dev/null +++ b/src/app/ui/forms/editSub.tsx @@ -0,0 +1,265 @@ +"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 { toast } from "@/components/ui/use-toast" +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" +import { useState } from "react" +import { createSub } from "app/lib/create" +import { subSchema } from "./schemas" +import { updateSub } from "app/lib/update" + +export type SubForm = z.infer + + +export default function EditSubmissionForm({ stories, pubs, responses, defaults }) { + const form = useForm>({ + resolver: zodResolver(subSchema), + defaultValues: { + responseId: responses[0].id, + ...defaults + } + }) + const [isSubCalendarOpen, setIsSubCalendarOpen] = useState(false); + const [isRespCalendarOpen, setIsRespCalendarOpen] = useState(false); + const storiesSelectItems = stories.map(e => ( + + {e.title} + + )) + const pubsSelectItems = pubs.map(e => ( + + {e.title} + + )) + + const reponsesSelectItems = responses.map(e => ( + + {e.response} + + )) + + + // 2. Define a submit handler. + async function onSubmit(values: z.infer) { + try { + const res = await updateSub(values) + if (res === undefined) throw new Error("something went wrong") + toast({ title: "Successfully created new submission!" }) + window.location.reload() + } catch (error) { + toast({ + title: "UH-OH", + description: error.message + }) + } + } + + function onErrors(errors) { + toast({ + title: "You have errors", + description: ( +
+					{JSON.stringify(errors, null, 2)}
+				
+ ), + }) + console.log(JSON.stringify(errors)) + } + + return ( +
+ + ( + + Story + + The piece you submitted + + + )} + /> + ( + + Publication + + The market you sent it to + + + )} + /> + + ( + + Date of submission + + + + + + + + { field.onChange(e); setIsSubCalendarOpen(false); }} + disabled={(date) => + date > new Date() || date < new Date("1900-01-01") + } + initialFocus + /> + + + + The date you sent it + + + + )} + /> + + ( + + Date of response + + + + + + + + { field.onChange(e); setIsRespCalendarOpen(false); }} + disabled={(date) => + date > new Date() || date < new Date("1900-01-01") + } + initialFocus + /> + + + + The date they wrote back + + + + )} + /> + + + ( + + Response + + The market you sent it to + + + )} + /> + + + + ) +}