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

Reset password - reset password dialog #278

Merged
merged 8 commits into from
Dec 9, 2023
4 changes: 2 additions & 2 deletions src/components/PageLayout/Authentication/Authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {AuthContainer} from '@/utils/AuthContainer'
import {useSeminarInfo} from '@/utils/useSeminarInfo'

import {Overlay} from '../../Overlay/Overlay'
import {LoginForm} from '../LoginForm/LoginForm'
import {LoginFormWrapper} from '../LoginFormWrapper/LoginFormWrapper'
import styles from './Authentication.module.scss'

export const Authentication: FC = () => {
Expand Down Expand Up @@ -38,7 +38,7 @@ export const Authentication: FC = () => {
<Overlay display={displayAuthenticationOverlay} closeOverlay={toggleDisplayLoginOverlay}>
<div className={styles.authenticationContainer}>
<div className={styles.content}>
<LoginForm closeOverlay={toggleDisplayLoginOverlay} />
<LoginFormWrapper closeOverlay={toggleDisplayLoginOverlay} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nie som si isty, ci mozeme ratat s tym, ze toggleDisplayLoginOverlay unmountne LoginFormWrapper component. ked sa neunmountne a ostane len nejak invisible, tak pri znovuotvoreni loginu by som cakal zachovany stav, ze resetujeme heslo

</div>
</div>
</Overlay>
Expand Down
8 changes: 3 additions & 5 deletions src/components/PageLayout/LoginForm/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {Button} from '@/components/Clickable/Button'
import {FormInput} from '@/components/FormItems/FormInput/FormInput'
import {AuthContainer} from '@/utils/AuthContainer'

import {LoginFormWrapperProps} from '../LoginFormWrapper/LoginFormWrapper'

type LoginFormValues = {
email: string
password: string
Expand All @@ -17,11 +19,7 @@ const defaultValues: LoginFormValues = {
password: '',
}

interface ILoginForm {
closeOverlay: () => void
}

export const LoginForm: FC<ILoginForm> = ({closeOverlay}) => {
export const LoginForm: FC<LoginFormWrapperProps> = ({closeOverlay}) => {
const {login} = AuthContainer.useContainer()
const {handleSubmit, control} = useForm<LoginFormValues>({defaultValues})

Expand Down
38 changes: 38 additions & 0 deletions src/components/PageLayout/LoginFormWrapper/LoginFormWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {useRouter} from 'next/router'
import {FC, useState} from 'react'

import {Button} from '@/components/Clickable/Button'

import {LoginForm} from '../LoginForm/LoginForm'
import {PasswordResetRequestForm} from '../PasswordResetRequest/PasswordResetRequest'

export type LoginFormWrapperProps = {
closeOverlay: () => void
}

export const LoginFormWrapper: FC<LoginFormWrapperProps> = ({closeOverlay}) => {
const router = useRouter()
const [form, changeForm] = useState('login')

if (form === 'login')
return (
<>
<LoginForm closeOverlay={closeOverlay} />
<Button
onClick={() => {
changeForm('reset')
}}
>
Zabudnuté heslo
</Button>
</>
)

return (
<PasswordResetRequestForm
closeOverlay={() => {
router.push('/verifikacia')
}}
/>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {useMutation} from '@tanstack/react-query'
import axios from 'axios'
import {FC} from 'react'
import {SubmitHandler, useForm} from 'react-hook-form'

import {Button} from '@/components/Clickable/Button'
import {FormInput} from '@/components/FormItems/FormInput/FormInput'
import {IGeneralPostResponse} from '@/types/api/general'

import {LoginFormWrapperProps} from '../LoginFormWrapper/LoginFormWrapper'

type PasswordResetRequestFormValues = {
email: string
}

const defaultValues: PasswordResetRequestFormValues = {
email: '',
}

export const PasswordResetRequestForm: FC<LoginFormWrapperProps> = ({closeOverlay}) => {
const {handleSubmit, control} = useForm<PasswordResetRequestFormValues>({defaultValues})

const requiredRule = {required: '* Toto pole nemôže byť prázdne.'}

const {mutate: submitFormData} = useMutation({
mutationFn: (data: PasswordResetRequestFormValues) => {
return axios.post<IGeneralPostResponse>('/api/user/password/reset/', data)
},

onError: (error, variables, context) => {},

Check warning on line 30 in src/components/PageLayout/PasswordResetRequest/PasswordResetRequest.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'error' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 30 in src/components/PageLayout/PasswordResetRequest/PasswordResetRequest.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'variables' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 30 in src/components/PageLayout/PasswordResetRequest/PasswordResetRequest.tsx

View workflow job for this annotation

GitHub Actions / branch-test

'context' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 30 in src/components/PageLayout/PasswordResetRequest/PasswordResetRequest.tsx

View workflow job for this annotation

GitHub Actions / branch-test

Unexpected empty method 'onError'

onSuccess: () => {
closeOverlay()
},
})

const onSubmit: SubmitHandler<PasswordResetRequestFormValues> = (data) => {
submitFormData(data)
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<FormInput
control={control}
name="email"
label="Email"
rules={{
...requiredRule,
pattern: {
value: /^[\w%+.-]+@[\d.a-z-]+\.[a-z]{2,}$/iu,
message: '* Vložte správnu emailovú adresu.',
},
}}
/>
<Button type="submit">Resetovať heslo</Button>
</form>
)
}
Loading