Skip to content

Commit 5a31639

Browse files
authored
Merge pull request #14779 from guardian/jm/fix-mandatory-gate-for-non-auxia-users
add getGateDismissedCount function for dismissed gate logic
2 parents bd014ac + 0fb31b5 commit 5a31639

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

dotcom-rendering/src/components/SignInGate/dismissGate.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
hasUserDismissedGate,
33
hasUserDismissedGateMoreThanCount,
44
incrementUserDismissedGateCount,
5+
retrieveLastGateDismissedCount,
56
setUserDismissedGate,
67
unsetUserDismissedGate,
78
} from './dismissGate';
@@ -206,4 +207,49 @@ describe('SignInGate - dismissGate methods', () => {
206207
).toBe(false);
207208
});
208209
});
210+
211+
describe('getGateDismissedCount', () => {
212+
test('returns 0 when no treatmentId has been saved yet (first visit)', () => {
213+
const count = retrieveLastGateDismissedCount('AuxiaSignInGate');
214+
expect(count).toBe(0);
215+
});
216+
217+
test('returns the count using the last saved treatmentId', () => {
218+
// Simulate incrementing with a specific treatmentId
219+
incrementUserDismissedGateCount('14414017', 'AuxiaSignInGate');
220+
incrementUserDismissedGateCount('14414017', 'AuxiaSignInGate');
221+
222+
const count = retrieveLastGateDismissedCount('AuxiaSignInGate');
223+
expect(count).toBe(2);
224+
});
225+
226+
test('returns correct count when treatmentId changes between visits', () => {
227+
// First visit with treatmentId 'default-treatment-id'
228+
incrementUserDismissedGateCount(
229+
'default-treatment-id',
230+
'AuxiaSignInGate',
231+
);
232+
incrementUserDismissedGateCount(
233+
'default-treatment-id',
234+
'AuxiaSignInGate',
235+
);
236+
237+
expect(retrieveLastGateDismissedCount('AuxiaSignInGate')).toBe(2);
238+
239+
// Later visit with treatmentId '14414017'
240+
incrementUserDismissedGateCount('14414017', 'AuxiaSignInGate');
241+
242+
// Should now return count for '14414017' since it's the last one saved
243+
expect(retrieveLastGateDismissedCount('AuxiaSignInGate')).toBe(1);
244+
});
245+
246+
test('handles different gate names independently', () => {
247+
incrementUserDismissedGateCount('variant-1', 'GateA');
248+
incrementUserDismissedGateCount('variant-2', 'GateB');
249+
250+
expect(retrieveLastGateDismissedCount('GateA')).toBe(1);
251+
expect(retrieveLastGateDismissedCount('GateB')).toBe(1);
252+
expect(retrieveLastGateDismissedCount('GateC')).toBe(0);
253+
});
254+
});
209255
});

dotcom-rendering/src/components/SignInGate/dismissGate.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,22 @@ export const incrementUserDismissedGateCount = (
127127
const prefs = getSigninGatePrefsSafely();
128128
const key = localStorageDismissedCountKey(variant, name);
129129
prefs[key] = retrieveDismissedCount(variant, name) + 1;
130+
prefs[`last-treatment-id-${name}`] = variant; // Also save this as the last known treatmentId for this gate
131+
130132
setSigninGatePrefs(prefs);
131133
};
134+
135+
/**
136+
* Get the dismissed count using the last known treatmentId if available.
137+
* Returns 0 if no treatmentId has been saved yet (first visit).
138+
*/
139+
export const retrieveLastGateDismissedCount = (name: string): number => {
140+
const prefs = getSigninGatePrefsSafely();
141+
const lastTreatmentId = prefs[`last-treatment-id-${name}`];
142+
143+
if (typeof lastTreatmentId === 'string') {
144+
return retrieveDismissedCount(lastTreatmentId, name);
145+
}
146+
147+
return 0; // First visit - no treatmentId saved yet, return 0
148+
};

dotcom-rendering/src/components/StickyBottomBanner.importable.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { usePageViewId } from '../lib/usePageViewId';
2323
import type { RenderingTarget } from '../types/renderingTarget';
2424
import type { TagType } from '../types/tag';
2525
import { useConfig } from './ConfigContext';
26-
import { retrieveDismissedCount } from './SignInGate/dismissGate';
2726
import type { AuxiaGateDisplayData } from './SignInGate/types';
2827
import {
2928
BrazeBanner,
@@ -204,7 +203,6 @@ const buildSignInGateConfig = (
204203
contentType,
205204
sectionId,
206205
tags,
207-
retrieveDismissedCount,
208206
);
209207
},
210208
show: (meta: AuxiaGateDisplayData) => () => (

dotcom-rendering/src/components/StickyBottomBanner/SignInGatePortal.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ describe('SignInGatePortal', () => {
9393
'Article', // contentType (must be a valid content type)
9494
'section', // sectionId
9595
[], // tags
96-
() => 0, // retrieveDismissedCount
9796
);
9897

9998
expect(result).toEqual({ show: true, meta: auxiaReturn });
@@ -134,7 +133,6 @@ describe('SignInGatePortal', () => {
134133
'Article',
135134
'section',
136135
[],
137-
() => 0,
138136
);
139137

140138
expect(result).toEqual({ show: true, meta: auxiaReturn });

dotcom-rendering/src/components/StickyBottomBanner/SignInGatePortal.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { CanShowResult } from '../../lib/messagePicker';
66
import { useAuthStatus } from '../../lib/useAuthStatus';
77
import type { TagType } from '../../types/tag';
88
import { Island } from '../Island';
9+
import { retrieveLastGateDismissedCount } from '../SignInGate/dismissGate';
910
import { pageIdIsAllowedForGating } from '../SignInGate/displayRules';
1011
import type { AuxiaGateDisplayData } from '../SignInGate/types';
1112
import {
@@ -149,7 +150,6 @@ export const canShowSignInGatePortal = async (
149150
contentType?: string,
150151
sectionId?: string,
151152
tags?: TagType[],
152-
retrieveDismissedCount?: (variant: string, name: string) => number,
153153
): Promise<CanShowResult<AuxiaGateDisplayData>> => {
154154
// Check if the sign-in gate placeholder exists in the DOM
155155
const targetElement = document.getElementById('sign-in-gate');
@@ -171,8 +171,7 @@ export const canShowSignInGatePortal = async (
171171
editionId === undefined ||
172172
contentType === undefined ||
173173
sectionId === undefined ||
174-
tags === undefined ||
175-
retrieveDismissedCount === undefined
174+
tags === undefined
176175
) {
177176
return Promise.resolve({ show: false });
178177
}
@@ -185,7 +184,7 @@ export const canShowSignInGatePortal = async (
185184
contentType,
186185
sectionId,
187186
tags,
188-
retrieveDismissedCount('auxia-signin-gate', 'AuxiaSignInGate'),
187+
retrieveLastGateDismissedCount('AuxiaSignInGate'),
189188
);
190189

191190
return {

0 commit comments

Comments
 (0)