Skip to content

Commit 5bd5b11

Browse files
author
Juarez Mota
committed
refactor: replace choice card URL building logic with getChoiceCardUrl function
1 parent 608f3ec commit 5bd5b11

File tree

3 files changed

+28
-42
lines changed

3 files changed

+28
-42
lines changed

dotcom-rendering/src/components/marketing/banners/designableBanner/DesignableBanner.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../../../../lib/useMatchMedia';
3131
import { getChoiceCards } from '../../lib/choiceCards';
3232
import type { ReactComponent } from '../../lib/ReactComponent';
33-
import { addChoiceCardsProductParams } from '../../lib/tracking';
33+
import { getChoiceCardUrl } from '../../lib/tracking';
3434
import { ThreeTierChoiceCards } from '../../shared/ThreeTierChoiceCards';
3535
import { bannerWrapper, validatedBannerWrapper } from '../common/BannerWrapper';
3636
import type { BannerRenderProps } from '../common/types';
@@ -108,24 +108,6 @@ const buildChoiceCardSettings = (
108108
return undefined;
109109
};
110110

111-
const buildUrlForThreeTierChoiceCards = (
112-
baseUrl: string,
113-
selectedChoiceCard: ChoiceCard,
114-
) => {
115-
const { destinationUrl, product } = selectedChoiceCard;
116-
const url =
117-
destinationUrl && destinationUrl.trim() !== ''
118-
? destinationUrl.trim()
119-
: baseUrl;
120-
return product.supportTier === 'OneOff'
121-
? url
122-
: addChoiceCardsProductParams(
123-
url,
124-
product.supportTier,
125-
product.ratePlan,
126-
);
127-
};
128-
129111
const DesignableBanner: ReactComponent<BannerRenderProps> = ({
130112
content,
131113
onCloseClick,
@@ -422,9 +404,9 @@ const DesignableBanner: ReactComponent<BannerRenderProps> = ({
422404

423405
<div css={styles.ctaContainer}>
424406
<LinkButton
425-
href={buildUrlForThreeTierChoiceCards(
426-
mainOrMobileContent.primaryCta.ctaUrl,
407+
href={getChoiceCardUrl(
427408
selectedChoiceCard,
409+
mainOrMobileContent.primaryCta.ctaUrl,
428410
)}
429411
onClick={onCtaClick}
430412
priority="tertiary"

dotcom-rendering/src/components/marketing/epics/ctas/ContributionsEpicButtons.tsx

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import { useEffect } from 'react';
1818
import { useIsInView } from '../../../../lib/useIsInView';
1919
import { hasSetReminder } from '../../lib/reminders';
2020
import {
21-
addChoiceCardsOneTimeParams,
22-
addChoiceCardsProductParams,
2321
enrichSupportUrl,
22+
getChoiceCardUrl,
2423
isSupportUrl,
2524
} from '../../lib/tracking';
2625
import {
@@ -183,27 +182,9 @@ export const ContributionsEpicButtons = ({
183182
return cta;
184183
}
185184

186-
const { destinationUrl, product } = threeTierSelectedChoiceCard;
187-
188-
const url: string =
189-
destinationUrl && destinationUrl.trim() !== ''
190-
? destinationUrl.trim()
191-
: cta.baseUrl;
192-
193-
if (product.supportTier === 'OneOff') {
194-
return {
195-
text: cta.text,
196-
baseUrl: addChoiceCardsOneTimeParams(url),
197-
};
198-
}
199-
200185
return {
201186
text: cta.text,
202-
baseUrl: addChoiceCardsProductParams(
203-
url,
204-
product.supportTier,
205-
product.ratePlan,
206-
),
187+
baseUrl: getChoiceCardUrl(threeTierSelectedChoiceCard, cta.baseUrl),
207188
};
208189
};
209190

dotcom-rendering/src/components/marketing/lib/tracking.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
TargetingAbTest,
1717
Tracking,
1818
} from '@guardian/support-dotcom-components/dist/shared/types';
19+
import type { ChoiceCard } from '@guardian/support-dotcom-components/dist/shared/types/props/choiceCards';
1920

2021
// TRACKING VIA support.theguardian.com
2122
type LinkParams = {
@@ -305,6 +306,28 @@ export const addTrackingParamsToProfileUrl = (
305306
: baseUrl;
306307
};
307308

309+
export const getChoiceCardUrl = (
310+
choiceCard: ChoiceCard,
311+
baseUrl: string,
312+
): string => {
313+
const { destinationUrl, product } = choiceCard;
314+
315+
const url: string =
316+
destinationUrl && destinationUrl.trim() !== ''
317+
? destinationUrl.trim()
318+
: baseUrl;
319+
320+
if (product.supportTier === 'OneOff') {
321+
return addChoiceCardsOneTimeParams(url);
322+
}
323+
324+
return addChoiceCardsProductParams(
325+
url,
326+
product.supportTier,
327+
product.ratePlan,
328+
);
329+
};
330+
308331
// SHARED TRACKING
309332
const campaignPrefix = 'gdnwb_copts_memco';
310333

0 commit comments

Comments
 (0)