'use client' import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { AdminApi } from "@/lib/api"; import { BlogPermission } from "@/lib/types/Blog.Permission.enum"; import { useState } from "react"; import { toast } from "sonner"; interface AddBlogProps { children: React.ReactNode; onRefresh: () => void; } const blogPermissions = [ { permission: BlogPermission.Public, localText: '公开可读', }, { permission: BlogPermission.ByPassword, localText: '受密码保护', }, { permission: BlogPermission.List, localText: '显示在列表中', }, ] as const; export default function AddBlog({ children, onRefresh }: AddBlogProps) { const [open, setOpen] = useState(false); const [blog, setBlog] = useState({ title: "", description: "", contentUrl: "", permissions: [] as BlogPermission[], password: "", }); const handleSubmit = async () => { try { const res = await AdminApi.web.blog.create({ ...blog, }); if (res) { setOpen(false); onRefresh(); toast.success("添加成功"); setBlog({ title: '', description: '', contentUrl: '', permissions: [], password: '', }) } else { throw new Error(); } } catch (error) { toast.error((error as Error).message || "添加失败"); } } return ( {children} 添加博客
setBlog({ ...blog, title: e.target.value })} />
setBlog({ ...blog, description: e.target.value })} />
setBlog({ ...blog, contentUrl: e.target.value })} />
{ blogPermissions.map((v, i) => (
{ setBlog({ ...blog, permissions: newChecked ? [...blog.permissions, v.permission] : [...blog.permissions].filter(p => p !== v.permission), }) }} />
)) }
{ blog.permissions.includes(BlogPermission.ByPassword) &&
setBlog({ ...blog, password: e.target.value })} />
}
) }