feat: 添加删除代码片段功能,支持在代码片段页面删除指定代码片段

This commit is contained in:
ngc2207 2024-11-12 16:42:47 +08:00
parent 859fd0f725
commit 3474cb11af
2 changed files with 12 additions and 1 deletions

View File

@ -11,3 +11,9 @@ export async function editSnippet(id: number, code: string) {
redirect(`/snippets/${id}`);
}
export async function deleteSnippet(id: number) {
await db.snippet.delete({ where: { id } });
redirect("/");
}

View File

@ -1,5 +1,6 @@
import { db } from "@/db";
import Link from "next/link";
import * as actions from "@/actions";
import { notFound } from "next/navigation";
interface SnippetShowPageProps {
@ -22,6 +23,8 @@ export default async function SnippetShowPage(props: SnippetShowPageProps) {
return notFound();
}
const deleteSnippetAction = actions.deleteSnippet.bind(null, snippet.id);
return (
<div>
<div className="flex m-4 justify-between items-center">
@ -33,7 +36,9 @@ export default async function SnippetShowPage(props: SnippetShowPageProps) {
>
Edit
</Link>
<button className="p-2 border rounded">Delete</button>
<form action={deleteSnippetAction}>
<button className="p-2 border rounded">Delete</button>
</form>
</div>
</div>
<pre className="p-3 border rounded bg-gray-200 border-gray-200">