"use client" import { getUserGroupAll, userAdd } from "@/components/label/api/user" import { Button, Group, Modal, Select, Stack, TextInput, } from "@mantine/core" import { useForm } from "@mantine/form" import { notifications } from "@mantine/notifications" import { useEffect, useState } from "react" interface ComponentProps { opened: boolean onCloseAction: (refresh?: boolean) => void } export default function CreateUserModal({ opened, onCloseAction, }: ComponentProps) { const [groupOpts, setGroupOpts] = useState>( [] ) const [submitting, setSubmitting] = useState(false) const form = useForm({ initialValues: { user_name: "", group_uuid: "", base_city: "", }, validate: { user_name: (v) => (v.trim() ? null : "请输入用户名"), group_uuid: (v) => (v ? null : "请选择组织"), base_city: (v) => (v.trim() ? null : "请输入所在地"), }, }) useEffect(() => { if (!opened) return const run = async () => { const res = await getUserGroupAll() setGroupOpts(res ?? []) } queueMicrotask(run) }, [opened]) const submit = form.onSubmit(async (values) => { try { setSubmitting(true) await userAdd({ user_name: values.user_name.trim(), group_uuid: values.group_uuid, base_city: values.base_city.trim(), }) notifications.show({ color: "green", title: "操作成功", message: "已新增用户", }) form.reset() onCloseAction(true) } catch (e) { notifications.show({ color: "red", title: "操作失败", message: e instanceof Error ? e.message : "请求失败", }) } finally { setSubmitting(false) } }) return ( onCloseAction()} title="新增用户" centered size={560} closeOnClickOutside={false}>