Skip to content

Commit

Permalink
Move useGoogleAccountCheck to its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Jan 30, 2024
1 parent b44aadc commit bc4520c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
22 changes: 8 additions & 14 deletions js/src/components/google-ads-account-card/non-connected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import SpinnerCard from '.~/components/spinner-card';
import CreateAccount from './create-account';
import useExistingGoogleAdsAccounts from '.~/hooks/useExistingGoogleAdsAccounts';
import useGoogleAccount from '.~/hooks/useGoogleAccount';
import useGoogleAccountCheck from '.~/hooks/useGoogleAccountCheck';
import ConnectAds from './connect-ads';

const useGoogleAccountCheck = () => {
const { google } = useGoogleAccount();
const { existingAccounts } =
google && google.active !== 'no'
? // eslint-disable-next-line react-hooks/rules-of-hooks
useExistingGoogleAdsAccounts()
: { existingAccounts: null };

return { google, existingAccounts };
};

const NonConnected = () => {
const { google, existingAccounts } = useGoogleAccountCheck();
const { google, existingAccounts, hasFinishedResolution } =
useGoogleAccountCheck();
const [ ignoreExisting, setIgnoreExisting ] = useState( false );

const handleShowExisting = () => {
setIgnoreExisting( false );
};

if ( ! hasFinishedResolution ) {
return <SpinnerCard />;
}

if (
! existingAccounts ||
existingAccounts.length === 0 ||
Expand Down
64 changes: 64 additions & 0 deletions js/src/hooks/useGoogleAccountCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { glaData } from '.~/constants';
import { STORE_KEY } from '.~/data/constants';
import toScopeState from '.~/utils/toScopeState';
import useJetpackAccount from './useJetpackAccount';

const useGoogleAccountCheck = () => {
const {
jetpack,
isResolving: isResolvingJetpack,
hasFinishedResolution: hasFinishedResolutionJetpack,
} = useJetpackAccount();

return useSelect(
( select ) => {
if ( ! jetpack || jetpack.active === 'no' ) {
return {
google: undefined,
scope: toScopeState( glaData.adsSetupComplete ),
isResolving: isResolvingJetpack,
hasFinishedResolution: hasFinishedResolutionJetpack,
};
}

const {
getGoogleAccount,
getExistingGoogleAdsAccounts,
isResolving,
hasFinishedResolution,
} = select( STORE_KEY );
const google = getGoogleAccount();

if ( google?.active === 'no' ) {
return {
google,
existingAccounts: null,
isResolving: isResolving( 'getGoogleAccount' ),
hasFinishedResolution:
hasFinishedResolution( 'getGoogleAccount' ),
};
}

const existingAccounts = getExistingGoogleAdsAccounts();
return {
google,
existingAccounts,
isResolving: isResolving( 'getExistingGoogleAdsAccounts' ),
hasFinishedResolution: hasFinishedResolution(
'getExistingGoogleAdsAccounts'
),
};
},
[ jetpack, isResolvingJetpack, hasFinishedResolutionJetpack ]
);
};

export default useGoogleAccountCheck;

0 comments on commit bc4520c

Please sign in to comment.