Skip to content

Commit

Permalink
fix(ProTable): 接口请求swr替换@kkt/request
Browse files Browse the repository at this point in the history
  • Loading branch information
yaob421123 committed Apr 27, 2023
1 parent 5182859 commit 4f6c06b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
2 changes: 0 additions & 2 deletions packages/components/src/ProForm/formdom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ function FormDom({
const store = useStore();
const colProps = useColPropsContext();

console.log(8888, formType);

const { setFormState } = store as {
setFormState: ((p: any) => void) | undefined;
};
Expand Down
46 changes: 18 additions & 28 deletions packages/components/src/ProTable/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import React, {
useRef,
useMemo,
} from 'react';
import useSWR from 'swr';
import { Table, Pagination, Checkbox, Radio, Empty } from 'uiw';
import { request } from '@uiw-admin/utils';
import { useStore } from './hooks';
import { Fields, BaseTableProps, FormCol } from './types';
import useSelections from './useSelections';
import { useReactMutation, fetchFn } from '@kkt/request';

const BaseTable: React.FC<BaseTableProps> = ({
style,
Expand All @@ -36,15 +35,8 @@ const BaseTable: React.FC<BaseTableProps> = ({

const store = useStore();

let {
formatData,
updateStore,
query,
key,
searchValues,
SWRConfiguration = {},
requestOptions,
} = store as any;
let { formatData, updateStore, query, key, searchValues, requestOptions } =
store as any;

const { selectKey, type = 'checkbox', defaultSelected = [] } = rowSelection;
const { x } = scroll;
Expand Down Expand Up @@ -83,11 +75,8 @@ const BaseTable: React.FC<BaseTableProps> = ({
// 格式化接口查询参数
const formatQuery = () => {
if (query) {
return query(
pageIndex,
pgSize,
isFirstMountRef.current === false ? defaultValues : searchValues,
);
const params = { ...defaultValues, ...searchValues };
return query(pageIndex, pgSize, params);
} else {
// 默认传参
return {
Expand All @@ -98,21 +87,22 @@ const BaseTable: React.FC<BaseTableProps> = ({
};

const pageSize = formatQuery().pageSize || 10;
// 调接口

const { data, isValidating, mutate } = useSWR(
[key, { method: 'POST', body: formatQuery(), ...requestOptions }],
request,
{
revalidateOnFocus: false,
revalidateOnMount: false,
...SWRConfiguration,
// 调接口
const { mutate, data, isLoading }: any = useReactMutation({
mutationFn: async () => {
console.log(689, formatQuery(), query);
return fetchFn(key, {
method: 'POST',
body: JSON.stringify(formatQuery()),
...requestOptions,
});
},
);
});

useEffect(() => {
// 第一次加载
mutate(false);
mutate();
}, [mutate]);

// table数据
Expand Down Expand Up @@ -165,7 +155,7 @@ const BaseTable: React.FC<BaseTableProps> = ({
const stores: any = {
data: tableData,
total,
loading: isValidating,
loading: isLoading,
// onSearch,
selection,
pageIndex,
Expand All @@ -186,7 +176,7 @@ const BaseTable: React.FC<BaseTableProps> = ({
}
}, [
JSON.stringify(tableData),
isValidating,
isLoading,
JSON.stringify(columns),
pageIndex,
JSON.stringify(selection),
Expand Down
11 changes: 10 additions & 1 deletion packages/components/src/ProTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Table from './BaseTable';
import BaseForm from './BaseForm';
import { StoreCtx } from './hooks';
import { ProtableProps } from './types';
import { QueryClientProvider, queryClient } from '@kkt/request';
import './index.css';

export * from './types';
Expand Down Expand Up @@ -122,4 +123,12 @@ const ProTabel: React.FC<ProtableProps> = (props) => {
);
};

export default ProTabel;
const Tables: React.FC<ProtableProps> = (props) => {
return (
<QueryClientProvider client={queryClient}>
<ProTabel {...props} />
</QueryClientProvider>
);
};

export default Tables;
5 changes: 1 addition & 4 deletions packages/user-login/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import './styles/index.css';
export type FormValue = {
username?: string;
password?: string;
swr_Rest_Time?: number | string;
};

export interface FieldsProps<T = any> extends FormFieldsProps<T> {
Expand Down Expand Up @@ -122,7 +121,6 @@ const Login = (props: UserLoginProps) => {
url: api,
method: 'POST',
...requestConfig,
mutationKey: ['login'],
onSuccess: (res) => onSuccess(res, store),
onError: () => onSuccess(undefined, store),
});
Expand Down Expand Up @@ -237,7 +235,6 @@ const Login = (props: UserLoginProps) => {
setStore({
...current,
...(result || {}),
swr_Rest_Time: new Date().getTime(),
});
await mutateAsync({
...(current as any),
Expand All @@ -249,7 +246,7 @@ const Login = (props: UserLoginProps) => {
return;
}
}
setStore({ ...current, swr_Rest_Time: new Date().getTime() });
setStore({ ...current });
await mutateAsync({ ...(current as any) });
}
}}
Expand Down

0 comments on commit 4f6c06b

Please sign in to comment.