Skip to content

Commit 9870413

Browse files
authored
♻️ Refactor recover password (fastapi#1242)
1 parent cb596e8 commit 9870413

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

frontend/src/routes/recover-password.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
Input,
88
Text,
99
} from "@chakra-ui/react"
10+
import { useMutation } from "@tanstack/react-query"
1011
import { createFileRoute, redirect } from "@tanstack/react-router"
1112
import { type SubmitHandler, useForm } from "react-hook-form"
1213

13-
import { LoginService } from "../client"
14+
import { type ApiError, LoginService } from "../client"
1415
import { isLoggedIn } from "../hooks/useAuth"
1516
import useCustomToast from "../hooks/useCustomToast"
1617
import { emailPattern } from "../utils"
@@ -34,19 +35,35 @@ function RecoverPassword() {
3435
const {
3536
register,
3637
handleSubmit,
38+
reset,
3739
formState: { errors, isSubmitting },
3840
} = useForm<FormData>()
3941
const showToast = useCustomToast()
4042

41-
const onSubmit: SubmitHandler<FormData> = async (data) => {
43+
const recoverPassword = async (data: FormData) => {
4244
await LoginService.recoverPassword({
4345
email: data.email,
4446
})
45-
showToast(
46-
"Email sent.",
47-
"We sent an email with a link to get back into your account.",
48-
"success",
49-
)
47+
}
48+
49+
const mutation = useMutation({
50+
mutationFn: recoverPassword,
51+
onSuccess: () => {
52+
showToast(
53+
"Email sent.",
54+
"We sent an email with a link to get back into your account.",
55+
"success",
56+
)
57+
reset()
58+
},
59+
onError: (err: ApiError) => {
60+
const errDetail = (err.body as any)?.detail
61+
showToast("Something went wrong.", `${errDetail}`, "error")
62+
},
63+
})
64+
65+
const onSubmit: SubmitHandler<FormData> = async (data) => {
66+
mutation.mutate(data)
5067
}
5168

5269
return (

0 commit comments

Comments
 (0)