From eaa2ee1887f47924924b707c537d9946fb0fce52 Mon Sep 17 00:00:00 2001 From: William Grant Date: Mon, 4 Mar 2024 17:58:15 +1100 Subject: [PATCH] feat: added in loading animation when restoring an account --- .../registration/stages/RestoreAccount.tsx | 161 ++++++++++-------- 1 file changed, 91 insertions(+), 70 deletions(-) diff --git a/ts/components/registration/stages/RestoreAccount.tsx b/ts/components/registration/stages/RestoreAccount.tsx index 8d59f07cc..00de2efed 100644 --- a/ts/components/registration/stages/RestoreAccount.tsx +++ b/ts/components/registration/stages/RestoreAccount.tsx @@ -1,106 +1,127 @@ -import { useState } from 'react'; +import { useEffect, useState } from 'react'; +import { useDispatch } from 'react-redux'; +import { + AccountRestoration, + setAccountRestorationStep, +} from '../../../state/onboarding/ducks/registration'; +import { useOnboardAccountRestorationStep } from '../../../state/onboarding/selectors/registration'; import { Flex } from '../../basic/Flex'; import { SessionButton, SessionButtonColor } from '../../basic/SessionButton'; import { SpacerLG, SpacerSM } from '../../basic/Text'; import { SessionIcon } from '../../icon'; import { SessionInput } from '../../inputs'; +import { SessionProgressBar } from '../../loading'; import { signInWithLinking } from '../RegistrationStages'; import { OnboardContainer, OnboardDescription, OnboardHeading } from '../components'; import { BackButtonWithininContainer } from '../components/BackButton'; export const RestoreAccount = () => { - // const step = useOnboardAccountRestorationStep(); + const step = useOnboardAccountRestorationStep(); const [recoveryPhrase, setRecoveryPhrase] = useState(''); const [recoveryPhraseError, setRecoveryPhraseError] = useState(undefined as string | undefined); - const [loading, setIsLoading] = useState(false); + const [progress, setProgress] = useState(0); + + const dispatch = useDispatch(); // Seed is mandatory no matter which mode const seedOK = !!recoveryPhrase && !recoveryPhraseError; - const activateContinueButton = seedOK && !loading; + const activateContinueButton = seedOK && !(step === AccountRestoration.Loading); - const continueYourSession = () => { - // if (isRecovery) { - // void signInWithRecovery({ - // displayName, - // userRecoveryPhrase: recoveryPhrase, - // }); - // } - setIsLoading(true); - void signInWithLinking({ + const continueYourSession = async () => { + dispatch(setAccountRestorationStep(AccountRestoration.Loading)); + await signInWithLinking({ userRecoveryPhrase: recoveryPhrase, errorCallback: setRecoveryPhraseError, }); - setIsLoading(false); + dispatch(setAccountRestorationStep(AccountRestoration.Complete)); }; + useEffect(() => { + let interval: NodeJS.Timeout; + if (step === AccountRestoration.Loading) { + interval = setInterval(() => { + setProgress(oldProgress => { + if (oldProgress === 100) { + clearInterval(interval); + return 100; + } + // Increment by 100 / 15 = 6.67 each second to complete in 15 seconds + return Math.min(oldProgress + 100 / 15, 100); + }); + }, 1000); + } + + return () => clearInterval(interval); + }, [step]); + return ( - + {step === AccountRestoration.Loading ? ( - - {window.i18n('sessionRecoveryPassword')} - - - - {window.i18n('onboardingRecoveryPassword')} - - { - setRecoveryPhrase(seed); - setRecoveryPhraseError(!seed ? window.i18n('recoveryPhraseEmpty') : undefined); - }} - onEnterPressed={continueYourSession} - error={recoveryPhraseError} - enableShowHide={true} - inputDataTestId="recovery-phrase-input" - /> - - - - {/* TODO[epic=898] Replace with new Session Progress Loader */} - {/* {loading && ( - - + - )} */} - + ) : ( + + + + {window.i18n('sessionRecoveryPassword')} + + + + {window.i18n('onboardingRecoveryPassword')} + + { + setRecoveryPhrase(seed); + setRecoveryPhraseError(!seed ? window.i18n('recoveryPhraseEmpty') : undefined); + }} + onEnterPressed={continueYourSession} + error={recoveryPhraseError} + enableShowHide={true} + inputDataTestId="recovery-phrase-input" + /> + + + + + )} ); };