Skip to content

Commit e588c62

Browse files
michaelclaphamRichard Bangay
andcommitted
Attempt to fill in email in newsletters sign up using bridget function (if apps)
Co-authored-by: Richard Bangay <richard.bangay@guardian.co.uk>
1 parent 2b077f6 commit e588c62

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

dotcom-rendering/src/lib/useNewsletterSignupForm.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { clearSubscriptionCache } from './newsletterSubscriptionCache';
2323
import { useAuthStatus, useIsSignedIn } from './useAuthStatus';
2424
import { useBrowserId } from './useBrowserId';
25+
import { getNewslettersClient } from './bridgetApi';
2526

2627
// ---------------------------------------------------------------------------
2728
// Helpers (kept local — not part of the public API)
@@ -305,17 +306,36 @@ 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-
});
311+
console.log('after useEffect fill in email');
312+
if (renderingTarget === 'Apps') {
313+
// Fill in email using apps bridget API
314+
void getNewslettersClient()
315+
.getLoggedInUserEmail()
316+
.then((maybeEmail) => {
317+
console.log('after getLoggedInUserEmail ', maybeEmail);
318+
const email = maybeEmail.emailAddress;
319+
if (!email) return;
320+
setUserEmail(email);
321+
setHasPrefilledEmail(true);
322+
setIsInteracted(true);
323+
})
324+
.catch((reason) => {
325+
console.log('after getLoggedInUserEmail catch ', reason);
326+
});
327+
} else {
328+
if (emailFetchStartedRef.current) return;
329+
if (isSignedIn === 'Pending') return;
330+
emailFetchStartedRef.current = true;
331+
332+
void resolveUserEmail(isSignedIn).then((email) => {
333+
if (!isString(email)) return;
334+
setUserEmail(email);
335+
setHasPrefilledEmail(true);
336+
setIsInteracted(true);
337+
});
338+
}
319339
}, [isSignedIn]);
320340

321341
const submitForm = useCallback(

0 commit comments

Comments
 (0)