From 6978d3483b0dfc16d7767364694ebb3ad500faf4 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Mon, 11 Nov 2024 11:27:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8server=20action?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=88=9B=E5=BB=BA=E4=BB=A3=E7=A0=81=E7=89=87?= =?UTF-8?q?=E6=AE=B5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/snippets/new/page.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/app/snippets/new/page.tsx b/src/app/snippets/new/page.tsx index 0a244be..2659bb2 100644 --- a/src/app/snippets/new/page.tsx +++ b/src/app/snippets/new/page.tsx @@ -1,6 +1,26 @@ +import { db } from "@/db"; +import { redirect } from "next/navigation"; + export default function SnippetCreatePage() { + async function createSnippet(formData: FormData) { + // This needs to be a server action! + "use server"; + // Check the user's inputs and make sure they're valid + const title = formData.get("title") as string; + const code = formData.get("code") as string; + // Create a new record in the database + const snippet = await db.snippet.create({ + data: { + title: title, + code: code, + }, + }); + console.log(snippet); + // Redirect the user back to the root route + redirect("/"); + } return ( -
+

Create a Snippet