import { Button, Form, Input, Spin, Switch, message } from "antd"; import { useContext, useEffect } from "react"; import { useStaff } from "@nice/client"; import DepartmentSelect from "@web/src/components/models/department/department-select"; import { api } from "@nice/client"; import { useAuth } from "@web/src/providers/auth-provider"; import AvatarUploader from "@web/src/components/common/uploader/AvatarUploader"; import { StaffDto } from "@nice/common"; import { UserEditorContext } from "./UserMenu"; import toast from "react-hot-toast"; export default function StaffForm() { const { user } = useAuth(); const { create, update } = useStaff(); // Ensure you have these methods in your hooks const { formLoading, modalOpen, setModalOpen, domainId, setDomainId, form, setFormLoading, } = useContext(UserEditorContext); const { data, isLoading, }: { data: StaffDto; isLoading: boolean; } = api.staff.findFirst.useQuery( { where: { id: user?.id } }, { enabled: !!user?.id } ); const { isRoot } = useAuth(); async function handleFinish(values: any) { const { username, showname, deptId, domainId, password, phoneNumber, officerId, enabled, avatar, photoUrl, email, rank, office, } = values; setFormLoading(true); try { if (data && user?.id) { await update.mutateAsync({ where: { id: data.id }, data: { username, deptId, showname, domainId, password, phoneNumber, officerId, enabled, avatar, }, }); } toast.success("提交成功"); setModalOpen(false); } catch (err: any) { toast.error(err.message); } finally { setFormLoading(false); } } useEffect(() => { form.resetFields(); console.log('cc', data); if (data) { form.setFieldValue("username", data.username); form.setFieldValue("showname", data.showname); form.setFieldValue("domainId", data.domainId); form.setFieldValue("deptId", data.deptId); form.setFieldValue("officerId", data.officerId); form.setFieldValue("phoneNumber", data.phoneNumber); form.setFieldValue("enabled", data.enabled); form.setFieldValue("avatar", data.avatar); } }, [data]); // useEffect(() => { // if (!data && domainId) { // form.setFieldValue("domainId", domainId); // form.setFieldValue("deptId", domainId); // } // }, [domainId, data as any]); return (
{isLoading && (
)}
{ setDomainId(value as string); }} domain={true} />
); }