| import { |
| recordTracksEvent, |
| getTrackingPrefs, |
| setTrackingPrefs, |
| isRegionInCcpaZone, |
| } from '@automattic/calypso-analytics'; |
| import cookie from 'cookie'; |
| import { useCallback, useEffect, useState } from 'react'; |
| import { useDispatch } from 'react-redux'; |
| import { saveUserSettings } from 'calypso/state/user-settings/actions'; |
| import { refreshCountryCodeCookieGdpr } from '.'; |
|
|
| export default () => { |
| const [ shouldSeeDoNotSell, setShouldSeeDoNotSell ] = useState( false ); |
| const [ isDoNotSell, setIsDoNotSell ] = useState( false ); |
| const dispatch = useDispatch(); |
|
|
| useEffect( () => { |
| const controller = new AbortController(); |
|
|
| refreshCountryCodeCookieGdpr( controller.signal ) |
| .then( () => { |
| const cookies = cookie.parse( document.cookie ); |
|
|
| setShouldSeeDoNotSell( isRegionInCcpaZone( cookies.country_code, cookies.region ) ); |
| } ) |
| .catch( () => { |
| |
| setShouldSeeDoNotSell( true ); |
| } ); |
|
|
| return () => controller.abort(); |
| }, [ setShouldSeeDoNotSell ] ); |
|
|
| useEffect( () => { |
| |
| setIsDoNotSell( ! getTrackingPrefs().buckets.advertising ); |
| }, [] ); |
|
|
| const setUserAdvertisingOptOut = useCallback( |
| ( isOptedOut: boolean ) => { |
| dispatch( saveUserSettings( { advertising_targeting_opt_out: isOptedOut } ) ); |
| }, |
| [ saveUserSettings ] |
| ); |
|
|
| const onSetDoNotSell = useCallback( |
| ( isActive: boolean ) => { |
| |
| |
| const prefs = setTrackingPrefs( { ok: true, buckets: { advertising: ! isActive } } ); |
|
|
| if ( isActive ) { |
| recordTracksEvent( 'a8c_ccpa_optout', { |
| source: 'calypso', |
| hostname: window.location.hostname, |
| pathname: window.location.pathname, |
| } ); |
| } |
|
|
| setUserAdvertisingOptOut( isActive ); |
| setIsDoNotSell( ! prefs.buckets.advertising ); |
| }, |
| [ setIsDoNotSell ] |
| ); |
|
|
| return { shouldSeeDoNotSell, onSetDoNotSell, setUserAdvertisingOptOut, isDoNotSell }; |
| }; |
|
|