Skip to content

Commit

Permalink
converted ResetPassword component from jsx to tsx (#2688)
Browse files Browse the repository at this point in the history
* converted ResetPassword component from jsx to tsx

* added async for clarity of the code
  • Loading branch information
markgol777 authored Nov 22, 2024
1 parent 8653c33 commit 5882ab7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo } from 'react'
import { FC, useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'

import useForm from '~/hooks/use-form'
Expand All @@ -19,14 +19,20 @@ import TitleWithDescription from '~/components/title-with-description/TitleWithD
import LoginDialog from '~/containers/guest-home-page/login-dialog/LoginDialog'
import { styles } from '~/containers/guest-home-page/reset-password/ResetPassword.styles'

import { ButtonVariantEnum, SizeEnum } from '~/types'
import { ButtonVariantEnum, SizeEnum, NewPassword } from '~/types'
import { confirmPassword, password } from '~/utils/validations/login'
import { snackbarVariants } from '~/constants'
import imgSuccess from '~/assets/img/email-confirmation-modals/success-icon.svg'
import { openAlert } from '~/redux/features/snackbarSlice'
import { getErrorKey } from '~/utils/get-error-key'
import { Component } from '~/context/modal-context'

const ResetPassword = ({ resetToken, openModal }) => {
interface ResetPasswordProps {
resetToken: string
openModal: (component: Component, delayToClose?: number) => void
}

const ResetPassword: FC<ResetPasswordProps> = ({ resetToken, openModal }) => {
const { t } = useTranslation()
const dispatch = useAppDispatch()

Expand Down Expand Up @@ -57,7 +63,7 @@ const ResetPassword = ({ resetToken, openModal }) => {
loading,
fetchData: sendResetPassword
} = useAxios({
service: (newPassword) =>
service: (newPassword: NewPassword) =>
AuthService.resetPassword(resetToken, newPassword),
fetchOnMount: false,
defaultResponse: null
Expand All @@ -76,13 +82,13 @@ const ResetPassword = ({ resetToken, openModal }) => {
}
}, [error, openModal, response, dispatch, successNotification])

const { handleSubmit, handleInputChange, handleBlur, errors, data } = useForm(
{
onSubmit: () => sendResetPassword({ password: data.password }),
const { handleSubmit, handleInputChange, handleBlur, errors, data } =
useForm<NewPassword>({
onSubmit: async (): Promise<void> =>
sendResetPassword({ password: data.password }),
initialValues: { password: '', confirmPassword: '' },
validations: { password, confirmPassword }
}
)
})

const { inputVisibility: passwordVisibility, showInputText: showPassword } =
useInputVisibility(errors.password)
Expand All @@ -107,7 +113,6 @@ const ResetPassword = ({ resetToken, openModal }) => {
onBlur={handleBlur('password')}
onChange={handleInputChange('password')}
required
size='large'
sx={{ mb: '5px' }}
type={showPassword ? 'text' : 'password'}
value={data.password}
Expand All @@ -120,7 +125,6 @@ const ResetPassword = ({ resetToken, openModal }) => {
onBlur={handleBlur('confirmPassword')}
onChange={handleInputChange('confirmPassword')}
required
size='large'
type={showConfirmPassword ? 'text' : 'password'}
value={data.confirmPassword}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/services/auth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const AuthService = {
},
resetPassword: (
resetToken: string,
newPassword: string
newPassword: { password: string }
): Promise<AxiosResponse> => {
const confirmUrl = createUrlPath(URLs.auth.resetPassword, resetToken)
return axiosClient.patch(confirmUrl, newPassword)
Expand Down
9 changes: 9 additions & 0 deletions src/types/user/user-interfaces/user.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ export interface ChangePasswordParams {
confirmPassword: string
}

export interface NewPassword {
confirmPassword?: string
password: string
}

export interface SendResetPasswordPayload {
password: string
}

export interface SignupResponse {
userId: string
userEmail: string
Expand Down

0 comments on commit 5882ab7

Please sign in to comment.