Skip to content

Commit 2aadeff

Browse files
authored
chore: Extract paging related functions into standalone hook (#2334)
1 parent 9d99ab7 commit 2aadeff

File tree

8 files changed

+54
-84
lines changed

8 files changed

+54
-84
lines changed

web/src/hooks/usePagination.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { useEffect, useState } from 'react';
18+
import { useLocation, history } from 'umi';
19+
import querystring from 'query-string';
20+
21+
export default function usePagination() {
22+
const location = useLocation();
23+
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
24+
useEffect(() => {
25+
const { page = 1, pageSize = 10 } = querystring.parse(location.search);
26+
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
27+
}, [location.search]);
28+
29+
const savePageList = (page = 1, pageSize = 10) => {
30+
history.replace(`${location.pathname}?page=${page}&pageSize=${pageSize}`);
31+
};
32+
33+
return { paginationConfig, savePageList };
34+
}

web/src/pages/Consumer/List.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import React, { useEffect, useRef, useState } from 'react';
17+
import React, { useRef, useState } from 'react';
1818
import { PageContainer } from '@ant-design/pro-layout';
1919
import ProTable from '@ant-design/pro-table';
2020
import type { ProColumns, ActionType } from '@ant-design/pro-table';
2121
import { Popconfirm, Button, notification } from 'antd';
2222
import { history, useIntl } from 'umi';
23+
import usePagination from '@/hooks/usePagination';
2324
import { PlusOutlined } from '@ant-design/icons';
24-
import querystring from 'query-string';
2525
import { omit } from 'lodash';
2626

2727
import { DELETE_FIELDS } from '@/constants';
@@ -37,16 +37,7 @@ const Page: React.FC = () => {
3737
const [rawData, setRawData] = useState<Record<string, any>>({});
3838
const [id, setId] = useState('');
3939
const [editorMode, setEditorMode] = useState<'create' | 'update'>('create');
40-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
41-
42-
const savePageList = (page = 1, pageSize = 10) => {
43-
history.replace(`/consumer/list?page=${page}&pageSize=${pageSize}`);
44-
};
45-
46-
useEffect(() => {
47-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
48-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
49-
}, [window.location.search]);
40+
const { paginationConfig, savePageList } = usePagination();
5041

5142
const columns: ProColumns<ConsumerModule.ResEntity>[] = [
5243
{

web/src/pages/Plugin/List.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import React, { useEffect, useRef, useState } from 'react';
1818
import { PageHeaderWrapper } from '@ant-design/pro-layout';
1919
import { history, useIntl } from 'umi';
20+
import usePagination from '@/hooks/usePagination';
2021
import ProTable from '@ant-design/pro-table';
2122
import type { ActionType, ProColumns } from '@ant-design/pro-table';
2223
import { Button, Popconfirm, Space, notification } from 'antd';
2324
import { PlusOutlined } from '@ant-design/icons';
2425
import { omit } from 'lodash';
25-
import querystring from 'query-string';
2626

2727
import PluginDetail from '@/components/Plugin/PluginDetail';
2828

@@ -35,11 +35,7 @@ const Page: React.FC = () => {
3535
const [initialData, setInitialData] = useState({});
3636
const [pluginList, setPluginList] = useState<PluginComponent.Meta[]>([]);
3737
const [name, setName] = useState('');
38-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
39-
40-
const savePageList = (page = 1, pageSize = 10) => {
41-
history.replace(`/plugin/list?page=${page}&pageSize=${pageSize}`);
42-
};
38+
const { paginationConfig, savePageList } = usePagination();
4339

4440
useEffect(() => {
4541
fetchPluginList().then(setPluginList);
@@ -57,11 +53,6 @@ const Page: React.FC = () => {
5753
}
5854
}, [name]);
5955

60-
useEffect(() => {
61-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
62-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
63-
}, [window.location.search]);
64-
6556
const columns: ProColumns<PluginModule.TransformedPlugin>[] = [
6657
{
6758
title: formatMessage({ id: 'component.global.name' }),

web/src/pages/PluginTemplate/List.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,15 @@ import ProTable from '@ant-design/pro-table';
2121
import type { ActionType, ProColumns } from '@ant-design/pro-table';
2222
import { Button, notification, Popconfirm, Select, Space, Tag } from 'antd';
2323
import { PlusOutlined } from '@ant-design/icons';
24-
import querystring from 'query-string';
25-
24+
import usePagination from '@/hooks/usePagination';
2625
import { fetchList, remove, fetchLabelList } from './service';
2726

2827
const Page: React.FC = () => {
2928
const ref = useRef<ActionType>();
3029
const [labelList, setLabelList] = useState<LabelList>({});
31-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
3230
const { formatMessage } = useIntl();
3331

34-
const savePageList = (page = 1, pageSize = 10) => {
35-
history.replace(`/plugin-template/list?page=${page}&pageSize=${pageSize}`);
36-
};
37-
38-
useEffect(() => {
39-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
40-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
41-
}, [window.location.search]);
32+
const { paginationConfig, savePageList } = usePagination();
4233

4334
useEffect(() => {
4435
fetchLabelList().then(setLabelList);

web/src/pages/Route/List.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ import {
3636
Tooltip,
3737
} from 'antd';
3838
import { history, useIntl } from 'umi';
39+
import usePagination from '@/hooks/usePagination';
3940
import { PlusOutlined, ExportOutlined, ImportOutlined, DownOutlined } from '@ant-design/icons';
4041
import { js_beautify } from 'js-beautify';
4142
import yaml from 'js-yaml';
4243
import moment from 'moment';
4344
import { saveAs } from 'file-saver';
44-
import querystring from 'query-string';
4545
import { omit } from 'lodash';
4646

4747
import { DELETE_FIELDS } from '@/constants';
4848
import { timestampToLocaleString } from '@/helpers';
4949
import type { RcFile } from 'antd/lib/upload';
50+
5051
import {
5152
update,
5253
create,
@@ -86,22 +87,13 @@ const Page: React.FC = () => {
8687
const [rawData, setRawData] = useState<Record<string, any>>({});
8788
const [id, setId] = useState('');
8889
const [editorMode, setEditorMode] = useState<'create' | 'update'>('create');
89-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
90+
const { paginationConfig, savePageList } = usePagination();
9091
const [debugDrawVisible, setDebugDrawVisible] = useState(false);
9192

92-
const savePageList = (page = 1, pageSize = 10) => {
93-
history.replace(`/routes/list?page=${page}&pageSize=${pageSize}`);
94-
};
95-
9693
useEffect(() => {
9794
fetchLabelList().then(setLabelList);
9895
}, []);
9996

100-
useEffect(() => {
101-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
102-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
103-
}, [window.location.search]);
104-
10597
const rowSelection: any = {
10698
selectedRowKeys,
10799
onChange: (currentSelectKeys: string[]) => {

web/src/pages/SSL/List.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,21 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import React, { useEffect, useRef, useState } from 'react';
17+
import React, { useRef } from 'react';
1818
import { PageHeaderWrapper } from '@ant-design/pro-layout';
1919
import ProTable from '@ant-design/pro-table';
2020
import type { ProColumns, ActionType } from '@ant-design/pro-table';
2121
import { Button, Popconfirm, notification, Tag } from 'antd';
2222
import { useIntl, history } from 'umi';
23+
import usePagination from '@/hooks/usePagination';
2324
import { PlusOutlined } from '@ant-design/icons';
24-
import querystring from 'query-string';
25-
2625
import { fetchList, remove as removeSSL } from '@/pages/SSL/service';
2726
import { timestampToLocaleString } from '@/helpers';
2827

2928
const Page: React.FC = () => {
3029
const tableRef = useRef<ActionType>();
3130
const { formatMessage } = useIntl();
32-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
33-
34-
const savePageList = (page = 1, pageSize = 10) => {
35-
history.replace(`/ssl/list?page=${page}&pageSize=${pageSize}`);
36-
};
37-
38-
useEffect(() => {
39-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
40-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
41-
}, [window.location.search]);
31+
const { paginationConfig, savePageList } = usePagination();
4232

4333
const columns: ProColumns<SSLModule.ResponseBody>[] = [
4434
{

web/src/pages/Service/List.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import React, { useEffect, useRef, useState } from 'react';
17+
import React, { useRef, useState } from 'react';
1818
import { history, useIntl } from 'umi';
19+
import usePagination from '@/hooks/usePagination';
1920
import { PageHeaderWrapper } from '@ant-design/pro-layout';
2021
import ProTable from '@ant-design/pro-table';
2122
import type { ActionType, ProColumns } from '@ant-design/pro-table';
2223
import { PlusOutlined } from '@ant-design/icons';
2324
import { Button, notification, Popconfirm, Space } from 'antd';
24-
import querystring from 'query-string';
2525
import { omit } from 'lodash';
2626

2727
import { DELETE_FIELDS } from '@/constants';
@@ -35,16 +35,7 @@ const Page: React.FC = () => {
3535
const [rawData, setRawData] = useState<Record<string, any>>({});
3636
const [id, setId] = useState('');
3737
const [editorMode, setEditorMode] = useState<'create' | 'update'>('create');
38-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
39-
40-
const savePageList = (page = 1, pageSize = 10) => {
41-
history.replace(`/service/list?page=${page}&pageSize=${pageSize}`);
42-
};
43-
44-
useEffect(() => {
45-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
46-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
47-
}, [window.location.search]);
38+
const { paginationConfig, savePageList } = usePagination();
4839

4940
const columns: ProColumns<ServiceModule.ResponseBody>[] = [
5041
{

web/src/pages/Upstream/List.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
import React, { useEffect, useRef, useState } from 'react';
17+
import React, { useRef, useState } from 'react';
1818
import { PageContainer } from '@ant-design/pro-layout';
1919
import ProTable from '@ant-design/pro-table';
2020
import type { ProColumns, ActionType } from '@ant-design/pro-table';
2121
import { Popconfirm, Button, notification, Space } from 'antd';
2222
import { history, useIntl } from 'umi';
23+
2324
import { PlusOutlined } from '@ant-design/icons';
24-
import querystring from 'query-string';
2525
import { omit } from 'lodash';
26-
26+
import usePagination from '@/hooks/usePagination';
2727
import { DELETE_FIELDS } from '@/constants';
2828

2929
import { RawDataEditor } from '@/components/RawDataEditor';
@@ -37,19 +37,9 @@ const Page: React.FC = () => {
3737
const [rawData, setRawData] = useState<Record<string, any>>({});
3838
const [id, setId] = useState('');
3939
const [editorMode, setEditorMode] = useState<'create' | 'update'>('create');
40-
const [paginationConfig, setPaginationConfig] = useState({ pageSize: 10, current: 1 });
41-
40+
const { paginationConfig, savePageList } = usePagination();
4241
const { formatMessage } = useIntl();
4342

44-
const savePageList = (page = 1, pageSize = 10) => {
45-
history.replace(`/upstream/list?page=${page}&pageSize=${pageSize}`);
46-
};
47-
48-
useEffect(() => {
49-
const { page = 1, pageSize = 10 } = querystring.parse(window.location.search);
50-
setPaginationConfig({ pageSize: Number(pageSize), current: Number(page) });
51-
}, [window.location.search]);
52-
5343
const columns: ProColumns<UpstreamModule.ResponseBody>[] = [
5444
{
5545
title: formatMessage({ id: 'page.upstream.list.id' }),

0 commit comments

Comments
 (0)