diff --git a/tone-page-web/app/console/(with-menu)/web/blog/components/BlogEdit.tsx b/tone-page-web/app/console/(with-menu)/web/blog/components/BlogEdit.tsx new file mode 100644 index 0000000..7c8f922 --- /dev/null +++ b/tone-page-web/app/console/(with-menu)/web/blog/components/BlogEdit.tsx @@ -0,0 +1,146 @@ +"use client" + +import React, { use, useState } from "react" +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog" +import { Input } from "@/components/ui/input" +import { Label } from "@/components/ui/label" +import { Button } from "@/components/ui/button" +import { toast } from "sonner" +import { Textarea } from "@/components/ui/textarea" +import { Plus } from "lucide-react" +import { Resource } from "@/lib/types/resource" +import { AdminApi } from "@/lib/api" +import useSWR from "swr" +import { ApiError } from "next/dist/server/api-utils" +import { Skeleton } from "@/components/ui/skeleton" +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog" + + +interface BlogEditProps { + id: string; + children?: React.ReactNode; + onRefresh: () => void; +} + +export default function BlogEdit({ id, children, onRefresh }: BlogEditProps) { + const [open, setOpen] = useState(false) + const { data: blog, error, isLoading, mutate } = useSWR( + open ? `/api/admin/web/blog/${id}` : null, + () => AdminApi.web.blog.get(id), + { + revalidateOnFocus: false, + revalidateOnReconnect: false, + revalidateIfStale: false, + dedupingInterval: 5000, + } + ) + + const handleSubmit = async () => { + if (!blog) return; + try { + await AdminApi.web.blog.update(id, { + title: blog.title, + description: blog.description, + contentUrl: blog.contentUrl, + }); + toast.success("更新成功") + setOpen(false); + onRefresh(); + } catch (error) { + toast.error((error as ApiError).message || "更新失败") + } + } + + const handleDelete = async () => { + try { + await AdminApi.web.blog.remove(id); + toast.success("删除成功") + setOpen(false); + onRefresh(); + } catch (error) { + toast.error((error as ApiError).message || "删除失败") + } + } + + return ( + + + {children} + + + + 添加博客 + + { + blog && ( + <> + + + + 标题 + + mutate({ ...blog, title: e.target.value }, false)} + /> + + + + 描述 + + mutate({ ...blog, description: e.target.value }, false)} + /> + + + + 文章URL + + mutate({ ...blog, contentUrl: e.target.value }, false)} + /> + + + + + + 删除 + + + setOpen(false)}>取消 + 保存 + + + + > + ) + } + + + ) +} \ No newline at end of file diff --git a/tone-page-web/app/console/(with-menu)/web/blog/components/BlogTable.tsx b/tone-page-web/app/console/(with-menu)/web/blog/components/BlogTable.tsx index 7e58f66..8b8af4f 100644 --- a/tone-page-web/app/console/(with-menu)/web/blog/components/BlogTable.tsx +++ b/tone-page-web/app/console/(with-menu)/web/blog/components/BlogTable.tsx @@ -10,11 +10,13 @@ import { } from "@/components/ui/table" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip" import { Blog } from "@/lib/types/blog" +import BlogEdit from "./BlogEdit" +import { Button } from "@/components/ui/button" interface BlogTableProps { blogs: Blog[], error?: string, - onRefresh?: () => void, + onRefresh: () => void, } export default function BlogTable({ blogs, error, onRefresh }: BlogTableProps) { @@ -53,9 +55,9 @@ export default function BlogTable({ blogs, error, onRefresh }: BlogTableProps) { {blog.description} {blog.contentUrl} - {/* onRefresh()}> + onRefresh()}> 编辑 - */} + ))} diff --git a/tone-page-web/lib/api/admin/web/blog/get.ts b/tone-page-web/lib/api/admin/web/blog/get.ts new file mode 100644 index 0000000..a1bdebf --- /dev/null +++ b/tone-page-web/lib/api/admin/web/blog/get.ts @@ -0,0 +1,6 @@ +import fetcher from "@/lib/api/fetcher"; +import { Blog } from "@/lib/types/blog"; + +export async function get(id: string) { + return fetcher(`/api/admin/web/blog/${id}`) +} \ No newline at end of file diff --git a/tone-page-web/lib/api/admin/web/blog/index.ts b/tone-page-web/lib/api/admin/web/blog/index.ts index 4a80906..1dcb4de 100644 --- a/tone-page-web/lib/api/admin/web/blog/index.ts +++ b/tone-page-web/lib/api/admin/web/blog/index.ts @@ -1,4 +1,5 @@ export * from './create'; export * from './remove'; export * from './list'; -export * from './update'; \ No newline at end of file +export * from './update'; +export * from './get'; \ No newline at end of file diff --git a/tone-page-web/lib/api/admin/web/blog/update.ts b/tone-page-web/lib/api/admin/web/blog/update.ts index e31feaf..25ec5f9 100644 --- a/tone-page-web/lib/api/admin/web/blog/update.ts +++ b/tone-page-web/lib/api/admin/web/blog/update.ts @@ -1,12 +1,14 @@ import fetcher from "@/lib/api/fetcher"; type UpdateBlogParams = { - + title: string; + description: string; + contentUrl: string; } export async function update(id: string, data: UpdateBlogParams) { return fetcher(`/api/admin/web/blog/${id}`, { - method: 'POST', + method: 'PUT', body: JSON.stringify(data) }) } \ No newline at end of file