♻️ Refactor recover password (#1242)
This commit is contained in:
@@ -7,10 +7,11 @@ import {
|
|||||||
Input,
|
Input,
|
||||||
Text,
|
Text,
|
||||||
} from "@chakra-ui/react"
|
} from "@chakra-ui/react"
|
||||||
|
import { useMutation } from "@tanstack/react-query"
|
||||||
import { createFileRoute, redirect } from "@tanstack/react-router"
|
import { createFileRoute, redirect } from "@tanstack/react-router"
|
||||||
import { type SubmitHandler, useForm } from "react-hook-form"
|
import { type SubmitHandler, useForm } from "react-hook-form"
|
||||||
|
|
||||||
import { LoginService } from "../client"
|
import { type ApiError, LoginService } from "../client"
|
||||||
import { isLoggedIn } from "../hooks/useAuth"
|
import { isLoggedIn } from "../hooks/useAuth"
|
||||||
import useCustomToast from "../hooks/useCustomToast"
|
import useCustomToast from "../hooks/useCustomToast"
|
||||||
import { emailPattern } from "../utils"
|
import { emailPattern } from "../utils"
|
||||||
@@ -34,19 +35,35 @@ function RecoverPassword() {
|
|||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
reset,
|
||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
} = useForm<FormData>()
|
} = useForm<FormData>()
|
||||||
const showToast = useCustomToast()
|
const showToast = useCustomToast()
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
const recoverPassword = async (data: FormData) => {
|
||||||
await LoginService.recoverPassword({
|
await LoginService.recoverPassword({
|
||||||
email: data.email,
|
email: data.email,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const mutation = useMutation({
|
||||||
|
mutationFn: recoverPassword,
|
||||||
|
onSuccess: () => {
|
||||||
showToast(
|
showToast(
|
||||||
"Email sent.",
|
"Email sent.",
|
||||||
"We sent an email with a link to get back into your account.",
|
"We sent an email with a link to get back into your account.",
|
||||||
"success",
|
"success",
|
||||||
)
|
)
|
||||||
|
reset()
|
||||||
|
},
|
||||||
|
onError: (err: ApiError) => {
|
||||||
|
const errDetail = (err.body as any)?.detail
|
||||||
|
showToast("Something went wrong.", `${errDetail}`, "error")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||||
|
mutation.mutate(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Reference in New Issue
Block a user