From f534819b144dcf07b36cb4f3880ba2c505b2b28d Mon Sep 17 00:00:00 2001 From: User Date: Thu, 27 Jun 2024 15:17:45 -0500 Subject: [PATCH 1/3] Refactor recover password --- frontend/src/routes/recover-password.tsx | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/frontend/src/routes/recover-password.tsx b/frontend/src/routes/recover-password.tsx index 7be649c861..c023721ff2 100644 --- a/frontend/src/routes/recover-password.tsx +++ b/frontend/src/routes/recover-password.tsx @@ -7,10 +7,11 @@ import { Input, Text, } from "@chakra-ui/react" +import { useMutation } from "@tanstack/react-query" import { createFileRoute, redirect } from "@tanstack/react-router" import { type SubmitHandler, useForm } from "react-hook-form" -import { LoginService } from "../client" +import { type ApiError, LoginService } from "../client" import { isLoggedIn } from "../hooks/useAuth" import useCustomToast from "../hooks/useCustomToast" import { emailPattern } from "../utils" @@ -34,19 +35,31 @@ function RecoverPassword() { const { register, handleSubmit, + reset, formState: { errors, isSubmitting }, } = useForm() const showToast = useCustomToast() - const onSubmit: SubmitHandler = async (data) => { + const recoverPassword = async (data: FormData) => { await LoginService.recoverPassword({ email: data.email, }) - showToast( - "Email sent.", - "We sent an email with a link to get back into your account.", - "success", - ) + } + + const mutation = useMutation({ + mutationFn: recoverPassword, + onSuccess: () => { + console.log("success") + reset() + }, + onError: (err: ApiError) => { + const errDetail = (err.body as any)?.detail + showToast("Something went wrong.", `${errDetail}`, "error") + }, + }) + + const onSubmit: SubmitHandler = async (data) => { + mutation.mutate(data) } return ( From 9a80224b58de70ea9606af9f8ebc7175533244e2 Mon Sep 17 00:00:00 2001 From: User Date: Thu, 27 Jun 2024 15:20:45 -0500 Subject: [PATCH 2/3] Add success toast --- frontend/src/routes/recover-password.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/routes/recover-password.tsx b/frontend/src/routes/recover-password.tsx index c023721ff2..a6048594fb 100644 --- a/frontend/src/routes/recover-password.tsx +++ b/frontend/src/routes/recover-password.tsx @@ -49,7 +49,7 @@ function RecoverPassword() { const mutation = useMutation({ mutationFn: recoverPassword, onSuccess: () => { - console.log("success") + showToast("Success!", "Password recovery email sent.", "success") reset() }, onError: (err: ApiError) => { From 0780de744f0273e321c253f2022072f140aadd75 Mon Sep 17 00:00:00 2001 From: User Date: Thu, 27 Jun 2024 15:24:08 -0500 Subject: [PATCH 3/3] Update toast --- frontend/src/routes/recover-password.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frontend/src/routes/recover-password.tsx b/frontend/src/routes/recover-password.tsx index a6048594fb..6ea24bf37d 100644 --- a/frontend/src/routes/recover-password.tsx +++ b/frontend/src/routes/recover-password.tsx @@ -49,7 +49,11 @@ function RecoverPassword() { const mutation = useMutation({ mutationFn: recoverPassword, onSuccess: () => { - showToast("Success!", "Password recovery email sent.", "success") + showToast( + "Email sent.", + "We sent an email with a link to get back into your account.", + "success", + ) reset() }, onError: (err: ApiError) => {