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';
|
2022-02-21 02:34:25 +00:00
|
|
|
import interpolateComponents from '@automattic/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,
|
2022-06-09 09:30:06 +00:00
|
|
|
selectedAction: null,
|
2019-10-10 14:05:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:35:50 +00:00
|
|
|
async componentDidUpdate( prevProps, prevState ) {
|
2022-06-21 08:37:34 +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',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2020-02-14 02:23:21 +00:00
|
|
|
)
|
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() {
|
2021-08-10 18:58:01 +00:00
|
|
|
const { allowTracking, isResolving, onClose, onContinue } = this.props;
|
|
|
|
|
|
|
|
if ( isResolving ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:35:20 +00:00
|
|
|
// Bail if site has already opted in to tracking
|
2021-08-10 18:58:01 +00:00
|
|
|
if ( allowTracking ) {
|
2022-08-25 02:33:31 +00:00
|
|
|
onClose();
|
|
|
|
onContinue();
|
2020-07-13 22:35:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-26 00:27:37 +00:00
|
|
|
const {
|
|
|
|
isRequesting,
|
2022-03-30 09:00:04 +00:00
|
|
|
title = __( 'Build a better WooCommerce', 'woocommerce' ),
|
2020-11-26 00:27:37 +00:00
|
|
|
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.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2019-10-10 14:05:13 +00:00
|
|
|
),
|
2020-11-26 00:27:37 +00:00
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
2024-04-09 08:50:15 +00:00
|
|
|
href="https://woocommerce.com/usage-tracking?utm_medium=product"
|
2020-11-26 00:27:37 +00:00
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} ),
|
2022-03-30 09:00:04 +00:00
|
|
|
dismissActionText = __( 'No thanks', 'woocommerce' ),
|
|
|
|
acceptActionText = __( 'Yes, count me in!', 'woocommerce' ),
|
2020-11-26 00:27:37 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2022-06-09 09:30:06 +00:00
|
|
|
const { isRequestStarted, selectedAction } = this.state;
|
2020-11-26 00:27:37 +00:00
|
|
|
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
|
2022-06-09 09:30:06 +00:00
|
|
|
isBusy={ isBusy && selectedAction === 'dismiss' }
|
|
|
|
disabled={ isBusy && selectedAction === 'accept' }
|
|
|
|
onClick={ () => {
|
|
|
|
this.setState( { selectedAction: 'dismiss' } );
|
|
|
|
this.updateTracking( { allowTracking: false } );
|
|
|
|
} }
|
2020-11-26 00:27:37 +00:00
|
|
|
>
|
|
|
|
{ dismissActionText }
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
isPrimary
|
2022-06-09 09:30:06 +00:00
|
|
|
isBusy={ isBusy && selectedAction === 'accept' }
|
|
|
|
disabled={ isBusy && selectedAction === 'dismiss' }
|
|
|
|
onClick={ () => {
|
|
|
|
this.setState( { selectedAction: 'accept' } );
|
|
|
|
this.updateTracking( { allowTracking: true } );
|
|
|
|
} }
|
2020-11-26 00:27:37 +00:00
|
|
|
>
|
|
|
|
{ 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,
|
2021-08-10 18:58:01 +00:00
|
|
|
hasFinishedResolution,
|
2020-06-10 23:49:27 +00:00
|
|
|
} = select( OPTIONS_STORE_NAME );
|
2019-10-10 14:05:13 +00:00
|
|
|
|
|
|
|
return {
|
2021-08-10 18:58:01 +00:00
|
|
|
allowTracking: getOption( 'woocommerce_allow_tracking' ) === 'yes',
|
|
|
|
isRequesting: Boolean( isOptionsUpdating() ),
|
|
|
|
isResolving:
|
|
|
|
! hasFinishedResolution( 'getOption', [
|
|
|
|
'woocommerce_allow_tracking',
|
|
|
|
] ) ||
|
|
|
|
typeof getOption( 'woocommerce_allow_tracking' ) ===
|
|
|
|
'undefined',
|
|
|
|
hasErrors: Boolean( getOptionsUpdatingError() ),
|
2019-10-10 14:05:13 +00:00
|
|
|
};
|
|
|
|
} ),
|
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 );
|