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 (