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