"use client" import React, { 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 { AdminApi } from "@/lib/api" import useSWR from "swr" import { ApiError } from "next/dist/server/api-utils" 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, 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)} />
mutate({ ...blog, contentUrl: e.target.value }, false)} />
) }
) }