11import { css } from '@emotion/react' ;
22import { isString } from '@guardian/libs' ;
33import type { ComponentEvent , TAction } from '@guardian/ophan-tracker-js' ;
4- import { space , until } from '@guardian/source/foundations' ;
4+ import { space , textSans14 , until } from '@guardian/source/foundations' ;
55import {
66 Button ,
7+ Checkbox ,
8+ CheckboxGroup ,
79 InlineError ,
810 InlineSuccess ,
911 Link ,
@@ -19,6 +21,7 @@ import { useEffect, useRef, useState } from 'react';
1921import ReactGoogleRecaptcha from 'react-google-recaptcha' ;
2022import { submitComponentEvent } from '../client/ophan/ophan' ;
2123import { lazyFetchEmailWithTimeout } from '../lib/fetchEmail' ;
24+ import { useIsSignedIn } from '../lib/useAuthStatus' ;
2225import { palette } from '../palette' ;
2326import type { RenderingTarget } from '../types/renderingTarget' ;
2427import { useConfig } from './ConfigContext' ;
@@ -96,6 +99,13 @@ const errorContainerStyles = css`
9699 }
97100` ;
98101
102+ const optInCheckboxTextSmall = css `
103+ label > div {
104+ ${ textSans14 } ;
105+ line-height: 16px;
106+ }
107+ ` ;
108+
99109const ErrorMessageWithAdvice = ( { text } : { text ?: string } ) => (
100110 < InlineError >
101111 < span >
@@ -124,6 +134,7 @@ const buildFormData = (
124134 emailAddress : string ,
125135 newsletterId : string ,
126136 token : string ,
137+ marketingOptIn ?: boolean ,
127138) : FormData => {
128139 const pageRef = window . location . origin + window . location . pathname ;
129140 const refViewId = window . guardian . ophan ?. pageViewId ?? '' ;
@@ -139,6 +150,10 @@ const buildFormData = (
139150 formData . append ( 'g-recaptcha-response' , token ) ; // TO DO - PR on form handlers - is the token verified?
140151 }
141152
153+ if ( marketingOptIn !== undefined ) {
154+ formData . append ( 'marketing' , marketingOptIn ? 'true' : 'false' ) ;
155+ }
156+
142157 return formData ;
143158} ;
144159
@@ -267,6 +282,16 @@ export const SecureSignup = ({
267282 const [ errorMessage , setErrorMessage ] = useState < string | undefined > (
268283 undefined ,
269284 ) ;
285+ const [ marketingOptIn , setMarketingOptIn ] = useState < boolean | undefined > (
286+ undefined ,
287+ ) ;
288+ const isSignedIn = useIsSignedIn ( ) ;
289+
290+ useEffect ( ( ) => {
291+ if ( isSignedIn !== 'Pending' && ! isSignedIn ) {
292+ setMarketingOptIn ( true ) ;
293+ }
294+ } , [ isSignedIn ] ) ;
270295
271296 useEffect ( ( ) => {
272297 setCaptchaSiteKey ( window . guardian . config . page . googleRecaptchaSiteKey ) ;
@@ -282,9 +307,17 @@ export const SecureSignup = ({
282307 const emailAddress : string = input ?. value ?? '' ;
283308
284309 sendTracking ( newsletterId , 'form-submission' , renderingTarget , abTest ) ;
310+
311+ const formData = buildFormData (
312+ emailAddress ,
313+ newsletterId ,
314+ token ,
315+ marketingOptIn ,
316+ ) ;
317+
285318 const response = await postFormData (
286319 window . guardian . config . page . ajaxUrl + '/email' ,
287- buildFormData ( emailAddress , newsletterId , token ) ,
320+ formData ,
288321 ) ;
289322
290323 // The response body could be accessed with await response.text()
@@ -383,6 +416,23 @@ export const SecureSignup = ({
383416 type = "email"
384417 value = { signedInUserEmail }
385418 />
419+ { isSignedIn === false && (
420+ < CheckboxGroup
421+ name = "marketing-preferences"
422+ label = "Marketing preferences"
423+ hideLabel = { true }
424+ cssOverrides = { optInCheckboxTextSmall }
425+ >
426+ < Checkbox
427+ label = "Get updates about our journalism and ways to support and enjoy our work."
428+ value = "marketing-opt-in"
429+ checked = { marketingOptIn }
430+ onChange = { ( e ) =>
431+ setMarketingOptIn ( e . target . checked )
432+ }
433+ />
434+ </ CheckboxGroup >
435+ ) }
386436 < Button onClick = { handleClick } size = "small" type = "submit" >
387437 Sign up
388438 </ Button >
0 commit comments