From 1c87fef28d3ca22b4c502ee34c188f3f200671b8 Mon Sep 17 00:00:00 2001 From: jialin Date: Wed, 7 May 2025 16:32:41 +0800 Subject: [PATCH] fix: fetch list page more than total page --- src/hooks/use-table-fetch.ts | 31 +++++++++++++++-- .../llmodels/components/advance-config.tsx | 33 +++---------------- src/pages/llmodels/index.tsx | 23 +++++++++++++ 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/hooks/use-table-fetch.ts b/src/hooks/use-table-fetch.ts index f58adda3..3f988e96 100644 --- a/src/hooks/use-table-fetch.ts +++ b/src/hooks/use-table-fetch.ts @@ -35,11 +35,13 @@ export default function useTableFetch(options: { loading: boolean; loadend: boolean; total: number; + totalPage: number; }>({ dataList: [...defaultData], loading: false, loadend: false, - total: 0 + total: 0, + totalPage: 0 }); const [queryParams, setQueryParams] = useState({ page: 1, @@ -55,6 +57,7 @@ export default function useTableFetch(options: { setDataSource((pre) => { return { total: pre.total, + totalPage: pre.totalPage, loading: false, loadend: true, dataList: list, @@ -97,11 +100,32 @@ export default function useTableFetch(options: { }; const res = await fetchAPI(params); + if ( + !res.items.length && + params.page > res.pagination.totalPage && + res.pagination.totalPage > 0 + ) { + const newParams = { + ...params, + page: res.pagination.totalPage + }; + const newRes = await fetchAPI(newParams); + setDataSource({ + dataList: newRes.items || [], + loading: false, + loadend: true, + total: newRes.pagination.total, + totalPage: newRes.pagination.totalPage + }); + return; + } + setDataSource({ dataList: res.items || [], loading: false, loadend: true, - total: res.pagination.total + total: res.pagination.total, + totalPage: res.pagination.totalPage }); } catch (error) { console.log('error', error); @@ -109,7 +133,8 @@ export default function useTableFetch(options: { dataList: [], loading: false, loadend: true, - total: dataSource.total + total: dataSource.total, + totalPage: dataSource.totalPage }); } }; diff --git a/src/pages/llmodels/components/advance-config.tsx b/src/pages/llmodels/components/advance-config.tsx index 27ae63cd..bbf1b695 100644 --- a/src/pages/llmodels/components/advance-config.tsx +++ b/src/pages/llmodels/components/advance-config.tsx @@ -170,13 +170,13 @@ const AdvanceConfig: React.FC = (props) => { onValuesChange?.({}, form.getFieldsValue()); }; - const onSelectorChange = (field: string) => { + const onSelectorChange = (field: string, allowEmpty?: boolean) => { const workerSelector = form.getFieldValue(field); // check if all keys have values const hasEmptyValue = _.some(_.keys(workerSelector), (k: string) => { return !workerSelector[k]; }); - if (!hasEmptyValue) { + if (!hasEmptyValue || allowEmpty) { onValuesChange?.({}, form.getFieldsValue()); } }; @@ -190,7 +190,7 @@ const AdvanceConfig: React.FC = (props) => { }; const handleEnvSelectorOnBlur = () => { - onSelectorChange('env'); + onSelectorChange('env', true); }; const handleDeleteEnvSelector = (index: number) => { @@ -428,32 +428,7 @@ const AdvanceConfig: React.FC = (props) => { } > - - name="env" - rules={[ - () => ({ - validator(rule, value) { - if (_.keys(value).length > 0) { - if (_.some(_.keys(value), (k: string) => !value[k])) { - return Promise.reject( - intl.formatMessage( - { - id: 'common.validate.value' - }, - { - name: intl.formatMessage({ - id: 'common.text.variable' - }) - } - ) - ); - } - } - return Promise.resolve(); - } - }) - ]} - > + name="env"> { const res: any = await queryModelsList(params, { cancelToken: axiosToken.token }); + + if ( + !res.items.length && + params.page > res.pagination.totalPage && + res.pagination.totalPage > 0 + ) { + const newParams = { + ...params, + page: res.pagination.totalPage + }; + const newRes: any = await queryModelsList(newParams, { + cancelToken: axiosToken.token + }); + setDataSource({ + dataList: newRes.items || [], + loading: false, + loadend: true, + total: newRes.pagination.total, + deletedIds: [] + }); + return; + } + setDataSource({ dataList: res.items || [], loading: false,