Преглед изворни кода

♻️ refactor(contracts): update react-query usage to v4 syntax

- refactor useQuery calls to use object syntax instead of positional arguments
- fix fetchContracts function definition by removing unnecessary arrow function wrapper
- improve pagination state update by using functional update form for setPagination
yourname пре 8 месеци
родитељ
комит
e5042b89bb
1 измењених фајлова са 23 додато и 28 уклоњено
  1. 23 28
      src/client/admin/pages/Contracts.tsx

+ 23 - 28
src/client/admin/pages/Contracts.tsx

@@ -20,41 +20,36 @@ const Contracts: React.FC = () => {
   const queryClient = useQueryClient();
   
   // 获取客户列表
-  const { data: clientsData } = useQuery(
-    ['clients'],
-    async () => {
+  const { data: clientsData } = useQuery({
+    queryKey: ['clients'],
+    queryFn: async () => {
       const response = await clientClient.$get({ query: { page: 1, pageSize: 1000 } });
       if (response.status !== 200) throw new Error('Failed to fetch clients');
       return response.json() as Promise<InferResponseType<typeof clientClient.$get, 200>>;
     },
-    {
-      onSuccess: (result) => {
-        setClients(result.data);
-      },
-    }
-  );
+    onSuccess: (result) => {
+      setClients(result.data);
+    },
+  });
   
   // 获取合同列表数据
-  const fetchContracts = ({ page, pageSize }: { page: number; pageSize: number }): Promise<HetongListResponse> => 
-    async () => {
-      const response = await hetongClient.$get({ query: { page, pageSize, keyword: searchText } });
-      if (response.status !== 200) throw new Error('Failed to fetch contracts');
-      return response.json() as Promise<HetongListResponse>;
-    };
+  const fetchContracts = async ({ page, pageSize }: { page: number; pageSize: number }): Promise<HetongListResponse> => {
+    const response = await hetongClient.$get({ query: { page, pageSize, keyword: searchText } });
+    if (response.status !== 200) throw new Error('Failed to fetch contracts');
+    return response.json() as Promise<HetongListResponse>;
+  };
   
-  const { data, isLoading: loading, refetch } = useQuery(
-    ['contracts', pagination.current, pagination.pageSize, searchText],
-    () => fetchContracts({ page: pagination.current, pageSize: pagination.pageSize }),
-    {
-      onSuccess: (result: HetongListResponse) => {
-        setDataSource(result.data);
-        setPagination({
-          ...pagination,
-          total: result.pagination.total,
-        });
-      },
-    }
-  );
+  const { data, isLoading: loading, refetch } = useQuery({
+    queryKey: ['contracts', pagination.current, pagination.pageSize, searchText],
+    queryFn: () => fetchContracts({ page: pagination.current, pageSize: pagination.pageSize }),
+    onSuccess: (result: HetongListResponse) => {
+      setDataSource(result.data);
+      setPagination(prev => ({
+        ...prev,
+        total: result.pagination.total,
+      }));
+    },
+  });
   
   const [dataSource, setDataSource] = useState<HetongItem[]>([]);
   const [pagination, setPagination] = useState({