File size: 2,967 Bytes
2665f5c e08f767 2665f5c f859b0d 2665f5c 6e3eead 2665f5c 196c662 2665f5c 196c662 6e3eead 2665f5c 6e3eead 2665f5c 6e3eead 2665f5c 196c662 2665f5c 196c662 2665f5c 196c662 2665f5c 196c662 6e3eead 2665f5c 6e3eead 2665f5c 6e3eead f859b0d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import { Authorization } from '@/constants/authorization';
import userService from '@/services/user-service';
import authorizationUtil from '@/utils/authorization-util';
import { useMutation } from '@tanstack/react-query';
import { Form, message } from 'antd';
import { FormInstance } from 'antd/lib';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { history } from 'umi';
export interface ILoginRequestBody {
email: string;
password: string;
}
export interface IRegisterRequestBody extends ILoginRequestBody {
nickname: string;
}
export const useLogin = () => {
const { t } = useTranslation();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['login'],
mutationFn: async (params: { email: string; password: string }) => {
const { data: res = {}, response } = await userService.login(params);
if (res.code === 0) {
const { data } = res;
message.success(t('message.logged'));
const authorization = response.headers.get(Authorization);
const token = data.access_token;
const userInfo = {
avatar: data.avatar,
name: data.nickname,
email: data.email,
};
authorizationUtil.setItems({
Authorization: authorization,
userInfo: JSON.stringify(userInfo),
Token: token,
});
}
return res.code;
},
});
return { data, loading, login: mutateAsync };
};
export const useRegister = () => {
const { t } = useTranslation();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['register'],
mutationFn: async (params: {
email: string;
password: string;
nickname: string;
}) => {
const { data = {} } = await userService.register(params);
if (data.code === 0) {
message.success(t('message.registered'));
}
return data.code;
},
});
return { data, loading, register: mutateAsync };
};
export const useLogout = () => {
const { t } = useTranslation();
const {
data,
isPending: loading,
mutateAsync,
} = useMutation({
mutationKey: ['logout'],
mutationFn: async () => {
const { data = {} } = await userService.logout();
if (data.code === 0) {
message.success(t('message.logout'));
authorizationUtil.removeAll();
history.push('/login');
}
return data.code;
},
});
return { data, loading, logout: mutateAsync };
};
export const useHandleSubmittable = (form: FormInstance) => {
const [submittable, setSubmittable] = useState<boolean>(false);
// Watch all values
const values = Form.useWatch([], form);
useEffect(() => {
form
.validateFields({ validateOnly: true })
.then(() => setSubmittable(true))
.catch(() => setSubmittable(false));
}, [form, values]);
return { submittable };
};
|