优化后端,实现前端添加用户

This commit is contained in:
2025-05-12 13:29:29 +08:00
parent c17108e094
commit 17bcb8787a
6 changed files with 141 additions and 8 deletions

View File

@@ -0,0 +1,88 @@
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
import { useState } from "react";
import { AdminApi } from "@/lib/api";
import { toast } from "sonner";
import { ApiError } from "next/dist/server/api-utils";
interface CreateUserEditorProps {
children: React.ReactNode
}
export function CreateUserEditor({ children }: CreateUserEditorProps) {
const [open, setOpen] = useState(false);
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
try {
await AdminApi.user.create({
username: formData.get("username")?.toString() || null,
nickname: formData.get("nickname")?.toString() || null,
email: formData.get("email")?.toString() || null,
phone: formData.get("phone")?.toString() || null,
password: formData.get("password")?.toString() || null,
});
setOpen(false);
toast.success('创建成功')
} catch (error) {
toast.error((error as ApiError).message || '创建失败')
}
}
return (
<>
<div onClick={() => setOpen(true)} className="cursor-pointer">
{children}
</div>
<Drawer open={open} onClose={() => setOpen(false)}>
<DrawerContent>
<DrawerHeader className="text-left">
<DrawerTitle></DrawerTitle>
<DrawerDescription></DrawerDescription>
</DrawerHeader>
<form className="grid items-start gap-4 px-4" onSubmit={handleSubmit}>
<div className="grid gap-2">
<Label htmlFor="username"></Label>
<Input id="username" name="username" />
</div>
<div className="grid gap-2">
<Label htmlFor="nickname"></Label>
<Input id="nickname" name="nickname" />
</div>
<div className="grid gap-2">
<Label htmlFor="email"></Label>
<Input id="email" name="email" />
</div>
<div className="grid gap-2">
<Label htmlFor="phone"></Label>
<Input id="phone" name="phone" />
</div>
<div className="grid gap-2">
<Label htmlFor="phone"></Label>
<Input id="phone" name="phone" />
</div>
<Button type="submit"></Button>
</form>
<DrawerFooter className="pt-2">
<DrawerClose asChild>
<Button variant="outline"></Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
</>
)
}

View File

@@ -7,6 +7,7 @@ import { useUserList } from "@/hooks/admin/user/use-user-list";
import { useState } from "react";
import { UserInfoEditor } from "./components/user-info-editor";
import { User } from "@/lib/types/user";
import { CreateUserEditor } from "./components/create-user-editor";
export default function Page() {
@@ -47,6 +48,11 @@ export default function Page() {
return (
<>
<div>
<CreateUserEditor >
<Button ></Button>
</CreateUserEditor>
</div>
<Table>
{error && <TableCaption>{error.message}</TableCaption>}
<TableHeader>