2021-01-13 19:43:45 +00:00
/ * *
* External dependencies
* /
import { _ _ } from '@wordpress/i18n' ;
import { useState , useEffect , useRef } from '@wordpress/element' ;
import { Button , Modal , CheckboxControl } from '@wordpress/components' ;
import { withDispatch } from '@wordpress/data' ;
import { compose } from '@wordpress/compose' ;
import { OPTIONS _STORE _NAME } from '@woocommerce/data' ;
import { recordEvent } from '@woocommerce/tracks' ;
2021-04-15 01:32:46 +00:00
import { initializeExPlat } from '@woocommerce/explat' ;
2021-01-13 19:43:45 +00:00
const BetaFeaturesTrackingModal = ( { updateOptions } ) => {
const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
const [ isChecked , setIsChecked ] = useState ( false ) ;
const enableNavigationCheckbox = useRef (
document . querySelector ( '#woocommerce_navigation_enabled' )
) ;
const setTracking = async ( allow ) => {
if ( typeof window . wcTracks . enable === 'function' ) {
if ( allow ) {
2021-04-15 01:32:46 +00:00
window . wcTracks . enable ( ( ) => {
initializeExPlat ( ) ;
} ) ;
2021-01-13 19:43:45 +00:00
} else {
window . wcTracks . isEnabled = false ;
}
}
if ( allow ) {
recordEvent ( 'settings_features_tracking_enabled' ) ;
}
return updateOptions ( {
woocommerce _allow _tracking : allow ? 'yes' : 'no' ,
} ) ;
} ;
useEffect ( ( ) => {
if ( ! enableNavigationCheckbox . current ) {
return ;
}
const listener = ( e ) => {
if ( e . target . checked ) {
e . target . checked = false ;
setIsModalOpen ( true ) ;
}
} ;
const checkbox = enableNavigationCheckbox . current ;
checkbox . addEventListener ( 'change' , listener , false ) ;
return ( ) => checkbox . removeEventListener ( 'change' , listener ) ;
} , [ ] ) ;
if ( ! enableNavigationCheckbox . current ) {
return null ;
}
if ( ! isModalOpen ) {
return null ;
}
return (
< Modal
title = { _ _ ( 'Build a Better WooCommerce' , 'woocommerce-admin' ) }
onRequestClose = { ( ) => setIsModalOpen ( false ) }
className = "woocommerce-beta-features-tracking-modal"
>
< p >
{ _ _ (
'Testing new features requires sharing non-sensitive data via ' ,
'woocommerce-admin'
) }
< a href = "https://woocommerce.com/usage-tracking" >
{ _ _ ( 'usage tracking' , 'woocommerce-admin' ) }
< / a >
{ _ _ (
'. Gathering usage data allows us to make WooCommerce better — your store will be considered as we evaluate new features, judge the quality of an update, or determine if an improvement makes sense. No personal data is tracked or stored and you can opt-out at any time.' ,
'woocommerce-admin'
) }
< / p >
< div className = "woocommerce-beta-features-tracking-modal__checkbox" >
< CheckboxControl
label = "Enable usage tracking"
onChange = { setIsChecked }
checked = { isChecked }
/ >
< / d i v >
< div className = "woocommerce-beta-features-tracking-modal__actions" >
< Button
isPrimary
onClick = { async ( ) => {
if ( isChecked ) {
await setTracking ( true ) ;
enableNavigationCheckbox . current . checked = true ;
} else {
await setTracking ( false ) ;
}
setIsModalOpen ( false ) ;
} }
>
{ _ _ ( 'Save' , 'woocommerce-admin' ) }
< / B u t t o n >
< / d i v >
< / M o d a l >
) ;
} ;
export const BetaFeaturesTrackingContainer = compose (
withDispatch ( ( dispatch ) => {
const { updateOptions } = dispatch ( OPTIONS _STORE _NAME ) ;
return { updateOptions } ;
} )
) ( BetaFeaturesTrackingModal ) ;