-
Notifications
You must be signed in to change notification settings - Fork 21
/
setup-paid-ads.js
161 lines (143 loc) · 4.75 KB
/
setup-paid-ads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { useState } from '@wordpress/element';
import { noop } from 'lodash';
/**
* Internal dependencies
*/
import useAdminUrl from '~/hooks/useAdminUrl';
import useDispatchCoreNotices from '~/hooks/useDispatchCoreNotices';
import useAdsSetupCompleteCallback from '~/hooks/useAdsSetupCompleteCallback';
import useTargetAudienceFinalCountryCodes from '~/hooks/useTargetAudienceFinalCountryCodes';
import AdsCampaign from '~/components/paid-ads/ads-campaign';
import CampaignAssetsForm from '~/components/paid-ads/campaign-assets-form';
import AppButton from '~/components/app-button';
import useGoogleAdsAccountBillingStatus from '~/hooks/useGoogleAdsAccountBillingStatus';
import { getProductFeedUrl } from '~/utils/urls';
import { API_NAMESPACE } from '~/data/constants';
import { GUIDE_NAMES, GOOGLE_ADS_BILLING_STATUS } from '~/constants';
import { ACTION_COMPLETE, ACTION_SKIP } from './constants';
import SkipButton from './skip-button';
import clientSession from './clientSession';
import useBudgetRecommendation from '~/hooks/useBudgetRecommendation';
import AppSpinner from '~/components/app-spinner';
/**
* Clicking on the "Complete setup" button to complete the onboarding flow with paid ads.
*
* @event gla_onboarding_complete_with_paid_ads_button_click
* @property {number} budget The budget for the campaign
* @property {string} audiences The targeted audiences for the campaign
*/
/**
* Renders the onboarding step for setting up the paid ads (Google Ads account and paid campaign)
* or skipping it, and then completing the onboarding flow.
* @fires gla_onboarding_complete_with_paid_ads_button_click
*/
export default function SetupPaidAds() {
const adminUrl = useAdminUrl();
const [ completing, setCompleting ] = useState( null );
const { createNotice } = useDispatchCoreNotices();
const { data: countryCodes } = useTargetAudienceFinalCountryCodes();
const { highestDailyBudget, hasFinishedResolution } =
useBudgetRecommendation( countryCodes );
const [ handleSetupComplete ] = useAdsSetupCompleteCallback();
const { billingStatus } = useGoogleAdsAccountBillingStatus();
const isBillingCompleted =
billingStatus?.status === GOOGLE_ADS_BILLING_STATUS.APPROVED;
const finishOnboardingSetup = async ( onBeforeFinish = noop ) => {
try {
await onBeforeFinish();
await apiFetch( {
path: `${ API_NAMESPACE }/mc/settings/sync`,
method: 'POST',
} );
} catch ( e ) {
setCompleting( null );
createNotice(
'error',
__(
'Unable to complete your setup.',
'google-listings-and-ads'
)
);
}
// Force reload WC admin page to initiate the relevant dependencies of the Dashboard page.
const query = { guide: GUIDE_NAMES.SUBMISSION_SUCCESS };
window.location.href = adminUrl + getProductFeedUrl( query );
};
const handleSkipCreatePaidAds = async () => {
setCompleting( ACTION_SKIP );
await finishOnboardingSetup();
};
const createSkipButton = ( formContext ) => {
const { isValidForm } = formContext;
return (
<SkipButton
isValidForm={ isValidForm }
onSkipCreatePaidAds={ handleSkipCreatePaidAds }
disabled={ completing === ACTION_COMPLETE }
loading={ completing === ACTION_SKIP }
/>
);
};
const createContinueButton = ( formContext ) => {
const { isValidForm, values } = formContext;
const { amount } = values;
const disabled =
completing === ACTION_SKIP || ! isValidForm || ! isBillingCompleted;
const handleCompleteClick = async () => {
setCompleting( ACTION_COMPLETE );
const onBeforeFinish = handleSetupComplete.bind(
null,
amount,
countryCodes
);
await finishOnboardingSetup( onBeforeFinish );
};
return (
<AppButton
isPrimary
disabled={ disabled }
onClick={ handleCompleteClick }
loading={ completing === ACTION_COMPLETE }
text={ __( 'Complete setup', 'google-listings-and-ads' ) }
eventName="gla_onboarding_complete_with_paid_ads_button_click"
eventProps={ {
budget: amount,
audiences: countryCodes?.join( ',' ),
} }
/>
);
};
const paidAds = {
amount: highestDailyBudget,
...clientSession.getCampaign(),
};
if ( ! hasFinishedResolution || ! countryCodes ) {
return <AppSpinner />;
}
return (
<CampaignAssetsForm
initialCampaign={ paidAds }
recommendedDailyBudget={ highestDailyBudget }
onChange={ ( _, values ) => {
if ( values.amount >= highestDailyBudget ) {
clientSession.setCampaign( values );
}
} }
>
<AdsCampaign
headerTitle={ __(
'Create a campaign to advertise your products',
'google-listings-and-ads'
) }
continueButton={ createContinueButton }
skipButton={ createSkipButton }
context="setup-mc"
/>
</CampaignAssetsForm>
);
}