'use client'; import * as React from "react" import { cn } from "@/lib/utils" import { Button } from "@/components/ui/button" import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, } from "@/components/ui/drawer" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { useUser } from "@/hooks/admin/user/use-user"; import { User } from "@/lib/types/user"; import { Skeleton } from "@/components/ui/skeleton"; import { updateUser } from "@/lib/api/admin/user"; import { AdminApi } from "@/lib/api"; import { toast } from "sonner"; import { Alert, AlertDescription, AlertTitle, } from "@/components/ui/alert" import { AlertCircle } from "lucide-react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from "@/components/ui/alert-dialog"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; export function UserInfoEditor({ onClose, onUserUpdate, onUserDelete, userId, }: { onClose: () => void, onUserUpdate: (user: User) => void, onUserDelete: (userId: string) => void, userId: string }) { const { user, isLoading, error } = useUser(userId); const [saveLoading, setSaveLoading] = React.useState(false); const handleSave = async (user: updateUser) => { try { setSaveLoading(true); const res = await AdminApi.user.update(userId, user); if (res) { toast.success("保存成功"); onUserUpdate(res); } else { throw new Error(); } } catch (error) { toast.error((error as Error).message || "保存失败"); } finally { setSaveLoading(false); } } const [removeLoading, setRemoveLoading] = React.useState(false); const handleRemove = async (userId: string) => { try { setRemoveLoading(true); await AdminApi.user.remove(userId); toast.success("删除成功"); onUserDelete(userId); onClose(); } catch (error) { toast.error((error as Error).message || "删除失败"); } finally { setRemoveLoading(false); } } const [setPasswordLoading, setSetPasswordLoading] = React.useState(false); const handleSetPassword = async (userId: string, password: string) => { try { setSetPasswordLoading(true); await AdminApi.user.setPassword(userId, password); toast.success("密码修改成功"); } catch (error) { toast.error((error as Error).message || "密码修改失败"); } finally { setSetPasswordLoading(false); } } return ( 编辑用户信息 确保你在保存之前检查所有更改 {user && { e.preventDefault() const formData = new FormData(e.currentTarget); handleSave({ username: formData.get("username")?.toString()!, nickname: formData.get("nickname")?.toString()!, email: formData.get("email")?.toString() || null, phone: formData.get("phone")?.toString() || null, }) }} />} {isLoading && [...Array(5)].map((_, i) => ( )) } { error && ( 出错啦! {error.message} ) } ) } function ProfileForm({ className, user, onSetPassword, onRemove, ...props }: React.ComponentProps<"form"> & { user: User, onSetPassword: (userId: string, password: string) => Promise, onRemove: (userId: string) => Promise, }) { const [newPassword, setNewPassword] = React.useState(""); return (
修改密码 新密码长度在6-32位之间,且至少包含一个字母和一个数字,可以包含特殊字符
setNewPassword(e.target.value)} className="col-span-3" />
是否要删除该用户? 该操作无法撤销,这会永久删除该用户的所有数据 取消 onRemove(user.userId)}>删除
) }