|
|
@@ -1,20 +1,33 @@
|
|
|
import {getAllRoleList, getAllStorage, getDictionaryOptions} from '@apis';
|
|
|
-import {BaseResult, DictionaryParamsType} from '@models';
|
|
|
+import {BaseResult, DictionaryParamsType, StorageListData} from '@models';
|
|
|
import {useQuery} from '@tanstack/react-query';
|
|
|
+import {useMemo} from 'react';
|
|
|
|
|
|
function useQueryOptions<T extends {id: number | string}>(
|
|
|
- fn: () => BaseResult<T[]>,
|
|
|
- findName: (state: T) => string,
|
|
|
- addAll: boolean,
|
|
|
+ options: {
|
|
|
+ fn: () => BaseResult<T[]>,
|
|
|
+ findName: (state: T) => string,
|
|
|
+ addAll: boolean,
|
|
|
+ findValue?: (state: T) => string,
|
|
|
+ },
|
|
|
) {
|
|
|
+ const {findValue, fn, findName, addAll} = options ?? {};
|
|
|
+
|
|
|
+ const findValueFn = useMemo(function() {
|
|
|
+ return findValue;
|
|
|
+ }, []);
|
|
|
+
|
|
|
const {data} = useQuery(
|
|
|
- [fn.name, addAll],
|
|
|
+ [fn.name, addAll, findValueFn],
|
|
|
async function() {
|
|
|
const data = await fn();
|
|
|
|
|
|
if (data.msg === '200') {
|
|
|
const list = data.data.map(function(value) {
|
|
|
- return {label: findName(value), value: String(value.id)};
|
|
|
+ return {
|
|
|
+ label: findName(value),
|
|
|
+ value: findValueFn ? findValueFn(value) : String(value.id),
|
|
|
+ };
|
|
|
});
|
|
|
|
|
|
if (addAll)
|
|
|
@@ -31,20 +44,21 @@ function useQueryOptions<T extends {id: number | string}>(
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
-export function useStorageOptions(addAll = false) {
|
|
|
- return useQueryOptions(
|
|
|
- getAllStorage,
|
|
|
- state => state.storageLocationName,
|
|
|
+export function useStorageOptions(addAll = false, findValue?: (state: StorageListData) => string) {
|
|
|
+ return useQueryOptions({
|
|
|
+ fn: getAllStorage,
|
|
|
+ findName: state => state.storageLocationName,
|
|
|
addAll,
|
|
|
- );
|
|
|
+ findValue,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
export function useRoleOptions(addAll = false) {
|
|
|
- return useQueryOptions(
|
|
|
- getAllRoleList,
|
|
|
- state => state.roleName,
|
|
|
+ return useQueryOptions({
|
|
|
+ fn: getAllRoleList,
|
|
|
+ findName: state => state.roleName,
|
|
|
addAll,
|
|
|
- );
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
export function useDictionaryOptions(type: DictionaryParamsType, addAll = false) {
|