|
|
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); |
|
|
|
|
|
|
|
|
const values = Form.useWatch([], form); |
|
|
|
|
|
useEffect(() => { |
|
|
form |
|
|
.validateFields({ validateOnly: true }) |
|
|
.then(() => setSubmittable(true)) |
|
|
.catch(() => setSubmittable(false)); |
|
|
}, [form, values]); |
|
|
|
|
|
return { submittable }; |
|
|
}; |
|
|
|