|
@@ -1,12 +1,15 @@
|
|
|
-import {useContextSection, useQueryTableList} from '@hooks';
|
|
|
+import {useContextSection, usePage, useTableSearchState} from '@hooks';
|
|
|
import {context, pageContext, searchContext} from '../context';
|
|
|
-
|
|
|
import {getDictionaryList} from '@apis';
|
|
|
import {DictionaryData, DictionaryParamsType} from '@models';
|
|
|
import {Button} from 'antd';
|
|
|
import {ColumnsType} from 'antd/es/table';
|
|
|
-import {useState} from 'react';
|
|
|
+import {useEffect, useRef, useState} from 'react';
|
|
|
import {useBoolean} from 'ahooks';
|
|
|
+import {shallowEqual} from '@utils';
|
|
|
+import {useQuery} from '@tanstack/react-query';
|
|
|
+
|
|
|
+type MaterialType = DictionaryData & {count: number};
|
|
|
|
|
|
export function useList() {
|
|
|
const params = useContextSection(
|
|
@@ -16,12 +19,73 @@ export function useList() {
|
|
|
},
|
|
|
);
|
|
|
|
|
|
- return useQueryTableList({
|
|
|
- queryFn: getDictionaryList,
|
|
|
- params,
|
|
|
- pageContext,
|
|
|
- searchContext,
|
|
|
- });
|
|
|
+ const [count, setCount] = useState(0);
|
|
|
+ const [{page, pageSize}, {onCurrentPageChange}] = usePage(pageContext);
|
|
|
+ const prevData = useRef<MaterialType[]>([]);
|
|
|
+ const prevParams = useRef(params);
|
|
|
+
|
|
|
+ const {data = [], isFetching, refetch} = useQuery<MaterialType[]>(
|
|
|
+ [getDictionaryList.name, page, pageSize, ...Object.values(params)],
|
|
|
+ async function() {
|
|
|
+ /**
|
|
|
+ * 当参数发生改变说明请求内容发生变化
|
|
|
+ * 如果当前页码不为1需要跳转到第1页查询
|
|
|
+ */
|
|
|
+ if (page !== 1 && !shallowEqual(prevParams.current, params)) {
|
|
|
+ prevParams.current = params;
|
|
|
+ onCurrentPageChange(1);
|
|
|
+ return prevData.current;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 有可能参数发生变化但是页码为1 第一步不会拦截
|
|
|
+ * 这里确保参数是最新的参数
|
|
|
+ */
|
|
|
+ prevParams.current = params;
|
|
|
+
|
|
|
+ const data = await getDictionaryList({
|
|
|
+ ...params,
|
|
|
+ page: page.toString(),
|
|
|
+ limit: pageSize.toString(),
|
|
|
+ });
|
|
|
+
|
|
|
+ if (data.msg === '200') {
|
|
|
+ const {data: {total, list}, amount} = data;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当页面只有一个数据的时候 删除了页码改为前一个 但是数据请求还是当前页码
|
|
|
+ * 需要判断当前是否无数据并且页码大于1 满足时跳转到前一个页面
|
|
|
+ */
|
|
|
+ page > 1 && list.length === 0 && onCurrentPageChange(page - 1);
|
|
|
+
|
|
|
+ setCount(total);
|
|
|
+
|
|
|
+ const arr = list.map<MaterialType>(function(val) {
|
|
|
+ return {
|
|
|
+ ...val,
|
|
|
+ count: amount[val.tldId] ?? 0,
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ return (prevData.current = arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ return [];
|
|
|
+ },
|
|
|
+ {
|
|
|
+ placeholderData: [],
|
|
|
+ keepPreviousData: true,
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ const [, searchChange] = useTableSearchState(searchContext);
|
|
|
+
|
|
|
+ // 保持更新搜索表格的查询状态
|
|
|
+ useEffect(function() {
|
|
|
+ searchChange(isFetching);
|
|
|
+ }, [isFetching, searchChange]);
|
|
|
+
|
|
|
+ return [{data, count}, {refetch}] as const;
|
|
|
}
|
|
|
|
|
|
export function useEdit() {
|
|
@@ -41,9 +105,10 @@ export function useEdit() {
|
|
|
export function useHandle() {
|
|
|
const [{visible, editId}, {onClose, onEdit}] = useEdit();
|
|
|
|
|
|
- const columns: ColumnsType<DictionaryData> = [
|
|
|
+ const columns: ColumnsType<MaterialType> = [
|
|
|
{title: '物料名称', dataIndex: 'name', key: 'name'},
|
|
|
{title: '物料编号', dataIndex: 'code', key: 'code'},
|
|
|
+ {title: '物料库存', dataIndex: 'count', key: 'count'},
|
|
|
{title: '物料类别', dataIndex: 'partType', key: 'partType'},
|
|
|
{title: '物料类型', dataIndex: 'materialType', key: 'materialType'},
|
|
|
{title: '存储容量', dataIndex: 'size', key: 'size'},
|