Skip to content

Commit 2916e8b

Browse files
committed
chore(platform): adjust list page
1 parent e07e107 commit 2916e8b

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

packages/platform/src/app/routes/list/standard-table/DeviceModal.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ import { useAsync, useEventCallback, useId } from '@react-devui/hooks';
99
import { FormControl, FormGroup, useForm, Validators } from '@react-devui/ui';
1010
import { DForm, DInput, DModal, DSelect } from '@react-devui/ui';
1111

12+
import { useAPI } from '../../../hooks';
13+
1214
export interface AppDeviceModalProps {
13-
aModelList: DSelectItem<string>[] | undefined;
15+
onSuccess: () => void;
1416
}
1517

1618
function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenModalFn<DeviceData>>): JSX.Element | null {
17-
const { aModelList } = props;
19+
const { onSuccess } = props;
1820

21+
const modelApi = useAPI('/device/model');
1922
const async = useAsync();
2023

2124
const uniqueId = useId();
2225
const id = `app-form-${uniqueId}`;
2326

27+
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
28+
2429
const [visible, setVisible] = useState(false);
2530
const [device, setDevice] = useState<DeviceData>();
2631
const [form, updateForm] = useForm(
@@ -34,8 +39,23 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenMod
3439
const open = useEventCallback<OpenModalFn<DeviceData>>((device) => {
3540
setVisible(true);
3641
setDevice(device);
42+
3743
form.reset(device ? { name: device.name, model: device.model } : undefined);
3844
updateForm();
45+
46+
if (isUndefined(modelList)) {
47+
modelApi.list().subscribe({
48+
next: (res) => {
49+
setModelList(
50+
res.resources.map((model) => ({
51+
label: model.name,
52+
value: model.name,
53+
disabled: model.disabled,
54+
}))
55+
);
56+
},
57+
});
58+
}
3959
});
4060

4161
useImperativeHandle(ref, () => open, [open]);
@@ -50,6 +70,7 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenMod
5070
onOkClick={() =>
5171
new Promise((r) => {
5272
async.setTimeout(() => {
73+
onSuccess();
5374
r(true);
5475
}, 500);
5576
})
@@ -69,9 +90,9 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenMod
6990
<DForm.Item dFormControls={{ model: 'Please select model!' }} dLabel="Model">
7091
{({ model }) => (
7192
<DSelect
72-
dFormControl={aModelList ? model : undefined}
73-
dList={aModelList ?? []}
74-
dLoading={isUndefined(aModelList)}
93+
dFormControl={modelList ? model : undefined}
94+
dList={modelList ?? []}
95+
dLoading={isUndefined(modelList)}
7596
dPlaceholder="Model"
7697
dClearable
7798
/>

packages/platform/src/app/routes/list/standard-table/StandardTable.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ export default function StandardTable(): JSX.Element | null {
152152
}}
153153
/>
154154
)}
155-
<AppDeviceModal ref={deviceModalRef} aModelList={modelList} />
155+
<AppDeviceModal
156+
ref={deviceModalRef}
157+
onSuccess={() => {
158+
setUpdateDeviceTable((n) => n + 1);
159+
}}
160+
/>
156161
<AppRouteHeader>
157162
<AppRouteHeader.Breadcrumb
158163
aList={[

packages/platform/src/app/routes/list/standard-table/detail/Detail.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import type { OpenModalFn } from '../../../../utils/types';
22
import type { DeviceData } from '../StandardTable';
3-
import type { DSelectItem } from '@react-devui/ui/components/select';
43

54
import { isUndefined } from 'lodash';
6-
import { useRef, useState } from 'react';
5+
import { useEffect, useRef, useState } from 'react';
76
import { useTranslation } from 'react-i18next';
87
import { useParams } from 'react-router-dom';
98

10-
import { useMount } from '@react-devui/hooks';
119
import { EditOutlined } from '@react-devui/icons';
1210
import { DButton, DCard, DSeparator, DSpinner, DTable } from '@react-devui/ui';
1311

@@ -21,38 +19,34 @@ export default function Detail(): JSX.Element | null {
2119

2220
const { t } = useTranslation();
2321

24-
const modelApi = useAPI('/device/model');
2522
const deviceApi = useAPI('/device');
2623

2724
const { id: _id } = useParams();
2825
const id = Number(_id!);
2926

27+
const [deviceLoading, setDeviceLoading] = useState(true);
3028
const [device, setDevice] = useState<DeviceData>();
3129

32-
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
33-
34-
useMount(() => {
35-
modelApi.list().subscribe({
36-
next: (res) => {
37-
setModelList(
38-
res.resources.map((model) => ({
39-
label: model.name,
40-
value: model.name,
41-
disabled: model.disabled,
42-
}))
43-
);
44-
},
45-
});
30+
const [updateDevice, setUpdateDevice] = useState(0);
31+
useEffect(() => {
32+
setDeviceLoading(true);
4633
deviceApi.get(id).subscribe({
4734
next: (device) => {
35+
setDeviceLoading(false);
4836
setDevice(device);
4937
},
5038
});
51-
});
39+
// eslint-disable-next-line react-hooks/exhaustive-deps
40+
}, [updateDevice]);
5241

5342
return (
5443
<>
55-
<AppDeviceModal ref={deviceModalRef} aModelList={modelList} />
44+
<AppDeviceModal
45+
ref={deviceModalRef}
46+
onSuccess={() => {
47+
setUpdateDevice((n) => n + 1);
48+
}}
49+
/>
5650
<AppRouteHeader>
5751
<AppRouteHeader.Breadcrumb
5852
aList={[
@@ -87,6 +81,7 @@ export default function Detail(): JSX.Element | null {
8781
</div>
8882
) : (
8983
<>
84+
<DSpinner dVisible={deviceLoading}></DSpinner>
9085
<DCard>
9186
<DCard.Content>
9287
<div className="app-title mb-3">Title 1</div>

0 commit comments

Comments
 (0)