diff --git a/dotcom-rendering/src/components/LiveBlogEpic.importable.tsx b/dotcom-rendering/src/components/LiveBlogEpic.importable.tsx index 05e87ba470f..a834d1f25b6 100644 --- a/dotcom-rendering/src/components/LiveBlogEpic.importable.tsx +++ b/dotcom-rendering/src/components/LiveBlogEpic.importable.tsx @@ -9,7 +9,6 @@ import { createPortal } from 'react-dom'; import { submitComponentEvent } from '../client/ophan/ophan'; import { useArticleCounts } from '../lib/articleCount'; import { - getLastOneOffContributionTimestamp, shouldHideSupportMessaging, useHasOptedOutOfArticleCount, } from '../lib/contributions'; @@ -124,8 +123,6 @@ const usePayload = ({ isPaidContent, tags, showSupportMessaging: !hideSupportMessagingForUser, - lastOneOffContributionDate: - getLastOneOffContributionTimestamp() ?? undefined, mvtId, countryCode, epicViewLog: getEpicViewLog(storage.local), diff --git a/dotcom-rendering/src/components/SlotBodyEnd/ReaderRevenueEpic.tsx b/dotcom-rendering/src/components/SlotBodyEnd/ReaderRevenueEpic.tsx index 9c2c9be1e2e..7553ef049b3 100644 --- a/dotcom-rendering/src/components/SlotBodyEnd/ReaderRevenueEpic.tsx +++ b/dotcom-rendering/src/components/SlotBodyEnd/ReaderRevenueEpic.tsx @@ -1,6 +1,10 @@ import { css } from '@emotion/react'; -import { cmp } from '@guardian/libs'; -import { getCookie, startPerformanceMeasure, storage } from '@guardian/libs'; +import { + cmp, + getCookie, + startPerformanceMeasure, + storage, +} from '@guardian/libs'; import type { ComponentEvent } from '@guardian/ophan-tracker-js'; import { getEpic, getEpicViewLog } from '@guardian/support-dotcom-components'; import type { @@ -12,7 +16,6 @@ import type { import { useEffect, useState } from 'react'; import { submitComponentEvent } from '../../client/ophan/ophan'; import { - getLastOneOffContributionTimestamp, hasCmpConsentForBrowserId, hasCmpConsentForWeeklyArticleCount, hasOptedOutOfArticleCount, @@ -69,7 +72,6 @@ const buildPayload = async ( isPaidContent: data.isPaidContent, tags: data.tags, showSupportMessaging: !data.hideSupportMessagingForUser, - lastOneOffContributionDate: getLastOneOffContributionTimestamp(), epicViewLog: getEpicViewLog(storage.local), weeklyArticleHistory: await data.asyncArticleCount, diff --git a/dotcom-rendering/src/components/StickyBottomBanner/ReaderRevenueBanner.tsx b/dotcom-rendering/src/components/StickyBottomBanner/ReaderRevenueBanner.tsx index ee6903663f2..b0828463e2a 100644 --- a/dotcom-rendering/src/components/StickyBottomBanner/ReaderRevenueBanner.tsx +++ b/dotcom-rendering/src/components/StickyBottomBanner/ReaderRevenueBanner.tsx @@ -16,7 +16,6 @@ import { useEffect, useState } from 'react'; import { submitComponentEvent } from '../../client/ophan/ophan'; import type { ArticleCounts } from '../../lib/articleCount'; import { - getLastOneOffContributionDate, getPurchaseInfo, hasCmpConsentForBrowserId, hasOptedOutOfArticleCount, @@ -165,7 +164,6 @@ const buildPayload = async ({ : undefined, purchaseInfo: getPurchaseInfo(), isSignedIn, - lastOneOffContributionDate: getLastOneOffContributionDate(), hasConsented: userConsent, abandonedBasket: parseAbandonedBasket( getCookie({ name: 'GU_CO_INCOMPLETE', shouldMemoize: true }), diff --git a/dotcom-rendering/src/components/TopBarSupport.importable.tsx b/dotcom-rendering/src/components/TopBarSupport.importable.tsx index 625a33d04ca..871131f69e0 100644 --- a/dotcom-rendering/src/components/TopBarSupport.importable.tsx +++ b/dotcom-rendering/src/components/TopBarSupport.importable.tsx @@ -14,7 +14,6 @@ import type { import { useEffect, useState } from 'react'; import { submitComponentEvent } from '../client/ophan/ophan'; import { - getLastOneOffContributionDate, getPurchaseInfo, shouldHideSupportMessaging, } from '../lib/contributions'; @@ -81,7 +80,6 @@ const ReaderRevenueLinksRemote = ({ mvtId: Number( getCookie({ name: 'GU_mvt_id', shouldMemoize: true }), ), - lastOneOffContributionDate: getLastOneOffContributionDate(), purchaseInfo: getPurchaseInfo(), isSignedIn, }, diff --git a/dotcom-rendering/src/lib/contributions.test.ts b/dotcom-rendering/src/lib/contributions.test.ts index ee59149c44a..dcd998d15a8 100644 --- a/dotcom-rendering/src/lib/contributions.test.ts +++ b/dotcom-rendering/src/lib/contributions.test.ts @@ -6,7 +6,6 @@ import { HIDE_SUPPORT_MESSAGING_COOKIE, isRecentOneOffContributor, NO_RR_BANNER_KEY, - ONE_OFF_CONTRIBUTION_DATE_COOKIE, recentlyClosedBanner, setLocalNoBannerCachePeriod, SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, @@ -22,23 +21,10 @@ const clearAllCookies = () => { } }; -describe('getLastOneOffContributionDate', () => { +describe('getLastOneOffContributionTimestamp', () => { beforeEach(clearAllCookies); - it('returns date from attributes cookie if only cookie found', () => { - const somePastDate = '2020-01-28'; - setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: somePastDate, - }); - const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); - - // Our function will convert YYYY-MM-DD into a timestamp - const somePastDateToTimestamp = Date.parse(somePastDate); - expect(lastOneOffContributionDate).toBe(somePastDateToTimestamp); - }); - - it('returns a support cookie date if only cookie found', () => { + it('returns a support cookie date if found', () => { const somePastDate = 1582567969093; setCookie({ name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, @@ -48,31 +34,10 @@ describe('getLastOneOffContributionDate', () => { expect(lastOneOffContributionDate).toBe(somePastDate); }); - it('returns the most recent date if both cookies present', () => { - const muchLongerAgo = '2020-01-28'; - setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: muchLongerAgo, - }); - - const notSoLongAgo = 1582567969093; + it('returns undefined if the date cannot be parsed correctly', () => { setCookie({ name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: String(notSoLongAgo), - }); - - const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); - expect(lastOneOffContributionDate).toBe(notSoLongAgo); - }); - - it('returns an empty string if no dates can be parsed correctly', () => { - setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: 'CANT_TOUCH_THIS', - }); - setCookie({ - name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, - value: 'OR_THIS', + value: 'NOT_A_DATE', }); const lastOneOffContributionDate = getLastOneOffContributionTimestamp(); @@ -97,8 +62,8 @@ describe('isRecentOneOffContributor', () => { it('returns true if there are 5 days between the last contribution date and now', () => { setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: '2018-08-01', + name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, + value: Date.parse('2018-08-01').toString(), }); MockDate.set(Date.parse('2018-08-07T10:50:34')); @@ -106,18 +71,19 @@ describe('isRecentOneOffContributor', () => { }); it('returns true if there are 0 days between the last contribution date and now', () => { + const theDate = Date.parse('2018-08-01T13:00:30'); setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: '2018-08-01', + name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, + value: theDate.toString(), }); - MockDate.set(Date.parse('2018-08-01T13:00:30')); + MockDate.set(theDate); expect(isRecentOneOffContributor()).toBe(true); }); it('returns false if the one-off contribution was more than 3 months ago', () => { setCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - value: '2018-08-01', + name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, + value: Date.parse('2018-08-01').toString(), }); MockDate.set(Date.parse('2019-08-01T13:00:30')); expect(isRecentOneOffContributor()).toBe(false); diff --git a/dotcom-rendering/src/lib/contributions.ts b/dotcom-rendering/src/lib/contributions.ts index 71b3a8d9eaf..9729ce31fde 100644 --- a/dotcom-rendering/src/lib/contributions.ts +++ b/dotcom-rendering/src/lib/contributions.ts @@ -14,14 +14,13 @@ import type { DCRTagPageType } from '../types/tagPage'; // User Attributes API cookies (created on sign-in) export const HIDE_SUPPORT_MESSAGING_COOKIE = 'gu_hide_support_messaging'; export const RECURRING_CONTRIBUTOR_COOKIE = 'gu_recurring_contributor'; -export const ONE_OFF_CONTRIBUTION_DATE_COOKIE = 'gu_one_off_contribution_date'; export const OPT_OUT_OF_ARTICLE_COUNT_COOKIE = 'gu_article_count_opt_out'; // Support Frontend cookies (created when a contribution is made) export const SUPPORT_RECURRING_CONTRIBUTOR_MONTHLY_COOKIE = - 'gu.contributions.recurring.contrib-timestamp.Monthly'; + 'gu.contributions.recurring.contrib-timestamp.Monthly'; // TODO: delete this, no longer needed export const SUPPORT_RECURRING_CONTRIBUTOR_ANNUAL_COOKIE = - 'gu.contributions.recurring.contrib-timestamp.Annual'; + 'gu.contributions.recurring.contrib-timestamp.Annual'; // TODO: delete this, no longer needed export const SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE = 'gu.contributions.contrib-timestamp'; @@ -61,58 +60,26 @@ export const hasSupporterCookie = ( } }; -// looks at attribute and support cookies -// ONE_OFF_CONTRIBUTION_DATE_COOKIE (attributes cookie, when loggin in) -// SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE (support cookie, when making one-off contribution) -// Get the date of the latest one-off contribution by looking at the two relevant cookies -// and returning a Unix epoch string of the latest date found. +// looks at the SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE (set by support-frontend when making one-off contribution) +// and returns a Unix epoch int of the date if it exists. export const getLastOneOffContributionTimestamp = (): number | undefined => { - // Attributes cookie - expects YYYY-MM-DD - const contributionDateFromAttributes = getCookie({ - name: ONE_OFF_CONTRIBUTION_DATE_COOKIE, - }); - // Support cookies - expects Unix epoch const contributionDateFromSupport = getCookie({ name: SUPPORT_ONE_OFF_CONTRIBUTION_COOKIE, }); - if (!contributionDateFromAttributes && !contributionDateFromSupport) { + if (!contributionDateFromSupport) { return undefined; } - // Parse dates into common format so they can be compared - const parsedDateFromAttributes = contributionDateFromAttributes - ? Date.parse(contributionDateFromAttributes) - : 0; + // Parse dates into common a number const parsedDateFromSupport = contributionDateFromSupport ? parseInt(contributionDateFromSupport, 10) : 0; - // Return most recent date - // Condition only passed if 'parsedDateFromAttributes' is NOT NaN - if (parsedDateFromAttributes > parsedDateFromSupport) { - return parsedDateFromAttributes; - } - return parsedDateFromSupport || undefined; // This guards against 'parsedDateFromSupport' being NaN }; -export const getLastOneOffContributionDate = (): string | undefined => { - const timestamp = getLastOneOffContributionTimestamp(); - - if (isUndefined(timestamp)) { - return undefined; - } - - const date = new Date(timestamp); - const year = date.getFullYear(); - const month = (date.getMonth() + 1).toString().padStart(2, '0'); - const day = date.getDate().toString().padStart(2, '0'); - - return `${year}-${month}-${day}`; -}; - const dateDiffDays = (from: number, to: number): number => { const oneDayMs = 1000 * 60 * 60 * 24; const diffMs = to - from;