Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor recover password #1242

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions frontend/src/routes/recover-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,19 +35,35 @@ function RecoverPassword() {
const {
register,
handleSubmit,
reset,
formState: { errors, isSubmitting },
} = useForm<FormData>()
const showToast = useCustomToast()

const onSubmit: SubmitHandler<FormData> = 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: () => {
showToast(
"Email sent.",
"We sent an email with a link to get back into your account.",
"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 (
Expand Down