|
|
@@ -5,20 +5,23 @@ import {useLatest} from 'ahooks';
|
|
|
import {useMemo, useState} from 'react';
|
|
|
import {debounce} from 'lodash-es';
|
|
|
|
|
|
-function useQueryOptions<T extends {id: number | string}>(options: {
|
|
|
- fn: () => BaseResult<T[]>;
|
|
|
+function useQueryOptions<T extends {id: number | string}, P extends unknown[]>(options: {
|
|
|
+ fn: (...args: P) => BaseResult<T[]>;
|
|
|
findName: (state: T) => string;
|
|
|
addAll: boolean;
|
|
|
findValue?: (state: T) => string;
|
|
|
+ params: P;
|
|
|
}) {
|
|
|
- const {findValue, fn, findName, addAll} = options ?? {};
|
|
|
+ const {findValue, fn, findName, addAll, params} = options ?? {};
|
|
|
|
|
|
const findValueFn = useLatest(findValue);
|
|
|
|
|
|
- const {data} = useQuery(
|
|
|
- [fn.name, addAll, findValueFn],
|
|
|
- async function () {
|
|
|
- const data = await fn();
|
|
|
+ const {data} = useQuery({
|
|
|
+ queryKey: [fn.name, addAll, findValueFn, ...Object.values(params)],
|
|
|
+ async queryFn({signal}) {
|
|
|
+ params.push(signal);
|
|
|
+
|
|
|
+ const data = await fn(...params);
|
|
|
|
|
|
if (data.msg === '200') {
|
|
|
const list = data.data.map(function (value) {
|
|
|
@@ -35,51 +38,33 @@ function useQueryOptions<T extends {id: number | string}>(options: {
|
|
|
|
|
|
return [];
|
|
|
},
|
|
|
- {initialData: [], cacheTime: 1000 * 60 * 10},
|
|
|
- );
|
|
|
+ initialData: [],
|
|
|
+ cacheTime: 1000 * 60 * 10,
|
|
|
+ });
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
-export function useStorageOptions(addAll = false, findValue?: (state: StorageListData) => string) {
|
|
|
+export function useStorageOptions(
|
|
|
+ addAll = false,
|
|
|
+ findValue?: (state: StorageListData) => string,
|
|
|
+ options: {id?: string} = {},
|
|
|
+) {
|
|
|
return useQueryOptions({
|
|
|
fn: getAllStorage,
|
|
|
findName: state => state.storageLocationName,
|
|
|
addAll,
|
|
|
findValue,
|
|
|
+ params: [options.id ?? ''],
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-export function useStorageOptionsWithWarehouse(warehouseId: string) {
|
|
|
- const {data} = useQuery(
|
|
|
- [getAllStorage.name, 'withWarehouse', warehouseId],
|
|
|
- async function () {
|
|
|
- const data = await getAllStorage(warehouseId);
|
|
|
-
|
|
|
- if (data.msg === '200') {
|
|
|
- const list = data.data.map(function (value) {
|
|
|
- return {
|
|
|
- label: value.storageLocationName,
|
|
|
- value: value.storageLocationCode,
|
|
|
- };
|
|
|
- });
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- return [];
|
|
|
- },
|
|
|
- {enabled: Boolean(warehouseId), initialData: []},
|
|
|
- );
|
|
|
-
|
|
|
- return data;
|
|
|
-}
|
|
|
-
|
|
|
export function useRoleOptions(addAll = false) {
|
|
|
return useQueryOptions({
|
|
|
fn: getAllRoleList,
|
|
|
findName: state => state.roleName,
|
|
|
addAll,
|
|
|
+ params: [],
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -88,10 +73,10 @@ export function useDictionaryOptions(
|
|
|
addAll = false,
|
|
|
findValue: (state: DictionaryData) => string = state => state.tldId,
|
|
|
) {
|
|
|
- const {data} = useQuery(
|
|
|
- [getDictionaryOptions.name, type, addAll],
|
|
|
- async function () {
|
|
|
- const data = await getDictionaryOptions(type);
|
|
|
+ const {data} = useQuery({
|
|
|
+ queryKey: [getDictionaryOptions.name, type, addAll],
|
|
|
+ async queryFn({signal}) {
|
|
|
+ const data = await getDictionaryOptions(type, '', '', signal);
|
|
|
|
|
|
if (data.msg === '200') {
|
|
|
const list = data.data.map(function (value) {
|
|
|
@@ -105,17 +90,18 @@ export function useDictionaryOptions(
|
|
|
|
|
|
return [];
|
|
|
},
|
|
|
- {initialData: [], cacheTime: 1000 * 60 * 10},
|
|
|
- );
|
|
|
+ initialData: [],
|
|
|
+ cacheTime: 1000 * 60 * 10,
|
|
|
+ });
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
export function useUserOptions(addAll = false) {
|
|
|
- const {data} = useQuery(
|
|
|
- [getAllUser.name, addAll],
|
|
|
- async function () {
|
|
|
- const data = await getAllUser();
|
|
|
+ const {data} = useQuery({
|
|
|
+ queryKey: [getAllUser.name, addAll],
|
|
|
+ async queryFn({signal}) {
|
|
|
+ const data = await getAllUser(signal);
|
|
|
|
|
|
if (data.msg === '200') {
|
|
|
const list = data.data.map(function (value) {
|
|
|
@@ -129,8 +115,9 @@ export function useUserOptions(addAll = false) {
|
|
|
|
|
|
return [];
|
|
|
},
|
|
|
- {initialData: [], cacheTime: 1000 * 60 * 10},
|
|
|
- );
|
|
|
+ initialData: [],
|
|
|
+ cacheTime: 1000 * 60 * 10,
|
|
|
+ });
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
@@ -147,29 +134,23 @@ export function useDictionaryWidthCode(
|
|
|
) {
|
|
|
const [code, setCode] = useState('');
|
|
|
|
|
|
- const {data, isFetching} = useQuery(
|
|
|
- [getDictionaryOptions.name, type, addAll, 'useDictionaryWidthCode', code],
|
|
|
- async function () {
|
|
|
- const data = await getDictionaryOptions(type, '', code);
|
|
|
-
|
|
|
+ const {data, isFetching} = useQuery({
|
|
|
+ queryKey: [getDictionaryOptions.name, type, addAll, 'useDictionaryWidthCode', code],
|
|
|
+ async queryFn({signal}) {
|
|
|
+ const data = await getDictionaryOptions(type, '', code, signal);
|
|
|
if (data.msg === '200') {
|
|
|
const list = data.data.map(function (value) {
|
|
|
return {label: value.name, value: findValue(value)};
|
|
|
});
|
|
|
-
|
|
|
if (addAll) list.unshift({label: '全部', value: ''});
|
|
|
-
|
|
|
return list;
|
|
|
}
|
|
|
-
|
|
|
return [];
|
|
|
},
|
|
|
- {
|
|
|
- initialData: [],
|
|
|
- cacheTime: 1000 * 60 * 10,
|
|
|
- enabled: Boolean(code),
|
|
|
- },
|
|
|
- );
|
|
|
+ initialData: [],
|
|
|
+ cacheTime: 1000 * 60 * 10,
|
|
|
+ enabled: Boolean(code),
|
|
|
+ });
|
|
|
|
|
|
const onSearch = useMemo(function () {
|
|
|
function onSearchValue(value: string) {
|