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
2022-03-30 09:00:04 +00:00
title = { _ _ ( 'Build a Better WooCommerce' , 'woocommerce' ) }
2021-01-13 19:43:45 +00:00
onRequestClose = { ( ) => setIsModalOpen ( false ) }
className = "woocommerce-beta-features-tracking-modal"
>
< p >
{ _ _ (
'Testing new features requires sharing non-sensitive data via ' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-01-13 19:43:45 +00:00
) }
2024-04-09 08:50:15 +00:00
< a href = "https://woocommerce.com/usage-tracking?utm_medium=product" >
2022-03-30 09:00:04 +00:00
{ _ _ ( 'usage tracking' , 'woocommerce' ) }
2021-01-13 19:43:45 +00:00
< / 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.' ,
2022-03-30 09:00:04 +00:00
'woocommerce'
2021-01-13 19:43:45 +00:00
) }
< / 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 ) ;
} }
>
2022-03-30 09:00:04 +00:00
{ _ _ ( 'Save' , 'woocommerce' ) }
2021-01-13 19:43:45 +00:00
< / 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 ) ;