diff --git a/apps/platform/src/views/auth/passwordReset.view.tsx b/apps/platform/src/views/auth/passwordReset.view.tsx index a20983a..21eb2be 100644 --- a/apps/platform/src/views/auth/passwordReset.view.tsx +++ b/apps/platform/src/views/auth/passwordReset.view.tsx @@ -1,14 +1,26 @@ -import { usePasswordResetRequestForm } from '@isomera/impl' +import { + pages, + useHandleErrorHook, + usePasswordResetRequestForm +} from '@isomera/impl' import { toast } from 'react-toastify' +import { useNavigate } from 'react-router-dom' export const PasswordResetView = () => { - // const navigate = useNavigate() - // const location = useLocation() + const navigate = useNavigate() + const { handleError } = useHandleErrorHook() - // const from = (location.state?.from.pathname as string) || '/profile' + const onSuccess = () => { + toast.success( + "If there was such user registered with email, then you'll receive confirmation code." + ) + navigate(pages.passwordResetRequestConfirmation.path) + } - const onSuccess = (message: string) => { - toast.success(message) + const onError = (error?: unknown) => { + if (error) { + handleError(error, { view: 'login' }) + } } const { @@ -19,28 +31,7 @@ export const PasswordResetView = () => { touched, handleSubmit, isSubmitting - } = usePasswordResetRequestForm(onSuccess) - - // useEffect(() => { - // if (isSuccess) { - // toast.success('You successfully logged in') - // navigate(from) - // } - // if (isError) { - // if (Array.isArray((error as any).data.error)) { - // ;(error as any).data.error.forEach((el: any) => - // toast.error(el.message, { - // position: 'top-right' - // }) - // ) - // } else { - // toast.error((error as any).data.message, { - // position: 'top-right' - // }) - // } - // } - // // eslint-disable-next-line react-hooks/exhaustive-deps - // }, [isLoading]) + } = usePasswordResetRequestForm({ onSuccess, onError }) return (
diff --git a/apps/platform/src/views/auth/passwordResetConfirm.view.tsx b/apps/platform/src/views/auth/passwordResetConfirm.view.tsx index 0564ed1..0b8e0ee 100644 --- a/apps/platform/src/views/auth/passwordResetConfirm.view.tsx +++ b/apps/platform/src/views/auth/passwordResetConfirm.view.tsx @@ -1,13 +1,27 @@ -import { usePasswordResetPerformForm } from '@isomera/impl' +import { + pages, + useHandleErrorHook, + usePasswordResetPerformForm +} from '@isomera/impl' import { toast } from 'react-toastify' +import { useNavigate } from 'react-router-dom' export const PasswordResetConfirmView = () => { - const onSuccess = (message: string) => { - toast.success(message) + const { handleError } = useHandleErrorHook() + const navigate = useNavigate() + + const onSuccess = () => { + toast.success( + 'Password changed successfully. Please log in with your new password.' + ) + navigate(pages.login.path) } - const onError = (message: string) => { - toast.error(message) + const onError = (error?: unknown) => { + toast.error('Password change failed. Please try again.') + if (error) { + handleError(error, { view: 'login' }) + } } const { @@ -18,7 +32,7 @@ export const PasswordResetConfirmView = () => { touched, handleSubmit, isSubmitting - } = usePasswordResetPerformForm(onSuccess, onError) + } = usePasswordResetPerformForm({ onSuccess, onError }) return ( diff --git a/apps/platform/src/views/auth/verificationCode.view.tsx b/apps/platform/src/views/auth/verificationCode.view.tsx index 91ba804..b6a06a5 100644 --- a/apps/platform/src/views/auth/verificationCode.view.tsx +++ b/apps/platform/src/views/auth/verificationCode.view.tsx @@ -1,13 +1,27 @@ -import { useConfirmCodePerformForm } from '@isomera/impl' +import { + pages, + useConfirmCodePerformForm, + useHandleErrorHook +} from '@isomera/impl' import { toast } from 'react-toastify' +import { useNavigate } from 'react-router-dom' export const VerificationCodeView = () => { - const onSuccess = (message: string) => { - toast.success(message) + const navigate = useNavigate() + const { handleError } = useHandleErrorHook() + + const onSuccess = () => { + toast.success( + 'Activate user successfully. Please log in with your account.' + ) + navigate(pages.login.path) } - const onError = (message: string) => { - toast.error(message) + const onError = (error?: unknown) => { + toast.error('Activate user failed. Please try again.') + if (error) { + handleError(error, { view: 'login' }) + } } const { @@ -18,7 +32,7 @@ export const VerificationCodeView = () => { touched, handleSubmit, isSubmitting - } = useConfirmCodePerformForm(onSuccess, onError) + } = useConfirmCodePerformForm({ onSuccess, onError }) return ( diff --git a/libs/impl/src/hooks/auth/useConfirmCodePerformForm.hook.ts b/libs/impl/src/hooks/auth/useConfirmCodePerformForm.hook.ts index d336d70..0237f3f 100644 --- a/libs/impl/src/hooks/auth/useConfirmCodePerformForm.hook.ts +++ b/libs/impl/src/hooks/auth/useConfirmCodePerformForm.hook.ts @@ -1,10 +1,5 @@ import { useFormik } from 'formik' - -import { useNavigate } from 'react-router-dom' - -import { pages } from '../../constants/pages' import { ConfirmationCodeDto, formikValidate } from '@isomera/dtos' -import { useHandleErrorHook } from '../error/useHandleError.hook' import { Pure, StatusType } from '@isomera/interfaces' import { userConfirmCodePerformHook } from './useConfirmCodePerform.hook' @@ -13,27 +8,24 @@ const initialValues: Pure = { email: '' } -export const useConfirmCodePerformForm = ( - onSuccess: (arg0: string) => void, - onError: (arg0: string) => void -) => { +interface Options { + onSuccess?: () => void + onError?: (error?: unknown) => void +} + +export const useConfirmCodePerformForm = (options: Options) => { const { performReset } = userConfirmCodePerformHook() - const { handleError } = useHandleErrorHook() - const navigate = useNavigate() const onSubmit = async (values: typeof initialValues) => { try { const result = await performReset(values) if (result.status === StatusType.OK) { - onSuccess( - 'Activate user successfully. Please log in with your account.' - ) + options.onSuccess && options.onSuccess() } else { - onError('Activate user failed. Please try again.') + options.onError && options.onError() } - navigate(pages.login.path) } catch (error) { - handleError(error, { view: 'login' }) + options.onError && options.onError(error) } } diff --git a/libs/impl/src/hooks/auth/usePasswordResetPerformForm.hook.ts b/libs/impl/src/hooks/auth/usePasswordResetPerformForm.hook.ts index d259d56..1618f00 100644 --- a/libs/impl/src/hooks/auth/usePasswordResetPerformForm.hook.ts +++ b/libs/impl/src/hooks/auth/usePasswordResetPerformForm.hook.ts @@ -1,10 +1,5 @@ import { useFormik } from 'formik' - -import { useNavigate } from 'react-router-dom' - -import { pages } from '../../constants/pages' import { formikValidate, ResetPasswordRequestDto } from '@isomera/dtos' -import { useHandleErrorHook } from '../error/useHandleError.hook' import { Pure, StatusType } from '@isomera/interfaces' import { usePasswordResetPerformHook } from './usePasswordResetPerform.hook' @@ -13,27 +8,24 @@ const initialValues: Pure = { passwordResetCode: '' } -export const usePasswordResetPerformForm = ( - onSuccess: (arg0: string) => void, - onError: (arg0: string) => void -) => { +interface Options { + onSuccess: () => void + onError: (error?: unknown) => void +} + +export const usePasswordResetPerformForm = (options: Options) => { const { performReset } = usePasswordResetPerformHook() - const { handleError } = useHandleErrorHook() - const navigate = useNavigate() const onSubmit = async (values: typeof initialValues) => { try { const result = await performReset(values) if (result.status === StatusType.OK) { - onSuccess( - 'Password changed successfully. Please log in with your new password.' - ) + options.onSuccess && options.onSuccess() } else { - onError('Password change failed. Please try again.') + options.onError && options.onError() } - navigate(pages.login.path) } catch (error) { - handleError(error, { view: 'login' }) + options.onError && options.onError(error) } } diff --git a/libs/impl/src/hooks/auth/usePasswordResetRequestForm.hook.ts b/libs/impl/src/hooks/auth/usePasswordResetRequestForm.hook.ts index a2744f7..0cc4617 100644 --- a/libs/impl/src/hooks/auth/usePasswordResetRequestForm.hook.ts +++ b/libs/impl/src/hooks/auth/usePasswordResetRequestForm.hook.ts @@ -1,10 +1,5 @@ import { useFormik } from 'formik' - -import { useNavigate } from 'react-router-dom' - -import { pages } from '../../constants/pages' import { formikValidate } from '@isomera/dtos' -import { useHandleErrorHook } from '../error/useHandleError.hook' import { Pure } from '@isomera/interfaces' import { usePasswordResetRequestHook } from './usePasswordResetRequest.hook' import { ForgotPasswordResetRequestDto } from '@isomera/dtos' @@ -13,22 +8,20 @@ const initialValues: Pure = { email: '' } -export const usePasswordResetRequestForm = ( - onSuccess: (arg0: string) => void -) => { +interface Options { + onSuccess?: () => void + onError?: (error?: unknown) => void +} + +export const usePasswordResetRequestForm = (options: Options) => { const { requestReset } = usePasswordResetRequestHook() - const { handleError } = useHandleErrorHook() - const navigate = useNavigate() const onSubmit = async (values: typeof initialValues) => { try { await requestReset(values) - onSuccess( - `If there was such user registered with email ${values.email}, then you'll receive confirmation code.` - ) - navigate(pages.passwordResetRequestConfirmation.path) + options.onSuccess && options.onSuccess() } catch (error) { - handleError(error, { view: 'login' }) + options.onError && options.onError(error) } }