fix: agent profile save button not working
- Replace form attribute with ref-based form submission to avoid potential portal-related form resolution issues in base-ui Dialog - Add null guard for fetchUsersAll return value to prevent crash when API returns null data
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { Controller, Resolver, useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod/v4";
|
||||
@@ -206,7 +206,7 @@ function AgentEditDialogBody({
|
||||
const [users, setUsers] = useState<AdminUser[]>([]);
|
||||
const [userSelectOpen, setUserSelectOpen] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const userOptions = users.map((user) => ({
|
||||
const userOptions = (Array.isArray(users) ? users : []).map((user) => ({
|
||||
value: String(user.id),
|
||||
label: `${user.nickname || user.username} (${user.username})`,
|
||||
}));
|
||||
@@ -215,7 +215,7 @@ function AgentEditDialogBody({
|
||||
const [usersData] = await Promise.all([
|
||||
fetchUsersAll(),
|
||||
]);
|
||||
setUsers(usersData);
|
||||
setUsers(Array.isArray(usersData) ? usersData : []);
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "加载选项失败");
|
||||
}
|
||||
@@ -228,6 +228,7 @@ function AgentEditDialogBody({
|
||||
resolver: editFormResolver,
|
||||
defaultValues: buildFormWithDefaultTeam(null, defaultTeamId),
|
||||
});
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const {
|
||||
control,
|
||||
handleSubmit,
|
||||
@@ -265,8 +266,6 @@ function AgentEditDialogBody({
|
||||
await onSubmit(buildPayload(values));
|
||||
}
|
||||
|
||||
const formId = "agent-edit-form";
|
||||
|
||||
return (
|
||||
<ProjectDialog
|
||||
open={open}
|
||||
@@ -283,7 +282,11 @@ function AgentEditDialogBody({
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button type="submit" form={formId} disabled={saving || loading}>
|
||||
<Button
|
||||
type="button"
|
||||
onClick={() => formRef.current?.requestSubmit()}
|
||||
disabled={saving || loading}
|
||||
>
|
||||
{saving ? "保存中..." : "保存"}
|
||||
</Button>
|
||||
</>
|
||||
@@ -295,7 +298,7 @@ function AgentEditDialogBody({
|
||||
</div>
|
||||
) : (
|
||||
<form
|
||||
id={formId}
|
||||
ref={formRef}
|
||||
onSubmit={handleSubmit(onFormSubmit)}
|
||||
className="space-y-4"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user