From 134cfa136ab1f7916edd8abb3c23363e5950892a Mon Sep 17 00:00:00 2001 From: andrzej Date: Sat, 29 Jun 2024 17:21:56 +0200 Subject: [PATCH] begin implementation of edit feature --- prisma/dev.db | Bin 69632 -> 69632 bytes src/app/lib/create.ts | 6 +++--- src/app/lib/update.ts | 22 ++++++++++++++++++++++ src/app/story/create.tsx | 2 +- src/app/story/page.tsx | 1 + src/app/ui/forms/story.tsx | 20 +++++++++++--------- src/app/ui/tables/contextMenu.tsx | 9 ++++++--- 7 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 src/app/lib/update.ts diff --git a/prisma/dev.db b/prisma/dev.db index 4dcac1831a483bfc423097af8050bd9c1fb41b86..4524a6696e345bf11f8c83185d382e15227b8208 100644 GIT binary patch delta 260 zcmZozz|ydQWr8&0jfpbOj5jtWH2O2DZGPl0Zy?XWz`)6Gz{LNA|2+R>{!;!hen)-- z{=@uR`M!hTW@$ o0<$3hjLGa5)6N@J%e5e6HV^jsGVD zAOGCR>=WczJNbcdv(|)Eem*6jFfSt$h+<<@nml)Yj6Tp9c19LvF0h7v2L8|d_xRWI zzu~{ezm0!6|8)Ly{PX$y`H%5G<=+Q_n>7~r^NR}uRf{t+D+vK;HD uG6iNn{#BFNFUT>^;GeNs<3a(yApdU$A)wOV3_L)EzZuw+Hm|%NZU6v;>QB-D diff --git a/src/app/lib/create.ts b/src/app/lib/create.ts index cf41f49..1d1e3ec 100644 --- a/src/app/lib/create.ts +++ b/src/app/lib/create.ts @@ -1,12 +1,12 @@ "use server" -import { Genre } from "@prisma/client" +import { Genre, Story } from "@prisma/client" import prisma from "./db" import { revalidatePath } from "next/cache" import { redirect } from "next/navigation" -export async function createStory(data) { +export async function createStory(data: Story & { genres: number[] }) { "use server" - const genresArray = data.genres.map((e: Genre) => { return { id: e } }) + const genresArray = data.genres.map((e) => { return { id: e } }) const res = await prisma.story.create({ data: { title: data.title, diff --git a/src/app/lib/update.ts b/src/app/lib/update.ts new file mode 100644 index 0000000..0fc3d1f --- /dev/null +++ b/src/app/lib/update.ts @@ -0,0 +1,22 @@ +import { Genre, Story } from "@prisma/client" +import { StoryWithGenres } from "app/story/page" +import prisma from "./db" +import { revalidatePath } from "next/cache" +import { redirect } from "next/navigation" + +export async function updateStory(data: Story & { genres: number[] }) { + "use server" + const genresArray = data.genres.map((e) => { return { id: e } }) + + const res = await prisma.story.update({ + where: { id: data.id }, + data: { + title: data.title, + word_count: data.word_count, + genres: { set: genresArray } + } + }) + console.log(`updated story: ${res}`) + revalidatePath("/story") + redirect("/story") +} diff --git a/src/app/story/create.tsx b/src/app/story/create.tsx index d61a499..912ab0a 100644 --- a/src/app/story/create.tsx +++ b/src/app/story/create.tsx @@ -19,7 +19,7 @@ export default function CreateStoryDialog({ genres }: ComponentProps<"div"> & { New story Create an entry for a new story i.e. a thing you intend to submit for publication. - + diff --git a/src/app/story/page.tsx b/src/app/story/page.tsx index 4d94658..a75734b 100644 --- a/src/app/story/page.tsx +++ b/src/app/story/page.tsx @@ -23,6 +23,7 @@ export default async function Page() {
+ {/* TODO - EDIT STORY DIALOG */}
) diff --git a/src/app/ui/forms/story.tsx b/src/app/ui/forms/story.tsx index 60e04cd..068731a 100644 --- a/src/app/ui/forms/story.tsx +++ b/src/app/ui/forms/story.tsx @@ -21,31 +21,33 @@ import { PopoverContent, } from "@/components/ui/popover" import { ComponentProps } from "react" -import { Genre } from "@prisma/client" +import { Genre, Story } from "@prisma/client" import { randomStoryTitle } from "app/lib/shortStoryTitleGenerator" import { usePathname } from "next/navigation" import GenrePicker from "./genrePicker" +import { StoryWithGenres } from "app/story/page" const formSchema = z.object({ + id: z.number().optional(), title: z.string().min(2).max(50), word_count: z.coerce.number().min(100), genres: z.array(z.number()) }) -export default function StoryForm({ genres, createStory, className }: ComponentProps<"div"> & { genres: Array, createStory: (data: any) => void }) { - // 1. Define your form. +export default function StoryForm({ genres, createStory, className, existingData }: ComponentProps<"div"> & { genres: Array, createStory: (data: any) => void, existingData: Story & { genres: number[] } | null }) { const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { - title: "", - word_count: 0, - genres: [] + id: existingData?.id, + title: existingData?.title ?? "", + word_count: existingData?.word_count ?? 500, + genres: existingData?.genres ?? [] }, }) - // 2. Define a submit handler. + + + function onSubmit(values: z.infer) { - // Do something with the form values. - // ✅ This will be type-safe and validated. toast({ title: "You submitted the following values:", description: ( diff --git a/src/app/ui/tables/contextMenu.tsx b/src/app/ui/tables/contextMenu.tsx index 95c9477..c237519 100644 --- a/src/app/ui/tables/contextMenu.tsx +++ b/src/app/ui/tables/contextMenu.tsx @@ -13,9 +13,12 @@ export default function FormContextMenu({ pathname, row, selectedRows, deselect {pathname !== "/submission" && selectedRows.length <= 1 ? - - Inspect - + <> + + Inspect + + Edit + : "" } {