Skip to content

Commit 7a64dc1

Browse files
committed
refactor(platform): adjust useAPI
1 parent 30178ad commit 7a64dc1

8 files changed

Lines changed: 37 additions & 43 deletions

File tree

packages/platform/src/app/core/http/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DeviceDoc } from '../../hooks/api/types';
1+
import type { DeviceDoc } from '../../utils/types';
22
import type { UserState, NotificationItem } from '../state';
33
import type { JWTToken, JWTTokenPayload } from '../token';
44

packages/platform/src/app/hooks/api/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { useAPI } from './api';
1+
export { useAPI } from './useAPI';
22
export { usePageTitle } from './usePageTitle';
33
export { useQueryParams } from './useQueryParams';

packages/platform/src/app/hooks/api/useAPI.ts renamed to packages/platform/src/app/hooks/useAPI.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
// https://cloud.google.com/apis/design
2-
3-
import type { StandardFields, StandardGetParams, StandardListRes } from './types';
4-
52
import { useEventCallback } from '@react-devui/hooks';
63

7-
import { useHttp } from '../../core';
4+
import { useHttp } from '../core';
5+
6+
interface StandardListRes<T> {
7+
resources: T[];
8+
metadata: {
9+
page: number;
10+
page_size: number;
11+
total_size: number;
12+
};
13+
}
814

915
export function useAPI(url: string) {
1016
const http = useHttp();
1117

1218
return {
13-
list: useEventCallback(<T extends StandardFields = any, P extends StandardGetParams = StandardGetParams>(params?: P) =>
19+
list: useEventCallback(<T = any, P = any>(params?: P) =>
1420
http<StandardListRes<T>>({
1521
url,
1622
method: 'get',
1723
params,
1824
})
1925
),
20-
get: useEventCallback(<T extends StandardFields = any, P extends StandardGetParams = StandardGetParams>(id: number, params?: P) =>
26+
get: useEventCallback(<T = any, P = any>(id: number, params?: P) =>
2127
http<T>({
2228
url: `${url}/${id}`,
2329
method: 'get',
2430
params,
2531
})
2632
),
27-
create: useEventCallback(<T extends StandardFields = any, D = any>(data: D) =>
33+
create: useEventCallback(<T = any, D = any>(data: D) =>
2834
http<T, D>({
2935
url,
3036
method: 'post',
3137
data,
3238
})
3339
),
34-
update: useEventCallback(<T extends StandardFields = any, D = any>(id: number, data: D) =>
40+
update: useEventCallback(<T = any, D = any>(id: number, data: D) =>
3541
http<T, D>({
3642
url: `${url}/${id}`,
3743
method: 'put',

packages/platform/src/app/hooks/useTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StandardFields } from './api/types';
1+
import type { StandardFields } from '../utils/types';
22

33
import { useImmer } from '@react-devui/hooks';
44

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DeviceDoc } from '../../../hooks/api/types';
1+
import type { DeviceDoc, StandardQueryParams } from '../../../utils/types';
22
import type { DSelectItem } from '@react-devui/ui/components/select';
33

44
import { isUndefined } from 'lodash';
@@ -30,6 +30,8 @@ import { useAPI, useQueryParams } from '../../../hooks';
3030
import { AppDeviceModal } from './DeviceModal';
3131
import styles from './StandardTable.module.scss';
3232

33+
type Device = DeviceDoc;
34+
3335
interface DeviceQueryParams {
3436
keyword: string;
3537
sort: 'id' | '-id' | null;
@@ -68,7 +70,7 @@ export default function StandardTable(): JSX.Element | null {
6870

6971
const [deviceTable, setDeviceTable] = useImmer({
7072
loading: true,
71-
list: [] as DeviceDoc[],
73+
list: [] as Device[],
7274
totalSize: 0,
7375
selected: new Set<number>(),
7476
});
@@ -79,12 +81,12 @@ export default function StandardTable(): JSX.Element | null {
7981

8082
const [paramsOfDeleteModal, setParamsOfDeleteModal] = useImmer<{
8183
visible: boolean;
82-
device: DeviceDoc;
84+
device: Device;
8385
}>();
8486

8587
const [paramsOfDeviceModal, setParamsOfDeviceModal] = useImmer<{
8688
visible: boolean;
87-
device: DeviceDoc | undefined;
89+
device: Device | undefined;
8890
}>();
8991
const [deviceForm, updateDeviceForm] = useForm(
9092
() =>
@@ -93,7 +95,7 @@ export default function StandardTable(): JSX.Element | null {
9395
model: new FormControl<string | null>(null, Validators.required),
9496
})
9597
);
96-
const openDeviceModal = (device?: DeviceDoc) => {
98+
const openDeviceModal = (device?: Device) => {
9799
setParamsOfDeviceModal({ visible: true, device });
98100
deviceForm.reset(device ? { name: device.name, model: device.model } : undefined);
99101
updateDeviceForm();
@@ -117,7 +119,7 @@ export default function StandardTable(): JSX.Element | null {
117119
useEffect(() => {
118120
setDeviceQuerySaved(deviceQuery);
119121

120-
const apiQuery: any = {
122+
const apiQuery: StandardQueryParams = {
121123
page: deviceQuery.page,
122124
page_size: deviceQuery.pageSize,
123125
};
@@ -128,7 +130,7 @@ export default function StandardTable(): JSX.Element | null {
128130
setDeviceTable((draft) => {
129131
draft.loading = true;
130132
});
131-
deviceApi.list<DeviceDoc>(apiQuery).subscribe({
133+
deviceApi.list<Device>(apiQuery).subscribe({
132134
next: (res) => {
133135
setDeviceQuery((draft) => {
134136
draft.page = res.metadata.page;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DeviceDoc } from '../../../../hooks/api/types';
1+
import type { DeviceDoc } from '../../../../utils/types';
22
import type { DSelectItem } from '@react-devui/ui/components/select';
33

44
import { isUndefined } from 'lodash';
@@ -15,6 +15,8 @@ import { useAPI } from '../../../../hooks';
1515
import { AppDeviceModal } from '../DeviceModal';
1616
import styles from './Detail.module.scss';
1717

18+
type Device = DeviceDoc;
19+
1820
export default function Detail(): JSX.Element | null {
1921
const { t } = useTranslation();
2022
const async = useAsync();
@@ -25,7 +27,7 @@ export default function Detail(): JSX.Element | null {
2527
const { id: _id } = useParams();
2628
const id = Number(_id!);
2729

28-
const [device, setDevice] = useState<DeviceDoc>();
30+
const [device, setDevice] = useState<Device>();
2931

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

@@ -39,7 +41,7 @@ export default function Detail(): JSX.Element | null {
3941
model: new FormControl<string | null>(null, Validators.required),
4042
})
4143
);
42-
const openEditModal = (device: DeviceDoc) => {
44+
const openEditModal = (device: Device) => {
4345
setParamsOfEditModal({ visible: true });
4446
editForm.reset({ name: device.name, model: device.model });
4547
updateEditForm();

packages/platform/src/app/hooks/api/types.ts renamed to packages/platform/src/app/utils/types.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
export interface StandardFields {
2-
id: number;
3-
create_time: number;
4-
update_time: number;
5-
}
6-
7-
export interface ErrorStandardFields {
8-
error: {
9-
code: string;
10-
message: string;
11-
};
12-
}
13-
14-
export interface StandardGetParams {
1+
export interface StandardQueryParams {
152
sort?: string;
163
page?: number;
174
page_size?: number;
5+
[index: string]: any;
186
}
197

20-
export interface StandardListRes<T> {
21-
resources: T[];
22-
metadata: {
23-
page: number;
24-
page_size: number;
25-
total_size: number;
26-
};
8+
export interface StandardFields {
9+
id: number;
10+
create_time: number;
11+
update_time: number;
2712
}
2813

2914
export interface DeviceDoc extends StandardFields {

0 commit comments

Comments
 (0)