2019-10-10 14:05:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-06-10 23:49:27 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2019-10-10 14:05:13 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
CheckboxControl,
|
|
|
|
FormToggle,
|
|
|
|
Modal,
|
|
|
|
} from '@wordpress/components';
|
2019-10-10 14:05:13 +00:00
|
|
|
|
2020-06-10 23:49:27 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
|
2019-10-10 14:05:13 +00:00
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-10-10 14:05:13 +00:00
|
|
|
*/
|
|
|
|
import { Link } from '@woocommerce/components';
|
|
|
|
|
|
|
|
class UsageModal extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
|
|
|
allowTracking: props.allowTracking,
|
2020-05-18 19:35:50 +00:00
|
|
|
isLoadingScripts: false,
|
2019-10-10 14:05:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.onTrackingChange = this.onTrackingChange.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
onTrackingChange() {
|
|
|
|
this.setState( {
|
|
|
|
allowTracking: ! this.state.allowTracking,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:35:50 +00:00
|
|
|
async componentDidUpdate( prevProps, prevState ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const {
|
|
|
|
hasErrors,
|
|
|
|
isRequesting,
|
|
|
|
onClose,
|
|
|
|
onContinue,
|
|
|
|
createNotice,
|
|
|
|
} = this.props;
|
2020-05-18 19:35:50 +00:00
|
|
|
const { isLoadingScripts } = this.state;
|
2020-02-14 02:23:21 +00:00
|
|
|
const isRequestSuccessful =
|
2020-05-18 19:35:50 +00:00
|
|
|
! isRequesting &&
|
|
|
|
! isLoadingScripts &&
|
|
|
|
( prevProps.isRequesting || prevState.isLoadingScripts ) &&
|
|
|
|
! hasErrors;
|
2020-02-14 02:23:21 +00:00
|
|
|
const isRequestError =
|
|
|
|
! isRequesting && prevProps.isRequesting && hasErrors;
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
if ( isRequestSuccessful ) {
|
|
|
|
onClose();
|
|
|
|
onContinue();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isRequestError ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
|
|
|
'There was a problem updating your preferences.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-10-10 14:05:13 +00:00
|
|
|
);
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTracking() {
|
2020-05-18 19:35:50 +00:00
|
|
|
const { allowTracking } = this.state;
|
2019-10-10 14:05:13 +00:00
|
|
|
const { updateOptions } = this.props;
|
2020-05-18 19:35:50 +00:00
|
|
|
|
|
|
|
if ( allowTracking && typeof window.wcTracks.enable === 'function' ) {
|
|
|
|
this.setState( { isLoadingScripts: true } );
|
|
|
|
window.wcTracks.enable( () => {
|
|
|
|
this.setState( { isLoadingScripts: false } );
|
|
|
|
} );
|
|
|
|
} else if ( ! allowTracking ) {
|
|
|
|
window.wcTracks.isEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const trackingValue = allowTracking ? 'yes' : 'no';
|
2019-10-10 14:05:13 +00:00
|
|
|
updateOptions( {
|
2020-05-18 19:35:50 +00:00
|
|
|
woocommerce_allow_tracking: trackingValue,
|
2019-10-10 14:05:13 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-07-13 22:35:20 +00:00
|
|
|
// Bail if site has already opted in to tracking
|
|
|
|
if ( this.props.allowTracking ) {
|
|
|
|
const { onClose, onContinue } = this.props;
|
|
|
|
onClose();
|
|
|
|
onContinue();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-10 14:05:13 +00:00
|
|
|
const { allowTracking } = this.state;
|
|
|
|
const { isRequesting } = this.props;
|
|
|
|
const trackingMessage = interpolateComponents( {
|
|
|
|
mixedString: __(
|
2019-12-10 04:08:54 +00:00
|
|
|
'Get improved features and faster fixes by sharing non-sensitive data via {{link}}usage tracking{{/link}} ' +
|
|
|
|
'that shows us how WooCommerce is used. No personal data is tracked or stored.',
|
2019-10-10 14:05:13 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
link: (
|
2020-02-14 02:23:21 +00:00
|
|
|
<Link
|
|
|
|
href="https://woocommerce.com/usage-tracking"
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
2019-10-10 14:05:13 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
2020-02-14 02:23:21 +00:00
|
|
|
title={ __(
|
2020-04-24 10:23:45 +00:00
|
|
|
'Build a better WooCommerce',
|
2020-02-14 02:23:21 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-10-10 14:05:13 +00:00
|
|
|
onRequestClose={ () => this.props.onClose() }
|
|
|
|
className="woocommerce-profile-wizard__usage-modal"
|
|
|
|
>
|
|
|
|
<div className="woocommerce-profile-wizard__usage-wrapper">
|
2020-02-14 02:23:21 +00:00
|
|
|
<div className="woocommerce-profile-wizard__usage-modal-message">
|
|
|
|
{ trackingMessage }
|
|
|
|
</div>
|
2019-10-10 14:05:13 +00:00
|
|
|
<div className="woocommerce-profile-wizard__tracking">
|
|
|
|
<CheckboxControl
|
|
|
|
className="woocommerce-profile-wizard__tracking-checkbox"
|
|
|
|
checked={ allowTracking }
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Yes, count me in!',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-10-10 14:05:13 +00:00
|
|
|
onChange={ this.onTrackingChange }
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormToggle
|
|
|
|
aria-hidden="true"
|
|
|
|
checked={ allowTracking }
|
|
|
|
onChange={ this.onTrackingChange }
|
2020-02-14 02:23:21 +00:00
|
|
|
onClick={ ( e ) => e.stopPropagation() }
|
2019-10-10 14:05:13 +00:00
|
|
|
tabIndex="-1"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isRequesting }
|
|
|
|
onClick={ () => this.updateTracking() }
|
|
|
|
>
|
|
|
|
{ __( 'Continue', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
|
|
|
const {
|
2020-06-10 23:49:27 +00:00
|
|
|
getOption,
|
|
|
|
getOptionsUpdatingError,
|
|
|
|
isOptionsUpdating,
|
|
|
|
} = select( OPTIONS_STORE_NAME );
|
2019-10-10 14:05:13 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const allowTracking =
|
2020-06-10 23:49:27 +00:00
|
|
|
getOption( 'woocommerce_allow_tracking' ) === 'yes';
|
|
|
|
const isRequesting = Boolean( isOptionsUpdating() );
|
|
|
|
const hasErrors = Boolean( getOptionsUpdatingError() );
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
allowTracking,
|
|
|
|
isRequesting,
|
|
|
|
hasErrors,
|
|
|
|
};
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-10-10 14:05:13 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2020-06-10 23:49:27 +00:00
|
|
|
const { updateOptions } = dispatch( OPTIONS_STORE_NAME );
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
updateOptions,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( UsageModal );
|