Skip to content

Commit b29bceb

Browse files
committed
optimize(projects): optimize code
1 parent 3dbe701 commit b29bceb

File tree

5 files changed

+50
-48
lines changed

5 files changed

+50
-48
lines changed

src/hooks/common/table.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export function useTable<A extends AntDesign.TableApiFn>(
1212
paginationConfig?: Omit<TablePaginationConfig, 'total' | 'current' | 'pageSize' | 'onChange'>
1313
) {
1414
const isMobile = useAppSelector(getIsMobile);
15+
1516
const [total, setTotal] = useState<TablePaginationConfig['total']>(0);
1617

17-
const { apiFn, apiParams, immediate } = config;
18+
const { apiFn, apiParams, immediate, rowKey = 'id' } = config;
19+
20+
const [form] = Form.useForm<AntDesign.AntDesignTableConfig<A>['apiParams']>();
1821

19-
const [form] = Form.useForm();
2022
const {
2123
loading,
2224
empty,
@@ -111,20 +113,25 @@ export function useTable<A extends AntDesign.TableApiFn>(
111113

112114
if (res) {
113115
if (isResetCurrent) {
114-
updateSearchParams({ current: 1, ...res });
116+
const { current = 1, ...rest } = res;
117+
updateSearchParams({ current, ...rest });
115118
} else {
116119
updateSearchParams(res);
117120
}
118121
}
119122
}
120123

121124
return {
122-
loading,
125+
tableProps: {
126+
loading,
127+
dataSource: data,
128+
columns,
129+
rowKey,
130+
pagination
131+
},
123132
empty,
124133
data,
125-
columns,
126134
columnChecks,
127-
pagination,
128135
run,
129136
setColumnChecks,
130137
searchParams,
@@ -208,7 +215,8 @@ export function useTableScroll(scrollX: number = 702) {
208215
const size = useSize(tableWrapperRef);
209216

210217
const height = size?.height;
211-
const result = height && height < 435 ? height - 184 : undefined;
218+
219+
const result = height && height < 600 ? height - 184 : undefined;
212220

213221
const scrollConfig = {
214222
y: result,

src/pages/manage/user/index.tsx

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Button, Card, Popconfirm, Table, Tag } from 'antd';
21
import { Suspense, lazy } from 'react';
32
import { fetchGetUserList } from '@/service/api';
43
import { enableStatusRecord, userGenderRecord } from '@/constants/business';
@@ -22,7 +21,7 @@ export function Component() {
2221

2322
const { tableWrapperRef, scrollConfig } = useTableScroll();
2423

25-
const { columns, columnChecks, data, run, loading, pagination, reset, form, setColumnChecks } = useTable(
24+
const { columnChecks, data, reset, form, setColumnChecks, tableProps, run } = useTable(
2625
{
2726
apiFn: fetchGetUserList,
2827
apiParams: {
@@ -65,7 +64,7 @@ export function Component() {
6564

6665
const label = t(userGenderRecord[record.userGender]);
6766

68-
return <Tag color={tagUserGenderMap[record.userGender]}>{label}</Tag>;
67+
return <ATag color={tagUserGenderMap[record.userGender]}>{label}</ATag>;
6968
}
7069
},
7170
{
@@ -99,10 +98,8 @@ export function Component() {
9998
if (record.status === null) {
10099
return null;
101100
}
102-
103101
const label = t(enableStatusRecord[record.status]);
104-
105-
return <Tag color={tagMap[record.status]}>{label}</Tag>;
102+
return <ATag color={tagMap[record.status]}>{label}</ATag>;
106103
}
107104
},
108105
{
@@ -112,25 +109,25 @@ export function Component() {
112109
width: 130,
113110
render: (_, record) => (
114111
<div className="flex-center gap-8px">
115-
<Button
112+
<AButton
116113
type="primary"
117114
ghost
118115
size="small"
119116
onClick={() => edit(record.id)}
120117
>
121118
{t('common.edit')}
122-
</Button>
123-
<Popconfirm
119+
</AButton>
120+
<APopconfirm
124121
title={t('common.confirmDelete')}
125122
onConfirm={() => handleDelete(record.id)}
126123
>
127-
<Button
124+
<AButton
128125
danger
129126
size="small"
130127
>
131128
{t('common.delete')}
132-
</Button>
133-
</Popconfirm>
129+
</AButton>
130+
</APopconfirm>
134131
</div>
135132
)
136133
}
@@ -175,7 +172,7 @@ export function Component() {
175172
reset={reset}
176173
form={form}
177174
/>
178-
<Card
175+
<ACard
179176
styles={{ body: { flex: 1, overflow: 'hidden' } }}
180177
ref={tableWrapperRef}
181178
bordered={false}
@@ -184,7 +181,7 @@ export function Component() {
184181
onDelete={handleBatchDelete}
185182
refresh={run}
186183
add={handleAdd}
187-
loading={loading}
184+
loading={tableProps.loading}
188185
setColumnChecks={setColumnChecks}
189186
disabledDelete={checkedRowKeys.length === 0}
190187
columns={columnChecks}
@@ -193,15 +190,11 @@ export function Component() {
193190
title={t('page.manage.user.title')}
194191
className="flex-col-stretch sm:flex-1-hidden card-wrapper"
195192
>
196-
<Table
197-
pagination={pagination}
198-
rowKey="id"
193+
<ATable
199194
scroll={scrollConfig}
200195
rowSelection={rowSelection}
201196
size="small"
202-
loading={loading}
203-
dataSource={data}
204-
columns={columns}
197+
{...tableProps}
205198
/>
206199
<Suspense>
207200
<UserOperateDrawer
@@ -212,7 +205,7 @@ export function Component() {
212205
closeDrawer={closeDrawer}
213206
/>
214207
</Suspense>
215-
</Card>
208+
</ACard>
216209
</div>
217210
);
218211
}

src/pages/manage/user/modules/UserOperateDrawer.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Button, Drawer, Flex, Form, Input, Radio, Select } from 'antd';
22
import type { FC } from 'react';
3+
import { useRequest } from '@sa/hooks';
34
import { enableStatusOptions, userGenderOptions } from '@/constants/business';
45
import { fetchGetAllRoles } from '@/service/api';
56

@@ -23,35 +24,32 @@ interface OptionsProps {
2324
value: string;
2425
}
2526

27+
function getOptions(item: Api.SystemManage.AllRole) {
28+
return {
29+
label: item.roleName,
30+
value: item.roleCode
31+
};
32+
}
33+
2634
const UserOperateDrawer: FC<Props> = ({ open, closeDrawer, submitted, operateType, rowData }) => {
2735
const { t } = useTranslation();
2836

29-
const [roleOptions, setRoleOptions] = useState<OptionsProps[]>([]);
30-
3137
const [form] = Form.useForm<Model>();
3238

39+
const { data, run } = useRequest(fetchGetAllRoles, {
40+
manual: true
41+
});
42+
43+
const roleOptions: OptionsProps[] = data ? data.map(getOptions) : [];
44+
3345
const onClose = () => {
3446
closeDrawer();
3547
form.resetFields();
3648
};
3749

38-
async function getRoleOptions() {
39-
const { error, data } = await fetchGetAllRoles();
40-
41-
if (!error) {
42-
const options = data.map(item => ({
43-
label: item.roleName,
44-
value: item.roleCode
45-
}));
46-
47-
// end
48-
setRoleOptions(options);
49-
}
50-
}
51-
5250
async function handleSubmit() {
53-
const data = await form.validateFields();
54-
console.log(data);
51+
const res = await form.validateFields();
52+
console.log(res);
5553

5654
// request
5755
window.$message?.success(t('common.updateSuccess'));
@@ -61,7 +59,7 @@ const UserOperateDrawer: FC<Props> = ({ open, closeDrawer, submitted, operateTyp
6159

6260
useUpdateEffect(() => {
6361
if (open) {
64-
getRoleOptions();
62+
run();
6563
}
6664
}, [open]);
6765

src/types/antd.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ declare namespace AntDesign {
3636
type AntDesignTableConfig<A extends TableApiFn> = Pick<
3737
import('@sa/hooks').TableConfig<A, GetTableData<A>, TableColumn<TableDataWithIndex<GetTableData<A>>>>,
3838
'apiFn' | 'apiParams' | 'columns' | 'immediate'
39-
>;
39+
> & { rowKey?: keyof GetTableData<A> };
4040
}

src/types/auto-imports.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ declare global {
1717
const AInput: typeof import('antd')['Input']
1818
const AMenu: typeof import('antd')['Menu']
1919
const AModal: typeof import('antd')['Modal']
20+
const APopconfirm: typeof import('antd')['Popconfirm']
2021
const APopover: typeof import('antd')['Popover']
2122
const ARow: typeof import('antd')['Row']
2223
const ASpace: typeof import('antd')['Space']
2324
const AStatistic: typeof import('antd')['Statistic']
2425
const ASwitch: typeof import('antd')['Switch']
26+
const ATable: typeof import('antd')['Table']
27+
const ATag: typeof import('antd')['Tag']
2528
const ATooltip: typeof import('antd')['Tooltip']
2629
const AWatermark: typeof import('antd')['Watermark']
2730
const AppProvider: typeof import('../components/stateful/AppProvider')['default']

0 commit comments

Comments
 (0)