Skip to content

Commit ee1d2e6

Browse files
committed
refactor(platform): add fn to open modal
1 parent 4ebea7b commit ee1d2e6

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,29 @@ export default function StandardTable(): JSX.Element | null {
6262
const allDeviceSelected =
6363
deviceTable.selected.size === 0 ? false : deviceTable.selected.size === deviceTable.list.length ? true : 'mixed';
6464

65-
const [paramsOfDeviceModal, setParamsOfDeviceModal] = useImmer<{
66-
visible: boolean;
67-
device: DeviceDoc | undefined;
68-
}>();
65+
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
6966

7067
const [paramsOfDeleteModal, setParamsOfDeleteModal] = useImmer<{
7168
visible: boolean;
7269
device: DeviceDoc;
7370
}>();
7471

75-
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
76-
72+
const [paramsOfDeviceModal, setParamsOfDeviceModal] = useImmer<{
73+
visible: boolean;
74+
device: DeviceDoc | undefined;
75+
}>();
7776
const [deviceForm, updateDeviceForm] = useForm(
7877
() =>
7978
new FormGroup({
8079
name: new FormControl('', Validators.required),
8180
model: new FormControl<string | null>(null, Validators.required),
8281
})
8382
);
83+
const openDeviceModal = (device?: DeviceDoc) => {
84+
setParamsOfDeviceModal({ visible: true, device });
85+
deviceForm.reset(device ? { name: device.name, model: device.model } : undefined);
86+
updateDeviceForm();
87+
};
8488

8589
useMount(() => {
8690
modelApi.list().subscribe({
@@ -193,9 +197,7 @@ export default function StandardTable(): JSX.Element | null {
193197
aActions={[
194198
<DButton
195199
onClick={() => {
196-
setParamsOfDeviceModal({ visible: true, device: undefined });
197-
deviceForm.reset();
198-
updateDeviceForm();
200+
openDeviceModal();
199201
}}
200202
dIcon={<PlusOutlined />}
201203
>
@@ -351,9 +353,7 @@ export default function StandardTable(): JSX.Element | null {
351353
onItemClick={(action) => {
352354
switch (action) {
353355
case 'edit':
354-
setParamsOfDeviceModal({ visible: true, device: data });
355-
deviceForm.reset({ name: data.name, model: data.model });
356-
updateDeviceForm();
356+
openDeviceModal(data);
357357
break;
358358

359359
case 'delete':

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@ export default function Detail(): JSX.Element | null {
2626
const id = Number(_id!);
2727

2828
const [device, setDevice] = useState<DeviceDoc>();
29+
30+
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
31+
2932
const [paramsOfEditModal, setParamsOfEditModal] = useImmer<{
3033
visible: boolean;
3134
}>();
32-
33-
const [deviceForm, updateDeviceForm] = useForm(
35+
const [editForm, updateEditForm] = useForm(
3436
() =>
3537
new FormGroup({
3638
name: new FormControl('', Validators.required),
3739
model: new FormControl<string | null>(null, Validators.required),
3840
})
3941
);
40-
41-
const [modelList, setModelList] = useState<DSelectItem<string>[]>();
42+
const openEditModal = (device: DeviceDoc) => {
43+
setParamsOfEditModal({ visible: true });
44+
editForm.reset({ name: device.name, model: device.model });
45+
updateEditForm();
46+
};
4247

4348
useMount(() => {
4449
modelApi.list().subscribe({
@@ -65,8 +70,8 @@ export default function Detail(): JSX.Element | null {
6570
<AppDeviceModal
6671
aVisible={paramsOfEditModal.visible}
6772
aHeader={`${device ? 'Edit' : 'Add'} Device`}
68-
aForm={deviceForm}
69-
aUpdateForm={updateDeviceForm}
73+
aForm={editForm}
74+
aUpdateForm={updateEditForm}
7075
aModelList={modelList}
7176
onClose={() => {
7277
setParamsOfEditModal((draft) => {
@@ -103,9 +108,7 @@ export default function Detail(): JSX.Element | null {
103108
disabled={isUndefined(device)}
104109
onClick={() => {
105110
if (!isUndefined(device)) {
106-
setParamsOfEditModal({ visible: true });
107-
deviceForm.reset({ name: device.name, model: device.model });
108-
updateDeviceForm();
111+
openEditModal(device);
109112
}
110113
}}
111114
dIcon={<EditOutlined />}

0 commit comments

Comments
 (0)