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-11-26 00:27:37 +00:00
|
|
|
import { Button, Modal } from '@wordpress/components';
|
2019-10-10 14:05:13 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2021-04-15 01:32:46 +00:00
|
|
|
import { initializeExPlat } from '@woocommerce/explat';
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
class UsageModal extends Component {
|
|
|
|
constructor( props ) {
|
|
|
|
super( props );
|
|
|
|
this.state = {
|
2020-05-18 19:35:50 +00:00
|
|
|
isLoadingScripts: false,
|
2020-11-26 00:27:37 +00:00
|
|
|
isRequestStarted: false,
|
2019-10-10 14:05:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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-11-26 00:27:37 +00:00
|
|
|
const { isLoadingScripts, isRequestStarted } = this.state;
|
|
|
|
|
|
|
|
// We can't rely on isRequesting props only because option update might be triggered by other component.
|
|
|
|
if ( ! isRequestStarted ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
__(
|
2021-02-10 23:57:51 +00:00
|
|
|
'There was a problem updating your preferences',
|
2020-02-14 02:23:21 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-10-10 14:05:13 +00:00
|
|
|
);
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
updateTracking( { allowTracking } ) {
|
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( () => {
|
2020-11-26 00:27:37 +00:00
|
|
|
// Don't update state if component is unmounted already
|
|
|
|
if ( ! this._isMounted ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-15 01:32:46 +00:00
|
|
|
initializeExPlat();
|
|
|
|
|
2020-05-18 19:35:50 +00:00
|
|
|
this.setState( { isLoadingScripts: false } );
|
|
|
|
} );
|
|
|
|
} else if ( ! allowTracking ) {
|
|
|
|
window.wcTracks.isEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const trackingValue = allowTracking ? 'yes' : 'no';
|
2020-11-26 00:27:37 +00:00
|
|
|
this.setState( { isRequestStarted: true } );
|
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
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this._isMounted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
const {
|
|
|
|
isRequesting,
|
|
|
|
title = __( 'Build a better WooCommerce', 'woocommerce-admin' ),
|
|
|
|
message = interpolateComponents( {
|
|
|
|
mixedString: __(
|
|
|
|
'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.',
|
|
|
|
'woocommerce-admin'
|
2019-10-10 14:05:13 +00:00
|
|
|
),
|
2020-11-26 00:27:37 +00:00
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
2021-08-03 20:56:43 +00:00
|
|
|
href="https://woocommerce.com/usage-tracking?utm_medium=product"
|
2020-11-26 00:27:37 +00:00
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
dismissActionText = __( 'No thanks', 'woocommerce-admin' ),
|
|
|
|
acceptActionText = __( 'Yes, count me in!', 'woocommerce-admin' ),
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const { isRequestStarted } = this.state;
|
|
|
|
const isBusy = isRequestStarted && isRequesting;
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
2020-11-26 00:27:37 +00:00
|
|
|
title={ title }
|
|
|
|
isDismissible={ this.props.isDismissible }
|
2019-10-10 14:05:13 +00:00
|
|
|
onRequestClose={ () => this.props.onClose() }
|
2020-11-26 00:27:37 +00:00
|
|
|
className="woocommerce-usage-modal"
|
2019-10-10 14:05:13 +00:00
|
|
|
>
|
2020-11-26 00:27:37 +00:00
|
|
|
<div className="woocommerce-usage-modal__wrapper">
|
|
|
|
<div className="woocommerce-usage-modal__message">
|
|
|
|
{ message }
|
2020-02-14 02:23:21 +00:00
|
|
|
</div>
|
2020-11-26 00:27:37 +00:00
|
|
|
<div className="woocommerce-usage-modal__actions">
|
|
|
|
<Button
|
|
|
|
isSecondary
|
|
|
|
isBusy={ isBusy }
|
|
|
|
onClick={ () =>
|
|
|
|
this.updateTracking( { allowTracking: false } )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ dismissActionText }
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isBusy }
|
|
|
|
onClick={ () =>
|
|
|
|
this.updateTracking( { allowTracking: true } )
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ acceptActionText }
|
|
|
|
</Button>
|
2019-10-10 14:05:13 +00:00
|
|
|
</div>
|
|
|
|
</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 );
|