From 859fd0f72505a83ab585e96291a5b7ce351d91af Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Tue, 12 Nov 2024 16:36:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=89=87=E6=AE=B5=E7=BC=96=E8=BE=91=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BF=9D=E5=AD=98=E7=BC=96=E8=BE=91=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/actions/index.ts | 13 +++++++++++++ src/components/snippet-edit-form.tsx | 8 ++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/actions/index.ts diff --git a/src/actions/index.ts b/src/actions/index.ts new file mode 100644 index 0000000..88b5d62 --- /dev/null +++ b/src/actions/index.ts @@ -0,0 +1,13 @@ +"use server"; + +import { db } from "@/db"; +import { redirect } from "next/navigation"; + +export async function editSnippet(id: number, code: string) { + await db.snippet.update({ + where: { id }, + data: { code }, + }); + + redirect(`/snippets/${id}`); +} diff --git a/src/components/snippet-edit-form.tsx b/src/components/snippet-edit-form.tsx index 9e93a11..060c0cc 100644 --- a/src/components/snippet-edit-form.tsx +++ b/src/components/snippet-edit-form.tsx @@ -1,6 +1,7 @@ "use client"; import { useState } from "react"; +import * as actions from "@/actions"; import { Snippet } from "@prisma/client"; import { Editor } from "@monaco-editor/react"; @@ -15,6 +16,8 @@ export default function SnippetEditForm({ snippet }: SnippetEditFormProps) { setCode(value); }; + const editSnippetAction = actions.editSnippet.bind(null, snippet.id, code); + return (
+
+ +
); }