feat: RequestPasswordReset

This commit is contained in:
Quangnhankie
2021-10-04 09:14:58 +07:00
parent 19c8267220
commit 7f927a0ba3
11 changed files with 376 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
import { useState } from 'react'
import useActiveCustomer from './useActiveCustomer'
import fetcher from 'src/utils/fetcher'
import { CommonError } from 'src/domains/interfaces/CommonError'
import { resetPasswordMutation } from '@framework/utils/mutations/reset-password-mutation'
import { ResetPasswordMutation } from '@framework/schema'
interface Props {
token?: string| string[] ,
password:string
}
const useResetPassword = () => {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
// const { mutate } = useActiveCustomer()
const resetPassword = (
{token,password}: Props,
fCallBack: (isSuccess: boolean, message?: string) => void
) => {
setError(null)
setLoading(true)
fetcher<ResetPasswordMutation>({
query: resetPasswordMutation,
variables: {
token: token,
password:password
},
})
.then((data) => {
if (data.resetPassword.__typename !== 'CurrentUser') {
throw CommonError.create(
data.resetPassword.message,
data.resetPassword.errorCode
)
}
// mutate()
fCallBack(true)
return data
})
.catch((error) => {
setError(error)
fCallBack(false, error.message)
})
.finally(() => setLoading(false))
}
return { loading, resetPassword, error }
}
export default useResetPassword