Skip to content

Commit

Permalink
Simplify setting default value.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvinb committed Oct 14, 2024
1 parent 25ed467 commit d7f0d51
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions js/src/setup-mc/setup-stepper/setup-paid-ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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 validateCampaign from '.~/components/paid-ads/validateCampaign';
import SkipButton from './skip-button';
import clientSession from './clientSession';

Expand All @@ -40,29 +39,6 @@ import clientSession from './clientSession';
* @property {boolean} isValid Whether the campaign data are valid values.
*/

const defaultPaidAds = {
amount: 0,
isValid: false,
};

/**
* Resolve the initial paid ads data from the given paid ads data.
* Parts of the resolved data are used in the `initialCampaign` prop of `CampaignAssetsForm` component.
*
* @return {PaidAdsData} The resolved paid ads data.
*/
function resolveInitialPaidAds() {
const startingPaidAds = {
...defaultPaidAds,
...clientSession.getCampaign(),
};
const nextPaidAds = { ...startingPaidAds };
nextPaidAds.isValid = ! Object.keys( validateCampaign( nextPaidAds ) )
.length;

return nextPaidAds;
}

/**
* 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.
Expand All @@ -75,7 +51,6 @@ export default function SetupPaidAds() {
const { data: countryCodes } = useTargetAudienceFinalCountryCodes();
const [ handleSetupComplete ] = useAdsSetupCompleteCallback();
const { billingStatus } = useGoogleAdsAccountBillingStatus();
const paidAds = resolveInitialPaidAds();

const isBillingCompleted =
billingStatus?.status === GOOGLE_ADS_BILLING_STATUS.APPROVED;
Expand Down Expand Up @@ -156,11 +131,16 @@ export default function SetupPaidAds() {
);
};

const paidAds = {
amount: 0,
...clientSession.getCampaign(),
};

return (
<CampaignAssetsForm
initialCampaign={ paidAds }
onChange={ ( _, values, isValid ) => {
clientSession.setCampaign( { ...values, isValid } );
onChange={ ( _, values ) => {
clientSession.setCampaign( { ...values } );
} }
>
<AdsCampaign
Expand Down

0 comments on commit d7f0d51

Please sign in to comment.