From cec5ecbc4744a547838c14a99b8fc0b861611229 Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Tue, 8 Feb 2022 18:19:05 +0700 Subject: [PATCH] fix(#2199): fix popup recovery --- .../components/RecoveryPopup/hooks.tsx | 33 ++++++++++++------- .../components/RecoveryPopup/index.tsx | 6 ++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx b/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx index c58efbc9..ec0135d4 100644 --- a/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx +++ b/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx @@ -1,4 +1,8 @@ -import { ChangeEvent, useState } from 'react' +import { + ChangeEvent, + useEffect, + useState, +} from 'react' import { isValidEmail } from 'features/AuthServiceApp/helpers/isValidEmail' import { loginCheckChangePass } from 'features/AuthServiceApp/requests/loginCheck' @@ -11,6 +15,7 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { const [email, setEmail] = useState('') const [isFetching, setIsFetching] = useState(false) const [isSendMessage, setIsSendMessage] = useState(false) + const [isSuccess, setIsSuccess] = useState(false) const onEmailChange = ({ target: { value }, @@ -26,6 +31,7 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { const closePopup = () => { setIsSendMessage(false) + setIsSuccess(false) setIsFetching(false) setError('') setEmail('') @@ -38,17 +44,22 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { setIsFetching(false) } - const handleSubmit = async () => { - try { - setIsFetching(true) - await loginCheckChangePass(email, lang) - setIsFetching(false) - setIsSendMessage(true) - } catch (err) { - handleError(String(err)) - } + const handleSubmit = () => { + setIsFetching(true) + loginCheckChangePass(email, lang) + .then(() => setIsFetching(false)) + .then(() => setIsSendMessage(true)) + .catch(handleError) } + useEffect(() => { + if (!isFetching && isSendMessage) { + setIsSuccess(true) + } else { + setIsSuccess(false) + } + }, [isSendMessage, isFetching]) + return { closePopup, email, @@ -56,7 +67,7 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { handleSubmit, isFetching, isSendBtnDisabled, - isSendMessage, + isSuccess, onEmailChange, } } diff --git a/src/features/AuthServiceApp/components/RecoveryPopup/index.tsx b/src/features/AuthServiceApp/components/RecoveryPopup/index.tsx index 2cb66413..f327aa09 100644 --- a/src/features/AuthServiceApp/components/RecoveryPopup/index.tsx +++ b/src/features/AuthServiceApp/components/RecoveryPopup/index.tsx @@ -33,7 +33,7 @@ export const RecoveryPopup = (props: Props) => { handleSubmit, isFetching, isSendBtnDisabled, - isSendMessage, + isSuccess, onEmailChange, } = useRecovery(setIsModalOpen) @@ -50,7 +50,7 @@ export const RecoveryPopup = (props: Props) => { - {isSendMessage ? ( + {isSuccess ? ( @@ -72,7 +72,7 @@ export const RecoveryPopup = (props: Props) => { )}