@@ -7,10 +7,11 @@ import {
7
7
Input ,
8
8
Text ,
9
9
} from "@chakra-ui/react"
10
+ import { useMutation } from "@tanstack/react-query"
10
11
import { createFileRoute , redirect } from "@tanstack/react-router"
11
12
import { type SubmitHandler , useForm } from "react-hook-form"
12
13
13
- import { LoginService } from "../client"
14
+ import { type ApiError , LoginService } from "../client"
14
15
import { isLoggedIn } from "../hooks/useAuth"
15
16
import useCustomToast from "../hooks/useCustomToast"
16
17
import { emailPattern } from "../utils"
@@ -34,19 +35,35 @@ function RecoverPassword() {
34
35
const {
35
36
register,
36
37
handleSubmit,
38
+ reset,
37
39
formState : { errors, isSubmitting } ,
38
40
} = useForm < FormData > ( )
39
41
const showToast = useCustomToast ( )
40
42
41
- const onSubmit : SubmitHandler < FormData > = async ( data ) => {
43
+ const recoverPassword = async ( data : FormData ) => {
42
44
await LoginService . recoverPassword ( {
43
45
email : data . email ,
44
46
} )
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 )
50
67
}
51
68
52
69
return (
0 commit comments