Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R2: Updated Enhanced Conversion flow #2361

Open
wants to merge 14 commits into
base: feature/2239-enhanced-conversions-panel
Choose a base branch
from

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/

import AppButton from '.~/components/app-button';

const ConfirmButton = ( { onConfirm = noop } ) => {
return (
<AppButton isPrimary onClick={ onConfirm }>
{ __( 'Confirm', 'google-listings-and-ads' ) }
</AppButton>
);
};

export default ConfirmButton;
100 changes: 34 additions & 66 deletions js/src/components/enhanced-conversion-tracking-settings/cta.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,66 @@
/**
* External dependencies
*/
import { noop } from 'lodash';
import { __ } from '@wordpress/i18n';
import { useCallback, useState, useEffect } from '@wordpress/element';
import { useEffect, useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
import { ENHANCED_ADS_CONVERSION_STATUS } from '.~/constants';
import { useAppDispatch } from '.~/data';
import { ENHANCED_ADS_CONVERSION_STATUS } from '.~/constants';
import AppButton from '.~/components/app-button';
import AcceptTerms from './accept-terms';
import useAcceptedCustomerDataTerms from '.~/hooks/useAcceptedCustomerDataTerms';
import useAllowEnhancedConversions from '.~/hooks/useAllowEnhancedConversions';
import useTermsPolling from './useTermsPolling';
import EnableButton from './enable-button';
import ConfirmButton from './confirm-button';
import useAutoCheckEnhancedConversionTOS from '.~/hooks/useAutoCheckEnhancedConversionTOS';
import useEnhancedConversionsSkipConfirmation from '.~/hooks/useEnhancedConversionsSkipConfirmation';

const CTA = ( {
disableLabel = __( 'Disable', 'google-listings-and-ads' ),
enableLabel = __( 'Enable', 'google-listings-and-ads' ),
onEnableClick = noop,
onDisableClick = noop,
} ) => {
const [ startBackgroundPoll, setStartBackgroundPoll ] = useState( false );
const CTA = () => {
const { updateEnhancedAdsConversionStatus } = useAppDispatch();
const { acceptedCustomerDataTerms } = useAcceptedCustomerDataTerms();
const { allowEnhancedConversions } = useAllowEnhancedConversions();
useTermsPolling( startBackgroundPoll );

const handleDisable = useCallback( () => {
if ( ! acceptedCustomerDataTerms ) {
return;
}
const {
acceptedCustomerDataTerms,
hasFinishedResolution,
isPolling,
setIsPolling,
} = useAutoCheckEnhancedConversionTOS();
const { skipConfirmation } = useEnhancedConversionsSkipConfirmation();

const handleConfirm = useCallback( () => {
updateEnhancedAdsConversionStatus(
ENHANCED_ADS_CONVERSION_STATUS.DISABLED
ENHANCED_ADS_CONVERSION_STATUS.ENABLED
);
}, [ updateEnhancedAdsConversionStatus ] );

onDisableClick();
}, [
updateEnhancedAdsConversionStatus,
acceptedCustomerDataTerms,
onDisableClick,
] );

// Turn off polling when the user has accepted the terms.
useEffect( () => {
if ( acceptedCustomerDataTerms && startBackgroundPoll ) {
setStartBackgroundPoll( false );
}
}, [ acceptedCustomerDataTerms, startBackgroundPoll ] );
// As soon as the terms are accepted, do not show the spinner
if ( acceptedCustomerDataTerms && isPolling ) {
// We automatically set the status to enabled.
handleConfirm();

const handleEnable = useCallback( () => {
if ( ! acceptedCustomerDataTerms ) {
return;
setIsPolling( false );
}
}, [ acceptedCustomerDataTerms, setIsPolling, isPolling, handleConfirm ] );

updateEnhancedAdsConversionStatus(
ENHANCED_ADS_CONVERSION_STATUS.ENABLED
);

onEnableClick();
}, [
updateEnhancedAdsConversionStatus,
acceptedCustomerDataTerms,
onEnableClick,
] );

const handleOnAcceptTerms = () => {
setStartBackgroundPoll( true );
const handleOnEnable = () => {
setIsPolling( true );
};

if ( startBackgroundPoll ) {
if ( ! hasFinishedResolution ) {
return null;
}

if ( isPolling ) {
return <AppButton isSecondary disabled loading />;
}

if ( ! acceptedCustomerDataTerms ) {
return <AcceptTerms onAcceptTerms={ handleOnAcceptTerms } />;
return <EnableButton onEnable={ handleOnEnable } />;
}

if ( allowEnhancedConversions === ENHANCED_ADS_CONVERSION_STATUS.ENABLED ) {
return (
<AppButton isPrimary isDestructive onClick={ handleDisable }>
{ disableLabel }
</AppButton>
);
if ( skipConfirmation ) {
return null;
}

// User has accepted TOS or tracking is disabled.
return (
<AppButton isPrimary onClick={ handleEnable }>
{ enableLabel }
</AppButton>
);
return <ConfirmButton onConfirm={ handleConfirm } />;
};

export default CTA;
Loading
Loading