Skip to content

Commit

Permalink
Save WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Oct 31, 2024
1 parent b93f3aa commit d3dae92
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import './claim-ads-account.scss';
const ClaimAdsAccount = () => {
const [ updating, setUpdating ] = useState( false );
const { googleAdsAccount } = useGoogleAdsAccount();
const { fetchGoogleAdsAccountStatus } = useAppDispatch();
const { hasAccess, step } = useGoogleAdsAccountStatus();
const [ upsertAdsAccount ] = useUpsertAdsAccount();
const { fetchGoogleAdsAccountStatus, invalidateResolution } =
useAppDispatch();

const shouldClaimGoogleAdsAccount = Boolean(
googleAdsAccount?.id && hasAccess === false
Expand All @@ -43,10 +44,17 @@ const ClaimAdsAccount = () => {
useWindowFocusCallbackIntervalEffect( checkUpdatedAdsAccountStatus, 30 );

useEffect( () => {
if ( hasAccess === true && step === 'conversion_action' ) {
upsertAdsAccount();
}
}, [ hasAccess, step, upsertAdsAccount ] );
const upsertAccount = async () => {
if ( hasAccess === true && step === 'conversion_action' ) {
await upsertAdsAccount();
invalidateResolution( 'getExistingGoogleAdsAccounts', [] );
invalidateResolution( 'getGoogleAdsAccount', [] );
invalidateResolution( 'getGoogleAdsAccountStatus', [] );
}
};

upsertAccount();
}, [ hasAccess, step, upsertAdsAccount, invalidateResolution ] );

const handleOnClick = () => {
setUpdating( true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ConnectedIconLabel from '.~/components/connected-icon-label';
* @param {boolean} props.isLoading Whether the card is in a loading state.
* @param {Function} props.setValue Callback to set the value.
* @param {string} props.accountID Google Ads account ID.
* @param {boolean} props.hasResolvedAccounts Whether the existing Ads accounts and current Ads account have resolved.
* @return {JSX.Element} Body component.
*/
const ConnectAdsBody = ( {
Expand All @@ -29,6 +30,7 @@ const ConnectAdsBody = ( {
isLoading,
setValue,
accountID,
hasResolvedAccounts,
} ) => {
const getAction = () => {
if ( isLoading ) {
Expand All @@ -47,7 +49,13 @@ const ConnectAdsBody = ( {
return <ConnectButton loading />;
}

return <ConnectButton accountID={ accountID } onClick={ onClick } />;
return (
<ConnectButton
accountID={ accountID }
onClick={ onClick }
loading={ ! hasResolvedAccounts }
/>
);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ import { useAppDispatch } from '.~/data';
*/
const ConnectAds = ( { isEditing } ) => {
const [ showCreateNewModal, setShowCreateNewModal ] = useState( false );
const { existingAccounts: accounts } = useExistingGoogleAdsAccounts();
const { googleAdsAccount, refetchGoogleAdsAccount } = useGoogleAdsAccount();
const [ value, setValue ] = useState();
const [ isLoading, setLoading ] = useState( false );
const { createNotice } = useDispatchCoreNotices();
const { fetchGoogleAdsAccountStatus } = useAppDispatch();
const { hasAccess } = useGoogleAdsAccountStatus();
const isConnected = useGoogleAdsAccountReady();
const {
hasAccess,
hasFinishedResolution: hasFinishedResolutionForAccountStatus,
} = useGoogleAdsAccountStatus();
const {
existingAccounts: accounts,
hasFinishedResolution: hasFinishedResolutionForExistingAdsAccount,
} = useExistingGoogleAdsAccounts();
const {
googleAdsAccount,
refetchGoogleAdsAccount,
hasFinishedResolution: hasFinishedResolutionForCurrentAccount,
} = useGoogleAdsAccount();
const [ connectGoogleAdsAccount ] = useApiFetchCallback( {
path: '/wc/gla/ads/accounts',
method: 'POST',
Expand Down Expand Up @@ -140,6 +150,12 @@ const ConnectAds = ( { isEditing } ) => {
) }
body={
<ConnectAdsBody
hasResolvedAccounts={
hasFinishedResolutionForExistingAdsAccount &&
hasFinishedResolutionForCurrentAccount &&
hasFinishedResolutionForAccountStatus &&
isConnected !== null
}
isConnected={ isConnected }
onClick={ handleConnectClick }
isLoading={ isLoading }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
}

.gla-connected-accounts-actions {
&:empty {
display: none;
}

padding-bottom: calc((var(--large-gap)) / 2);

@media (min-width: $break-small) {
Expand Down
9 changes: 2 additions & 7 deletions js/src/hooks/useUpsertAdsAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ const useUpsertAdsAccount = () => {
// If not, it means we are creating a new account.
const { googleAdsAccount } = useGoogleAdsAccount();
const { createNotice } = useDispatchCoreNotices();
const {
fetchGoogleAdsAccount,
fetchGoogleAdsAccountStatus,
fetchExistingGoogleAdsAccounts,
} = useAppDispatch();
const { fetchGoogleAdsAccount, fetchGoogleAdsAccountStatus } =
useAppDispatch();
const [ currentAction, setCurrentAction ] = useState( null );

const isCreation = ! googleAdsAccount?.id;
Expand Down Expand Up @@ -73,7 +70,6 @@ const useUpsertAdsAccount = () => {
await Promise.all( [
fetchGoogleAdsAccount(),
fetchGoogleAdsAccountStatus(),
fetchExistingGoogleAdsAccounts(),
] );

setCurrentAction( null );
Expand All @@ -83,7 +79,6 @@ const useUpsertAdsAccount = () => {
fetchCreateAccount,
fetchGoogleAdsAccount,
fetchGoogleAdsAccountStatus,
fetchExistingGoogleAdsAccounts,
] );

return [
Expand Down

0 comments on commit d3dae92

Please sign in to comment.