2024-03-07 19:16:23 +01:00
import { Button , Container , FormControl , FormErrorMessage , Heading , Input , Text } from '@chakra-ui/react' ;
import { createFileRoute , redirect } from '@tanstack/react-router' ;
import { SubmitHandler , useForm } from 'react-hook-form' ;
2024-02-15 17:17:26 -05:00
2024-03-07 19:16:23 +01:00
import { LoginService } from '../client' ;
import useCustomToast from '../hooks/useCustomToast' ;
import { isLoggedIn } from '../hooks/useAuth' ;
2024-02-15 17:17:26 -05:00
interface FormData {
email : string ;
}
2024-03-07 19:16:23 +01:00
export const Route = createFileRoute ( '/recover-password' ) ( {
component : RecoverPassword ,
beforeLoad : async ( ) = > {
if ( isLoggedIn ( ) ) {
throw redirect ( {
to : '/' ,
} )
}
}
} )
function RecoverPassword() {
2024-02-29 17:30:58 -05:00
const { register , handleSubmit , formState : { errors , isSubmitting } } = useForm < FormData > ( ) ;
2024-02-26 09:39:09 -05:00
const showToast = useCustomToast ( ) ;
2024-02-15 17:17:26 -05:00
const onSubmit : SubmitHandler < FormData > = async ( data ) = > {
2024-02-29 17:30:58 -05:00
await LoginService . recoverPassword ( {
2024-02-15 17:17:26 -05:00
email : data.email ,
} ) ;
2024-03-07 19:16:23 +01:00
showToast ( 'Email sent.' , 'We sent an email with a link to get back into your account.' , 'success' ) ;
2024-02-15 17:17:26 -05:00
} ;
return (
< Container
2024-03-07 19:16:23 +01:00
as = 'form'
2024-02-15 17:17:26 -05:00
onSubmit = { handleSubmit ( onSubmit ) }
2024-03-07 19:16:23 +01:00
h = '100vh'
maxW = 'sm'
alignItems = 'stretch'
justifyContent = 'center'
2024-02-15 17:17:26 -05:00
gap = { 4 }
centerContent
>
2024-03-07 19:16:23 +01:00
< Heading size = 'xl' color = 'ui.main' textAlign = 'center' mb = { 2 } >
2024-02-15 17:17:26 -05:00
Password Recovery
< / Heading >
2024-03-07 19:16:23 +01:00
< Text align = 'center' >
2024-02-29 17:30:58 -05:00
A password recovery email will be sent to the registered account .
< / Text >
< FormControl isInvalid = { ! ! errors . email } >
< Input id = 'email' { ...register ( 'email' , { required : 'Email is required' , pattern : { value : / ^ [ A - Z0 - 9._ % + - ] + @ [ A - Z0 - 9. - ] + \ . [ A - Z ] { 2 , 4 } $ / i , message : 'Invalid email address' } } ) } placeholder = 'Email' type = 'email' / >
{ errors . email && < FormErrorMessage > { errors . email . message } < / FormErrorMessage > }
2024-02-15 17:17:26 -05:00
< / FormControl >
2024-03-07 19:16:23 +01:00
< Button bg = 'ui.main' color = 'white' _hover = { { opacity : 0.8 } } type = 'submit' isLoading = { isSubmitting } >
2024-02-15 17:17:26 -05:00
Continue
< / Button >
< / Container >
) ;
} ;
export default RecoverPassword ;