1
This commit is contained in:
parent
a4a1e2d808
commit
635959ced8
@ -56,12 +56,12 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
initialValues,
|
||||
projectGroupId,
|
||||
}) => {
|
||||
const [gitInstances, setGitInstances] = useState<ExternalSystemResponse[]>([]);
|
||||
const [externalSystems, setExternalSystems] = useState<ExternalSystemResponse[]>([]);
|
||||
const [repositoryGroups, setRepositoryGroups] = useState<RepositoryGroup[]>([]);
|
||||
const [repositoryProjects, setRepositoryProjects] = useState<RepositoryProject[]>([]);
|
||||
const {toast} = useToast();
|
||||
const isEdit = !!initialValues?.id;
|
||||
const [selectedGitInstance, setSelectedGitInstance] = useState<ExternalSystemResponse>();
|
||||
const [selectedExternalSystem, setSelectedExternalSystem] = useState<ExternalSystemResponse>();
|
||||
|
||||
const form = useForm<ApplicationFormValues>({
|
||||
resolver: zodResolver(applicationFormSchema),
|
||||
@ -79,20 +79,20 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
},
|
||||
});
|
||||
|
||||
// 加载Git实例列表
|
||||
const loadGitInstances = async () => {
|
||||
// 加载外部系统列表
|
||||
const loadExternalSystems = async () => {
|
||||
try {
|
||||
const response = await getExternalSystems({
|
||||
type: SystemType.GIT,
|
||||
enabled: true,
|
||||
});
|
||||
if (response?.content) {
|
||||
setGitInstances(response.content);
|
||||
setExternalSystems(response.content);
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "加载Git实例失败",
|
||||
title: "加载外部系统失败",
|
||||
description: error instanceof Error ? error.message : undefined,
|
||||
duration: 3000,
|
||||
});
|
||||
@ -100,7 +100,7 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadGitInstances();
|
||||
loadExternalSystems();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
@ -117,15 +117,15 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
repoProjectId: initialValues.repoProjectId,
|
||||
projectGroupId: initialValues.projectGroupId,
|
||||
});
|
||||
const gitInstance = gitInstances.find(instance => instance.id === initialValues.externalSystemId);
|
||||
if (gitInstance) {
|
||||
setSelectedGitInstance(gitInstance);
|
||||
const externalSystem = externalSystems.find(system => system.id === initialValues.externalSystemId);
|
||||
if (externalSystem) {
|
||||
setSelectedExternalSystem(externalSystem);
|
||||
}
|
||||
}
|
||||
}, [initialValues, form, gitInstances]);
|
||||
}, [initialValues, form, externalSystems]);
|
||||
|
||||
// 当选择Git实例时,获取对应的仓库组列表
|
||||
const handleGitInstanceChange = (externalSystemId: number) => {
|
||||
// 当选择外部系统时,获取对应的仓库组列表
|
||||
const handleExternalSystemChange = (externalSystemId: number) => {
|
||||
form.setValue('repoGroupId', undefined);
|
||||
form.setValue('repoProjectId', undefined);
|
||||
setRepositoryProjects([]);
|
||||
@ -150,12 +150,29 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
...values,
|
||||
id: initialValues!.id,
|
||||
});
|
||||
toast({
|
||||
title: "更新成功",
|
||||
description: `应用 ${values.appName} 已更新`,
|
||||
duration: 3000,
|
||||
});
|
||||
} else {
|
||||
await createApplication(values);
|
||||
toast({
|
||||
title: "创建成功",
|
||||
description: `应用 ${values.appName} 已创建`,
|
||||
duration: 3000,
|
||||
});
|
||||
}
|
||||
onSuccess?.();
|
||||
onCancel();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: isEdit ? "更新失败" : "创建失败",
|
||||
description: e instanceof Error ? e.message : "操作失败,请重试",
|
||||
duration: 3000,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -240,26 +257,26 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
name="externalSystemId"
|
||||
render={({field}) => (
|
||||
<FormItem>
|
||||
<FormLabel>Git实例</FormLabel>
|
||||
<FormLabel>外部系统</FormLabel>
|
||||
<Select
|
||||
onValueChange={(value) => {
|
||||
field.onChange(Number(value));
|
||||
handleGitInstanceChange(Number(value));
|
||||
handleExternalSystemChange(Number(value));
|
||||
}}
|
||||
value={field.value?.toString()}
|
||||
>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="请选择Git实例"/>
|
||||
<SelectValue placeholder="请选择外部系统"/>
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
{gitInstances.map((instance) => (
|
||||
{externalSystems.map((system) => (
|
||||
<SelectItem
|
||||
key={instance.id}
|
||||
value={instance.id.toString()}
|
||||
key={system.id}
|
||||
value={system.id.toString()}
|
||||
>
|
||||
{instance.name}
|
||||
{system.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
@ -299,7 +316,7 @@ const ApplicationModal: React.FC<ApplicationModalProps> = ({
|
||||
(group) => group.id === field.value
|
||||
)?.fullName || "请选择代码仓库组"
|
||||
: !form.watch('externalSystemId')
|
||||
? "请先选择Git实例"
|
||||
? "请先选择外部系统"
|
||||
: "请选择代码仓库组"}
|
||||
<ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
|
||||
</Button>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user