diff --git a/src/pages/workspace/accounting/qbd/QuickBooksDesktopSetupPage.tsx b/src/pages/workspace/accounting/qbd/QuickBooksDesktopSetupPage.tsx index 74342bdf5f344..134e70f6f1495 100644 --- a/src/pages/workspace/accounting/qbd/QuickBooksDesktopSetupPage.tsx +++ b/src/pages/workspace/accounting/qbd/QuickBooksDesktopSetupPage.tsx @@ -1,4 +1,4 @@ -import React, {useCallback, useEffect, useState} from 'react'; +import React, {useEffect, useState} from 'react'; import {View} from 'react-native'; import ActivityIndicator from '@components/ActivityIndicator'; import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView'; @@ -28,102 +28,71 @@ import type SCREENS from '@src/SCREENS'; type RequireQuickBooksDesktopModalProps = PlatformStackScreenProps; +type SetupLinkFetchStatus = {status: 'loading'} | {status: 'success'; setupLink: string} | {status: 'error'}; + function RequireQuickBooksDesktopModal({route}: RequireQuickBooksDesktopModalProps) { const {translate} = useLocalize(); const styles = useThemeStyles(); const {environmentURL} = useEnvironment(); const illustrations = useMemoizedLazyIllustrations(['BrokenMagnifyingGlass', 'LaptopWithSecondScreenSync']); const policyID: string = route.params.policyID; - const [hasError, setHasError] = useState(false); - const [codatSetupLink, setCodatSetupLink] = useState(''); - const hasResultOfFetchingSetupLink = !!codatSetupLink || hasError; + const [setupLinkFetchStatus, setSetupLinkFetchStatus] = useState({status: 'loading'}); + + const isLoading = setupLinkFetchStatus.status === 'loading'; + const shouldShowError = setupLinkFetchStatus.status === 'error'; + const codatSetupLink = setupLinkFetchStatus.status === 'success' ? setupLinkFetchStatus.setupLink : ''; + + const ContentWrapper = isLoading + ? ({children}: React.PropsWithChildren) => {children} + : ({children}: React.PropsWithChildren) => children; + + useEffect(() => { + let shouldIgnoreResponse = false; + + // Since QBD doesn't support Taxes, we should disable them from the LHN when connecting to QBD + enablePolicyTaxes(policyID, false); + + setSetupLinkFetchStatus({status: 'loading'}); - const fetchSetupLink = useCallback(() => { - setHasError(false); - // eslint-disable-next-line rulesdir/no-thenable-actions-in-views getQuickbooksDesktopCodatSetupLink(policyID).then((response) => { - if (!response?.jsonCode) { + if (shouldIgnoreResponse || !response?.jsonCode) { return; } if (response.jsonCode === CONST.JSON_CODE.SUCCESS) { - setCodatSetupLink(String(response?.setupUrl ?? '')); + setSetupLinkFetchStatus({status: 'success', setupLink: String(response?.setupUrl ?? '')}); } else { setConnectionError(policyID, CONST.POLICY.CONNECTIONS.NAME.QBD, translate('workspace.qbd.setupPage.setupErrorTitle')); - setHasError(true); + setSetupLinkFetchStatus({status: 'error'}); } }); - }, [policyID, translate]); - useEffect(() => { - // Since QBD doesn't support Taxes, we should disable them from the LHN when connecting to QBD - enablePolicyTaxes(policyID, false); - - fetchSetupLink(); - // disabling this rule, as we want this to run only on the first render - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + return () => { + shouldIgnoreResponse = true; + }; + }, [policyID, translate]); useNetwork({ onReconnect: () => { - if (hasResultOfFetchingSetupLink) { + if (!isLoading) { return; } - fetchSetupLink(); - }, - }); - const shouldShowError = hasError; - - const children = ( - <> - {shouldShowError && ( - - - {translate('workspace.qbd.setupPage.setupErrorTitle')} - - - - - )} - {!shouldShowError && ( - - - - + // eslint-disable-next-line rulesdir/no-thenable-actions-in-views + getQuickbooksDesktopCodatSetupLink(policyID).then((response) => { + if (!response?.jsonCode) { + return; + } - {translate('workspace.qbd.setupPage.title')} - {translate('workspace.qbd.setupPage.body')} - - {!hasResultOfFetchingSetupLink ? ( - - ) : ( - - )} - - -