From e316fd7da9643cebfc00048a727948fb54e7b563 Mon Sep 17 00:00:00 2001 From: Adam Iskounen Date: Fri, 25 Oct 2024 15:48:02 -0400 Subject: [PATCH 1/3] conditionally display email enrollment checkbox during sign-up --- .../Onboarding/Auth2/hooks/useCountryCode.tsx | 77 +++++++++++++++++++ .../Auth2/scenes/SignUpNameStep.tsx | 22 ++++-- 2 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/app/Scenes/Onboarding/Auth2/hooks/useCountryCode.tsx diff --git a/src/app/Scenes/Onboarding/Auth2/hooks/useCountryCode.tsx b/src/app/Scenes/Onboarding/Auth2/hooks/useCountryCode.tsx new file mode 100644 index 00000000000..e25c2e51eff --- /dev/null +++ b/src/app/Scenes/Onboarding/Auth2/hooks/useCountryCode.tsx @@ -0,0 +1,77 @@ +import { useCountryCodeQuery } from "__generated__/useCountryCodeQuery.graphql" +import { useClientQuery } from "app/utils/useClientQuery" +import { useEffect, useState } from "react" +import { getIpAddress } from "react-native-device-info" +import { graphql } from "react-relay" + +const USE_COUNTRY_CODE_QUERY = graphql` + query useCountryCodeQuery($ip: String!) { + requestLocation(ip: $ip) { + countryCode + } + } +` + +export const useCountryCode = () => { + const [ip, setIp] = useState("0.0.0.0") + + useEffect(() => { + getIpAddress().then((ip) => { + setIp(ip) + }) + }, []) + + const { data, loading, error } = useClientQuery({ + query: USE_COUNTRY_CODE_QUERY, + variables: { + ip, + }, + cacheConfig: { + networkCacheConfig: { + force: false, + }, + }, + }) + + const countryCode = data?.requestLocation?.countryCode + + const isAutomaticallySubscribed = !!(countryCode && !GDPR_COUNTRY_CODES.includes(countryCode)) + + return { + countryCode, + error, + isAutomaticallySubscribed, + loading, + } +} + +export const GDPR_COUNTRY_CODES = [ + "AT", + "BE", + "BG", + "CY", + "CZ", + "DE", + "DK", + "EE", + "ES", + "FI", + "FR", + "GB", + "GR", + "HR", + "HU", + "IE", + "IT", + "LT", + "LU", + "LV", + "MT", + "NL", + "PL", + "PT", + "RO", + "SE", + "SI", + "SK", +] diff --git a/src/app/Scenes/Onboarding/Auth2/scenes/SignUpNameStep.tsx b/src/app/Scenes/Onboarding/Auth2/scenes/SignUpNameStep.tsx index 8927a7e6fa8..0f8be9d77d9 100644 --- a/src/app/Scenes/Onboarding/Auth2/scenes/SignUpNameStep.tsx +++ b/src/app/Scenes/Onboarding/Auth2/scenes/SignUpNameStep.tsx @@ -4,6 +4,7 @@ import { useAuthNavigation, useAuthScreen, } from "app/Scenes/Onboarding/Auth2/hooks/useAuthNavigation" +import { useCountryCode } from "app/Scenes/Onboarding/Auth2/hooks/useCountryCode" import { useInputAutofocus } from "app/Scenes/Onboarding/Auth2/hooks/useInputAutofocus" import { OnboardingNavigationStack } from "app/Scenes/Onboarding/Onboarding" import { EmailSubscriptionCheckbox } from "app/Scenes/Onboarding/OnboardingCreateAccount/EmailSubscriptionCheckbox" @@ -23,13 +24,19 @@ interface SignUpNameStepFormValues { export const SignUpNameStep: React.FC = () => { const screen = useAuthScreen() + const { loading, isAutomaticallySubscribed } = useCountryCode() + + // TODO: Show a skeleton loader + if (loading) { + return null + } return ( initialValues={{ name: "", acceptedTerms: false, - agreedToReceiveEmails: false, + agreedToReceiveEmails: isAutomaticallySubscribed, }} validationSchema={Yup.object().shape({ name: Yup.string().trim().required("Full name field is required"), @@ -84,6 +91,7 @@ const SignUpNameStepForm: React.FC = () => { const navigation = useAuthNavigation() const parentNavigation = useNavigation>() + const { isAutomaticallySubscribed } = useCountryCode() const { color } = useTheme() const nameRef = useRef(null) @@ -146,10 +154,14 @@ const SignUpNameStepForm: React.FC = () => { error={highlightTerms} navigation={parentNavigation} /> - setFieldValue("agreedToReceiveEmails", !values.agreedToReceiveEmails)} - checked={values.agreedToReceiveEmails} - /> + {!isAutomaticallySubscribed ? ( + setFieldValue("agreedToReceiveEmails", !values.agreedToReceiveEmails)} + checked={values.agreedToReceiveEmails} + /> + ) : ( + + )}