Przeglądaj źródła

chore: 部分hook逻辑调整

xyh 2 lat temu
rodzic
commit
7cd3b3f3a1

+ 1 - 7
packages/app/src/hooks/usePageContext/index.ts

@@ -2,7 +2,6 @@ import {PAGE_SIZE_LIST} from '@utils';
 import {Dispatch, useCallback, useReducer} from 'react';
 import {createContext} from 'use-context-selector';
 import {useContext} from '..';
-import {produce} from 'immer';
 
 export type PageContextState = {
   page: number,
@@ -26,12 +25,7 @@ function pageReduce(state: PageContextState, action: PageContextAction): PageCon
 
   switch (type) {
     case 'EDIT_PAGE': {
-      const next = produce(state, function(draft) {
-        draft.page = action.payload?.page ?? state.page;
-        draft.pageSize = action.payload?.pageSize ?? state.pageSize;
-      });
-
-      return next;
+      return {...state, ...action.payload};
     }
     case 'RESET':
       return pageContextInit();

+ 14 - 13
packages/app/src/hooks/useQueryList/index.ts

@@ -1,13 +1,13 @@
 import {createPageContext, createSearchContext, usePage, useTableSearch} from '@hooks';
-import {BaseListResult, ListParams, OriginalListParams} from '@models';
+import {BaseListResult, GetBaseListType, ListParams, OriginalListParams} from '@models';
 import {useQuery} from '@tanstack/react-query';
 import {shallowEqual} from '@utils';
 import {useEffect, useRef, useState} from 'react';
 
-type UseQueryListResult<D> = [
+type UseQueryListResult<D extends BaseListResult> = [
   {
     count: number,
-    data: D[],
+    data: GetBaseListType<D>[],
     isFetching: boolean,
   },
   {refetch: () => void},
@@ -15,9 +15,9 @@ type UseQueryListResult<D> = [
 
 type UseQueryListOptions<
   P extends ListParams,
-  R,
+  R extends BaseListResult,
 > = {
-  queryFn: (params: P) => BaseListResult<R>,
+  queryFn: (params: P) => R,
   params: OriginalListParams<P>,
   pageContext: ReturnType<typeof createPageContext>,
   searchContext: ReturnType<typeof createSearchContext>,
@@ -25,7 +25,7 @@ type UseQueryListOptions<
 
 export function useQueryList<
   P extends ListParams,
-  R,
+  R extends BaseListResult,
 >(
   {queryFn, params, pageContext, searchContext}: UseQueryListOptions<P, R>,
 ): UseQueryListResult<R> {
@@ -34,7 +34,7 @@ export function useQueryList<
   const prevData = useRef<R[]>([]);
   const prevParams = useRef(params);
 
-  const {data = [], isFetching, refetch} = useQuery<R[]>(
+  const {data = [], isFetching, refetch} = useQuery<GetBaseListType<R>[]>(
     [queryFn.name, page, pageSize, ...Object.values(params)],
     async function() {
       /**
@@ -88,11 +88,12 @@ export function useQueryList<
     searchChange(isFetching);
   }, [isFetching, searchChange]);
 
-  return [{
-    count,
-    data,
-    isFetching,
-  },
-  {refetch},
+  return [
+    {
+      count,
+      data,
+      isFetching,
+    },
+    {refetch},
   ];
 }

+ 1 - 0
packages/app/src/models/response/index.tsx

@@ -23,6 +23,7 @@ export type BaseListData<T> = {
   navigateLastPage: number;
 };
 export type BaseListResult<T = any> = BaseResult<BaseListData<T>>;
+export type GetBaseListType<T> = T extends BaseListResult<infer T> ? T : never;
 
 export * from './user';
 export * from './department';

+ 3 - 1
packages/app/src/pages/role/table/pda-modal/hooks.ts

@@ -36,6 +36,7 @@ export function useTransfer(id: string) {
   const client = useQueryClient();
   const [{page, pageSize}] = usePage(pageContext);
   const name = useContextSection(context, state => state[0].name);
+
   useEffect(function() {
     if (!id) return;
 
@@ -76,9 +77,10 @@ export function useSubmit(
 
   function onSubmit(e: FormEvent) {
     e.preventDefault();
+
     mutate({
       id,
-      menuPda: selected.join(','),
+      menuPda: selected.filter(Boolean).join(',') + ',',
     });
   }