Skip to content

Commit 3f450c8

Browse files
michaelclaphamRichard Bangay
andauthored
Newsletter sign ups - use bridget to fill in logged in user email in native apps (#16567)
* Update bridget to 8.14.0 Co-authored-by: Richard Bangay <richard.bangay@guardian.co.uk> * Attempt to fill in email in newsletters sign up using bridget function (if apps) Co-authored-by: Richard Bangay <richard.bangay@guardian.co.uk> * Make renderingTarget a useEffect dependency and lint fix * Only console.log in exception catch --------- Co-authored-by: Richard Bangay <richard.bangay@guardian.co.uk>
1 parent c10b55b commit 3f450c8

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

dotcom-rendering/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@emotion/server": "11.11.0",
3030
"@guardian/ab-testing-config": "workspace:ab-testing-config",
3131
"@guardian/braze-components": "23.0.2",
32-
"@guardian/bridget": "8.13.1",
32+
"@guardian/bridget": "8.14.0",
3333
"@guardian/browserslist-config": "6.1.0",
3434
"@guardian/cdk": "catalog:",
3535
"@guardian/commercial-core": "35.1.0",

dotcom-rendering/src/lib/useNewsletterSignupForm.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type React from 'react';
55
import { useCallback, useEffect, useRef, useState } from 'react';
66
import type ReactGoogleRecaptcha from 'react-google-recaptcha';
77
import type { RenderingTarget } from '../types/renderingTarget';
8+
import { getNewslettersClient } from './bridgetApi';
89
import { lazyFetchEmailWithTimeout } from './fetchEmail';
910
import {
1011
getEffectiveMarketingOptIn,
@@ -305,18 +306,38 @@ export const useNewsletterSignupForm = (
305306
useEffect(() => {
306307
setCaptchaSiteKey(window.guardian.config.page.googleRecaptchaSiteKey);
307308
}, []);
309+
308310
useEffect(() => {
309-
if (emailFetchStartedRef.current) return;
310-
if (isSignedIn === 'Pending') return;
311-
emailFetchStartedRef.current = true;
312-
313-
void resolveUserEmail(isSignedIn).then((email) => {
314-
if (!isString(email)) return;
315-
setUserEmail(email);
316-
setHasPrefilledEmail(true);
317-
setIsInteracted(true);
318-
});
319-
}, [isSignedIn]);
311+
if (renderingTarget === 'Apps') {
312+
// Fill in email using apps bridget API
313+
void getNewslettersClient()
314+
.getLoggedInUserEmail()
315+
.then((maybeEmail) => {
316+
const email = maybeEmail.emailAddress;
317+
if (!email) return;
318+
setUserEmail(email);
319+
setHasPrefilledEmail(true);
320+
setIsInteracted(true);
321+
})
322+
.catch((reason) => {
323+
console.log(
324+
'Failed to getLoggedInUserEmail from bridget ',
325+
reason,
326+
);
327+
});
328+
} else {
329+
if (emailFetchStartedRef.current) return;
330+
if (isSignedIn === 'Pending') return;
331+
emailFetchStartedRef.current = true;
332+
333+
void resolveUserEmail(isSignedIn).then((email) => {
334+
if (!isString(email)) return;
335+
setUserEmail(email);
336+
setHasPrefilledEmail(true);
337+
setIsInteracted(true);
338+
});
339+
}
340+
}, [isSignedIn, renderingTarget]);
320341

321342
const submitForm = useCallback(
322343
async (emailAddress: string, token: string): Promise<void> => {

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)