Skip to content

Commit

Permalink
chore(platform): update the model when modelList loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Mar 6, 2023
1 parent adb5f33 commit 453f13b
Showing 1 changed file with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
const modelApi = useAPI('/device/model');
const async = useAsync();

const [modelList, setModelList] = useState<DSelectItem<string>[]>();

const [visible, setVisible] = useState(false);
const [device, setDevice] = useState<DeviceData>();
const [form, updateForm] = useForm(
Expand All @@ -34,26 +32,31 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
})
);

const [modelList, setModelList] = useState<DSelectItem<string>[]>();

const open = useEventCallback<OpenSettingFn<DeviceData>>((device) => {
setVisible(true);
setDevice(device);

form.reset(device ? { name: device.name, model: device.model } : undefined);
form.reset(device ? { name: device.name } : undefined);
updateForm();

if (isUndefined(modelList)) {
modelApi.list().subscribe({
next: (res) => {
setModelList(
res.resources.map((model) => ({
label: model.name,
value: model.name,
disabled: model.disabled,
}))
);
},
});
}
setModelList(undefined);
modelApi.list().subscribe({
next: (res) => {
setModelList(
res.resources.map((model) => ({
label: model.name,
value: model.name,
disabled: model.disabled,
}))
);
if (device) {
form.patchValue({ model: device.model });
updateForm();
}
},
});
});

useImperativeHandle(ref, () => open, [open]);
Expand Down Expand Up @@ -88,13 +91,7 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
</DForm.Item>
<DForm.Item dFormControls={{ model: 'Please select model!' }} dLabel="Model">
{({ model }) => (
<DSelect
dFormControl={modelList ? model : undefined}
dList={modelList ?? []}
dLoading={isUndefined(modelList)}
dPlaceholder="Model"
dClearable
/>
<DSelect dFormControl={model} dList={modelList ?? []} dLoading={isUndefined(modelList)} dPlaceholder="Model" dClearable />
)}
</DForm.Item>
</DForm.Group>
Expand Down

0 comments on commit 453f13b

Please sign in to comment.