Browse Source

feat: migrate from useRequest to react-query (#11665)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
pull/11672/head
afc163 6 months ago
committed by GitHub
parent
commit
002ddde9b6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      config/config.ts
  2. 1
      package.json
  3. 2
      src/app.tsx
  4. 9
      src/pages/account/center/components/Applications/index.tsx
  5. 9
      src/pages/account/center/components/Articles/index.tsx
  6. 9
      src/pages/account/center/components/Projects/index.tsx
  7. 7
      src/pages/account/center/index.tsx
  8. 7
      src/pages/account/settings/components/base.tsx
  9. 7
      src/pages/dashboard/analysis/index.tsx
  10. 7
      src/pages/dashboard/monitor/index.tsx
  11. 20
      src/pages/dashboard/workplace/index.tsx
  12. 6
      src/pages/form/basic-form/index.tsx
  13. 35
      src/pages/list/basic-list/index.tsx
  14. 9
      src/pages/list/card-list/index.tsx
  15. 19
      src/pages/list/search/applications/index.tsx
  16. 40
      src/pages/list/search/articles/index.tsx
  17. 2
      src/pages/list/search/projects/data.d.ts
  18. 24
      src/pages/list/search/projects/index.tsx
  19. 9
      src/pages/profile/advanced/index.tsx
  20. 7
      src/pages/profile/basic/index.tsx
  21. 16
      src/pages/table-list/components/CreateForm.tsx
  22. 8
      src/pages/table-list/components/UpdateForm.tsx
  23. 7
      src/pages/table-list/index.tsx
  24. 21
      src/pages/user/register/index.tsx

6
config/config.ts

@ -145,6 +145,12 @@ export default defineConfig({
* @doc https://umijs.org/docs/max/request * @doc https://umijs.org/docs/max/request
*/ */
request: {}, request: {},
/**
* @name React Query 插件
* @description 使用 react-query 管理服务端状态
* @doc https://umijs.org/docs/max/react-query
*/
reactQuery: {},
/** /**
* @name 权限插件 * @name 权限插件
* @description 基于 initialState 的权限插件,必须先打开 initialState * @description 基于 initialState 的权限插件,必须先打开 initialState

1
package.json

@ -43,6 +43,7 @@
"@antv/l7": "^2.22.7", "@antv/l7": "^2.22.7",
"@antv/l7-react": "^2.4.3", "@antv/l7-react": "^2.4.3",
"@rc-component/util": "^1.9.0", "@rc-component/util": "^1.9.0",
"@tanstack/react-query": "^5.97.0",
"antd": "^6.2.2", "antd": "^6.2.2",
"antd-style": "^4.1.0", "antd-style": "^4.1.0",
"clsx": "^2.1.1", "clsx": "^2.1.1",

2
src/app.tsx

@ -156,7 +156,7 @@ export const layout: RunTimeLayoutConfig = ({
/** /**
* @name request 配置,可以配置错误处理 * @name request 配置,可以配置错误处理
* 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。 * 它基于 axios 提供了一套统一的网络请求和错误处理方案。
* @doc https://umijs.org/docs/max/request#配置 * @doc https://umijs.org/docs/max/request#配置
*/ */
export const request: RequestConfig = { export const request: RequestConfig = {

9
src/pages/account/center/components/Applications/index.tsx

@ -4,7 +4,7 @@ import {
EllipsisOutlined, EllipsisOutlined,
ShareAltOutlined, ShareAltOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Avatar, Card, Dropdown, List, Tooltip } from 'antd'; import { Avatar, Card, Dropdown, List, Tooltip } from 'antd';
import numeral from 'numeral'; import numeral from 'numeral';
import React from 'react'; import React from 'react';
@ -59,10 +59,9 @@ const CardInfo: React.FC<{
const Applications: React.FC = () => { const Applications: React.FC = () => {
const { styles: stylesApplications } = useStyles(); const { styles: stylesApplications } = useStyles();
// 获取tab列表数据 // 获取tab列表数据
const { data: listData } = useRequest(() => { const { data: listData } = useQuery({
return queryFakeList({ queryKey: ['applications-list', 30],
count: 30, queryFn: () => queryFakeList({ count: 30 }).then((res) => res.data),
});
}); });
return ( return (

9
src/pages/account/center/components/Articles/index.tsx

@ -1,5 +1,5 @@
import { LikeOutlined, MessageFilled, StarTwoTone } from '@ant-design/icons'; import { LikeOutlined, MessageFilled, StarTwoTone } from '@ant-design/icons';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { List, Tag } from 'antd'; import { List, Tag } from 'antd';
import React from 'react'; import React from 'react';
import type { ListItemDataType } from '../../data.d'; import type { ListItemDataType } from '../../data.d';
@ -20,10 +20,9 @@ const Articles: React.FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
// 获取tab列表数据 // 获取tab列表数据
const { data: listData } = useRequest(() => { const { data: listData } = useQuery({
return queryFakeList({ queryKey: ['articles-list', 30],
count: 30, queryFn: () => queryFakeList({ count: 30 }).then((res) => res.data),
});
}); });
return ( return (
<List<ListItemDataType> <List<ListItemDataType>

9
src/pages/account/center/components/Projects/index.tsx

@ -1,4 +1,4 @@
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Card, List } from 'antd'; import { Card, List } from 'antd';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
@ -12,10 +12,9 @@ dayjs.extend(relativeTime);
const Projects: React.FC = () => { const Projects: React.FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
// 获取tab列表数据 // 获取tab列表数据
const { data: listData } = useRequest(() => { const { data: listData } = useQuery({
return queryFakeList({ queryKey: ['projects-list', 30],
count: 30, queryFn: () => queryFakeList({ count: 30 }).then((res) => res.data),
});
}); });
return ( return (
<List<ListItemDataType> <List<ListItemDataType>

7
src/pages/account/center/index.tsx

@ -5,7 +5,7 @@ import {
PlusOutlined, PlusOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { GridContent } from '@ant-design/pro-components'; import { GridContent } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { import {
Avatar, Avatar,
Card, Card,
@ -144,8 +144,9 @@ const Center: React.FC = () => {
const [tabKey, setTabKey] = useState<tabKeyType>('articles'); const [tabKey, setTabKey] = useState<tabKeyType>('articles');
// 获取用户信息 // 获取用户信息
const { data: currentUser, loading } = useRequest(() => { const { data: currentUser, isLoading: loading } = useQuery({
return queryCurrent(); queryKey: ['current-user'],
queryFn: () => queryCurrent().then((res) => res.data),
}); });
// 渲染用户信息 // 渲染用户信息

7
src/pages/account/settings/components/base.tsx

@ -7,7 +7,7 @@ import {
ProFormText, ProFormText,
ProFormTextArea, ProFormTextArea,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Button, Input, message, Upload } from 'antd'; import { Button, Input, message, Upload } from 'antd';
import React from 'react'; import React from 'react';
import { queryCity, queryCurrent, queryProvince } from '../service'; import { queryCity, queryCurrent, queryProvince } from '../service';
@ -30,8 +30,9 @@ const validatorPhone = (
const BaseView: React.FC = () => { const BaseView: React.FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { data: currentUser, loading } = useRequest(() => { const { data: currentUser, isLoading: loading } = useQuery({
return queryCurrent(); queryKey: ['current-user'],
queryFn: () => queryCurrent().then((res) => res.data),
}); });
const getAvatarURL = () => { const getAvatarURL = () => {
if (currentUser) { if (currentUser) {

7
src/pages/dashboard/analysis/index.tsx

@ -1,6 +1,6 @@
import { EllipsisOutlined } from '@ant-design/icons'; import { EllipsisOutlined } from '@ant-design/icons';
import { GridContent } from '@ant-design/pro-components'; import { GridContent } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Col, Dropdown, Row } from 'antd'; import { Col, Dropdown, Row } from 'antd';
import type { RangePickerProps } from 'antd/es/date-picker'; import type { RangePickerProps } from 'antd/es/date-picker';
import type { Dayjs } from 'dayjs'; import type { Dayjs } from 'dayjs';
@ -31,7 +31,10 @@ const Analysis: FC<AnalysisProps> = () => {
const [rangePickerValue, setRangePickerValue] = useState<RangePickerValue>( const [rangePickerValue, setRangePickerValue] = useState<RangePickerValue>(
getTimeDistance('year'), getTimeDistance('year'),
); );
const { loading, data } = useRequest(fakeChartData); const { isLoading: loading, data } = useQuery({
queryKey: ['dashboard-analysis'],
queryFn: () => fakeChartData().then((res) => res.data),
});
const selectDate = (type: TimeType) => { const selectDate = (type: TimeType) => {
setRangePickerValue(getTimeDistance(type)); setRangePickerValue(getTimeDistance(type));
}; };

7
src/pages/dashboard/monitor/index.tsx

@ -1,6 +1,6 @@
import { Gauge, Liquid, WordCloud } from '@ant-design/plots'; import { Gauge, Liquid, WordCloud } from '@ant-design/plots';
import { GridContent } from '@ant-design/pro-components'; import { GridContent } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Card, Col, Progress, Row, Statistic } from 'antd'; import { Card, Col, Progress, Row, Statistic } from 'antd';
import numeral from 'numeral'; import numeral from 'numeral';
import type { FC } from 'react'; import type { FC } from 'react';
@ -13,7 +13,10 @@ const deadline = Date.now() + 1000 * 60 * 60 * 24 * 2 + 1000 * 30; // Moment is
const Monitor: FC = () => { const Monitor: FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { loading, data } = useRequest(queryTags); const { isLoading: loading, data } = useQuery({
queryKey: ['monitor-tags'],
queryFn: () => queryTags().then((res) => res.data),
});
const wordCloudData = (data?.list || []).map((item) => { const wordCloudData = (data?.list || []).map((item) => {
return { return {
id: +Date.now(), id: +Date.now(),

20
src/pages/dashboard/workplace/index.tsx

@ -1,6 +1,7 @@
import { Radar } from '@ant-design/plots'; import { Radar } from '@ant-design/plots';
import { PageContainer } from '@ant-design/pro-components'; import { PageContainer } from '@ant-design/pro-components';
import { Link, useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Link } from '@umijs/max';
import { Avatar, Card, Col, List, Row, Skeleton, Statistic } from 'antd'; import { Avatar, Card, Col, List, Row, Skeleton, Statistic } from 'antd';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
@ -90,11 +91,18 @@ const ExtraContent: FC<Record<string, any>> = () => {
}; };
const Workplace: FC = () => { const Workplace: FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { loading: projectLoading, data: projectNotice = [] } = const { isLoading: projectLoading, data: projectNotice = [] } = useQuery({
useRequest(queryProjectNotice); queryKey: ['project-notice'],
const { loading: activitiesLoading, data: activities = [] } = queryFn: () => queryProjectNotice().then((res) => res.data),
useRequest(queryActivities); });
const { data } = useRequest(fakeChartData); const { isLoading: activitiesLoading, data: activities = [] } = useQuery({
queryKey: ['activities'],
queryFn: () => queryActivities().then((res) => res.data),
});
const { data } = useQuery({
queryKey: ['workplace-chart'],
queryFn: () => fakeChartData().then((res) => res.data),
});
const renderActivities = (item: ActivitiesType) => { const renderActivities = (item: ActivitiesType) => {
const events = item.template.split(/@\{([^{}]*)\}/gi).map((key) => { const events = item.template.split(/@\{([^{}]*)\}/gi).map((key) => {
if (item[key as keyof ActivitiesType]) { if (item[key as keyof ActivitiesType]) {

6
src/pages/form/basic-form/index.tsx

@ -9,7 +9,7 @@ import {
ProFormText, ProFormText,
ProFormTextArea, ProFormTextArea,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useMutation } from '@tanstack/react-query';
import { Card, message } from 'antd'; import { Card, message } from 'antd';
import type { FC } from 'react'; import type { FC } from 'react';
import { fakeSubmitForm } from './service'; import { fakeSubmitForm } from './service';
@ -17,8 +17,8 @@ import useStyles from './style.style';
const BasicForm: FC<Record<string, any>> = () => { const BasicForm: FC<Record<string, any>> = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { run } = useRequest(fakeSubmitForm, { const { mutate: run } = useMutation({
manual: true, mutationFn: fakeSubmitForm,
onSuccess: () => { onSuccess: () => {
message.success('提交成功'); message.success('提交成功');
}, },

35
src/pages/list/basic-list/index.tsx

@ -1,6 +1,6 @@
import { DownOutlined, PlusOutlined } from '@ant-design/icons'; import { DownOutlined, PlusOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-components'; import { PageContainer } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useMutation, useQuery } from '@tanstack/react-query';
import { import {
Avatar, Avatar,
Button, Button,
@ -78,17 +78,13 @@ export const BasicList: FC = () => {
const [current, setCurrent] = useState< const [current, setCurrent] = useState<
Partial<BasicListItemDataType> | undefined Partial<BasicListItemDataType> | undefined
>(undefined); >(undefined);
const { const { data: listData, isLoading: loading } = useQuery({
data: listData, queryKey: ['basic-list'],
loading, queryFn: () => queryFakeList({ count: 50 }).then((res) => res.data),
mutate,
} = useRequest(() => {
return queryFakeList({
count: 50,
});
}); });
const { run: postRun } = useRequest(
(method, params) => { const { mutate: postRun } = useMutation({
mutationFn: async ({ method, params }: { method: string; params: any }) => {
if (method === 'remove') { if (method === 'remove') {
return removeFakeList(params); return removeFakeList(params);
} }
@ -97,13 +93,12 @@ export const BasicList: FC = () => {
} }
return addFakeList(params); return addFakeList(params);
}, },
{ });
manual: true,
onSuccess: (result) => { // Wrapper to handle the original calling convention
mutate(result); const runListOperation = (method: string, params: any) => {
}, postRun({ method, params });
}, };
);
const list = listData?.list || []; const list = listData?.list || [];
const paginationProps = { const paginationProps = {
showSizeChanger: true, showSizeChanger: true,
@ -116,7 +111,7 @@ export const BasicList: FC = () => {
setCurrent(item); setCurrent(item);
}; };
const deleteItem = (id: string) => { const deleteItem = (id: string) => {
postRun('remove', { runListOperation('remove', {
id, id,
}); });
}; };
@ -187,7 +182,7 @@ export const BasicList: FC = () => {
const handleSubmit = (values: BasicListItemDataType) => { const handleSubmit = (values: BasicListItemDataType) => {
setDone(true); setDone(true);
const method = values?.id ? 'update' : 'add'; const method = values?.id ? 'update' : 'add';
postRun(method, values); runListOperation(method, values);
}; };
return ( return (
<div> <div>

9
src/pages/list/card-list/index.tsx

@ -1,6 +1,6 @@
import { PlusOutlined } from '@ant-design/icons'; import { PlusOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-components'; import { PageContainer } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Button, Card, List, Typography } from 'antd'; import { Button, Card, List, Typography } from 'antd';
import type { CardListItemDataType } from './data.d'; import type { CardListItemDataType } from './data.d';
import { queryFakeList } from './service'; import { queryFakeList } from './service';
@ -9,10 +9,9 @@ import useStyles from './style.style';
const { Paragraph } = Typography; const { Paragraph } = Typography;
const CardList = () => { const CardList = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { data, loading } = useRequest(() => { const { data, isLoading: loading } = useQuery({
return queryFakeList({ queryKey: ['card-list'],
count: 8, queryFn: () => queryFakeList({ count: 8 }).then((res) => res.data),
});
}); });
const list = data?.list || []; const list = data?.list || [];
const content = ( const content = (

19
src/pages/list/search/applications/index.tsx

@ -4,7 +4,7 @@ import {
EllipsisOutlined, EllipsisOutlined,
ShareAltOutlined, ShareAltOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { import {
Avatar, Avatar,
Card, Card,
@ -79,13 +79,20 @@ const CardInfo: React.FC<{
}; };
export const Applications: FC<Record<string, any>> = () => { export const Applications: FC<Record<string, any>> = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { data, loading, run } = useRequest((values: any) => { const {
console.log('form data', values); data,
return queryFakeList({ isLoading: loading,
count: 8, refetch,
}); } = useQuery({
queryKey: ['search-applications'],
queryFn: () => queryFakeList({ count: 8 }).then((res) => res.data),
}); });
const run = (values: any) => {
console.log('form data', values);
refetch();
};
const list = data?.list || []; const list = data?.list || [];
return ( return (

40
src/pages/list/search/articles/index.tsx

@ -4,11 +4,11 @@ import {
MessageOutlined, MessageOutlined,
StarOutlined, StarOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Button, Card, Col, Form, List, Row, Select, Tag } from 'antd'; import { Button, Card, Col, Form, List, Row, Select, Tag } from 'antd';
import type { DefaultOptionType } from 'antd/es/select'; import type { DefaultOptionType } from 'antd/es/select';
import type { FC } from 'react'; import type { FC } from 'react';
import React, { useMemo } from 'react'; import React, { useMemo, useRef } from 'react';
import { categoryOptions } from '../../mock'; import { categoryOptions } from '../../mock';
import ArticleListContent from './components/ArticleListContent'; import ArticleListContent from './components/ArticleListContent';
import StandardFormRow from './components/StandardFormRow'; import StandardFormRow from './components/StandardFormRow';
@ -54,19 +54,33 @@ const IconText: React.FC<{
const Articles: FC = () => { const Articles: FC = () => {
const [form] = Form.useForm(); const [form] = Form.useForm();
const { styles } = useStyles(); const { styles } = useStyles();
const filtersRef = useRef<{
category?: (string | number)[];
owner?: string[];
}>({});
const { data, reload, loading, loadMore, loadingMore } = useRequest( const {
() => { data,
return queryFakeList({ isLoading: loading,
count: pageSize, isFetching,
}); refetch,
}, } = useQuery({
{ queryKey: ['search-articles', pageSize, filtersRef.current],
loadMore: true, queryFn: () =>
}, queryFakeList({ count: pageSize, ...filtersRef.current }).then(
); (res) => res.data,
),
});
const loadMore = () => {
refetch();
};
const loadingMore = isFetching;
const reload = () => {
filtersRef.current = form.getFieldsValue();
refetch();
};
const list = data?.list || []; const list = data?.list || [];

2
src/pages/list/search/projects/data.d.ts

@ -6,6 +6,8 @@ export type Member = {
export interface Params { export interface Params {
count: number; count: number;
category?: (string | number)[];
author?: string;
} }
export interface ListItemDataType { export interface ListItemDataType {
id: string; id: string;

24
src/pages/list/search/projects/index.tsx

@ -1,8 +1,9 @@
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Card, Col, Form, List, Row, Select, Typography } from 'antd'; import { Card, Col, Form, List, Row, Select, Typography } from 'antd';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; import relativeTime from 'dayjs/plugin/relativeTime';
import type { FC } from 'react'; import type { FC } from 'react';
import { useState } from 'react';
import { categoryOptions } from '../../mock'; import { categoryOptions } from '../../mock';
import AvatarList from './components/AvatarList'; import AvatarList from './components/AvatarList';
import StandardFormRow from './components/StandardFormRow'; import StandardFormRow from './components/StandardFormRow';
@ -18,12 +19,23 @@ const { Paragraph } = Typography;
const getKey = (id: string, index: number) => `${id}-${index}`; const getKey = (id: string, index: number) => `${id}-${index}`;
const Projects: FC = () => { const Projects: FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { data, loading, run } = useRequest((values: any) => { const [filters, setFilters] = useState<{
console.log('form data', values); category?: (string | number)[];
return queryFakeList({ author?: string;
count: 8, }>({});
}); const {
data,
isLoading: loading,
refetch,
} = useQuery({
queryKey: ['search-projects', filters],
queryFn: () =>
queryFakeList({ count: 8, ...filters }).then((res) => res.data),
}); });
const run = (values: any) => {
setFilters(values);
};
const list = data?.list || []; const list = data?.list || [];
const cardList = list && ( const cardList = list && (
<List<ListItemDataType> <List<ListItemDataType>

9
src/pages/profile/advanced/index.tsx

@ -9,7 +9,7 @@ import {
PageContainer, PageContainer,
RouteContext, RouteContext,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { import {
Badge, Badge,
Button, Button,
@ -271,9 +271,10 @@ const Advanced: FC = () => {
return dot; return dot;
}; };
const { data = {}, loading } = useRequest<{ const { data = {}, isLoading: loading } = useQuery<AdvancedProfileData>({
data: AdvancedProfileData; queryKey: ['profile-advanced'],
}>(queryAdvancedProfile); queryFn: () => queryAdvancedProfile().then((res) => res.data),
});
const { advancedOperation1, advancedOperation2, advancedOperation3 } = data; const { advancedOperation1, advancedOperation2, advancedOperation3 } = data;
const contentList = { const contentList = {
tab1: ( tab1: (

7
src/pages/profile/basic/index.tsx

@ -1,6 +1,6 @@
import type { ProColumns } from '@ant-design/pro-components'; import type { ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components'; import { PageContainer, ProTable } from '@ant-design/pro-components';
import { useRequest } from '@umijs/max'; import { useQuery } from '@tanstack/react-query';
import { Badge, Card, Descriptions, Divider } from 'antd'; import { Badge, Card, Descriptions, Divider } from 'antd';
import type { FC } from 'react'; import type { FC } from 'react';
import React from 'react'; import React from 'react';
@ -43,8 +43,9 @@ const progressColumns: ProColumns<BasicProgress>[] = [
]; ];
const Basic: FC = () => { const Basic: FC = () => {
const { styles } = useStyles(); const { styles } = useStyles();
const { data, loading } = useRequest(() => { const { data, isLoading: loading } = useQuery({
return queryBasicProfile(); queryKey: ['profile-basic'],
queryFn: () => queryBasicProfile().then((res) => res.data),
}); });
const { basicGoods, basicProgress } = data || { const { basicGoods, basicProgress } = data || {
basicGoods: [], basicGoods: [],

16
src/pages/table-list/components/CreateForm.tsx

@ -5,7 +5,8 @@ import {
ProFormText, ProFormText,
ProFormTextArea, ProFormTextArea,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max'; import { useMutation } from '@tanstack/react-query';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Button, message } from 'antd'; import { Button, message } from 'antd';
import type { FC } from 'react'; import type { FC } from 'react';
import { addRule } from '@/services/ant-design-pro/api'; import { addRule } from '@/services/ant-design-pro/api';
@ -24,8 +25,8 @@ const CreateForm: FC<CreateFormProps> = (props) => {
* */ * */
const intl = useIntl(); const intl = useIntl();
const { run, loading } = useRequest(addRule, { const { mutateAsync: run, isPending: loading } = useMutation({
manual: true, mutationFn: addRule,
onSuccess: () => { onSuccess: () => {
messageApi.success('Added successfully'); messageApi.success('Added successfully');
reload?.(); reload?.();
@ -51,9 +52,12 @@ const CreateForm: FC<CreateFormProps> = (props) => {
width="400px" width="400px"
modalProps={{ okButtonProps: { loading } }} modalProps={{ okButtonProps: { loading } }}
onFinish={async (value) => { onFinish={async (value) => {
await run({ data: value as API.RuleListItem }); try {
await run({ data: value as API.RuleListItem });
return true; return true;
} catch {
return false;
}
}} }}
> >
<ProFormText <ProFormText

8
src/pages/table-list/components/UpdateForm.tsx

@ -6,7 +6,8 @@ import {
ProFormTextArea, ProFormTextArea,
StepsForm, StepsForm,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max'; import { useMutation } from '@tanstack/react-query';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Modal, message } from 'antd'; import { Modal, message } from 'antd';
import React, { cloneElement, useCallback, useState } from 'react'; import React, { cloneElement, useCallback, useState } from 'react';
import { updateRule } from '@/services/ant-design-pro/api'; import { updateRule } from '@/services/ant-design-pro/api';
@ -34,8 +35,8 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
const [messageApi, contextHolder] = message.useMessage(); const [messageApi, contextHolder] = message.useMessage();
const { run } = useRequest(updateRule, { const { mutateAsync: run } = useMutation({
manual: true, mutationFn: updateRule,
onSuccess: () => { onSuccess: () => {
messageApi.success('Configuration is successful'); messageApi.success('Configuration is successful');
onOk?.(); onOk?.();
@ -56,7 +57,6 @@ const UpdateForm: React.FC<UpdateFormProps> = (props) => {
const onFinish = useCallback( const onFinish = useCallback(
async (values?: any) => { async (values?: any) => {
await run({ data: values }); await run({ data: values });
onCancel(); onCancel();
}, },
[onCancel, run], [onCancel, run],

7
src/pages/table-list/index.tsx

@ -9,7 +9,8 @@ import {
ProDescriptions, ProDescriptions,
ProTable, ProTable,
} from '@ant-design/pro-components'; } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max'; import { useMutation } from '@tanstack/react-query';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Button, Drawer, type FormInstance, Input, message } from 'antd'; import { Button, Drawer, type FormInstance, Input, message } from 'antd';
import React, { useCallback, useRef, useState } from 'react'; import React, { useCallback, useRef, useState } from 'react';
import { removeRule, rule } from '@/services/ant-design-pro/api'; import { removeRule, rule } from '@/services/ant-design-pro/api';
@ -31,8 +32,8 @@ const TableList: React.FC = () => {
const [messageApi, contextHolder] = message.useMessage(); const [messageApi, contextHolder] = message.useMessage();
const { run: delRun, loading } = useRequest(removeRule, { const { mutate: delRun, isPending: loading } = useMutation({
manual: true, mutationFn: removeRule,
onSuccess: () => { onSuccess: () => {
setSelectedRows([]); setSelectedRows([]);
actionRef.current?.reloadAndRest?.(); actionRef.current?.reloadAndRest?.();

21
src/pages/user/register/index.tsx

@ -1,4 +1,5 @@
import { history, Link, useRequest } from '@umijs/max'; import { useMutation } from '@tanstack/react-query';
import { history, Link } from '@umijs/max';
import { import {
Button, Button,
Col, Col,
@ -85,15 +86,23 @@ const Register: FC = () => {
} }
return 'poor'; return 'poor';
}; };
const { loading: submitting, run: register } = useRequest<{ const { isPending: submitting, mutate: register } = useMutation({
data: StateType; mutationFn: (formValues: Store) => {
}>(fakeRegister, { const payload = {
manual: true, mail: formValues.email,
password: formValues.password,
confirm: formValues.confirm,
mobile: formValues.mobile,
captcha: formValues.captcha,
prefix: formValues.prefix,
};
return fakeRegister(payload);
},
onSuccess: (data, params) => { onSuccess: (data, params) => {
if (data.status === 'ok') { if (data.status === 'ok') {
message.success('注册成功!'); message.success('注册成功!');
history.push({ history.push({
pathname: `/user/register-result?account=${params[0].email}`, pathname: `/user/register-result?account=${params.mail}`,
}); });
} }
}, },

Loading…
Cancel
Save