2020-05-27 02:38:57 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-06-12 06:38:33 +00:00
|
|
|
import { withDispatch, withSelect } from '@wordpress/data';
|
2020-05-27 02:38:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
2020-06-12 06:38:33 +00:00
|
|
|
import { H } from '@woocommerce/components';
|
2020-06-10 02:59:05 +00:00
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
2020-06-10 16:46:46 +00:00
|
|
|
import { PLUGINS_STORE_NAME, useUserPreferences } from '@woocommerce/data';
|
2020-05-27 02:38:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Connect from 'dashboard/components/connect';
|
|
|
|
import { recordEvent } from 'lib/tracks';
|
2020-06-12 06:38:33 +00:00
|
|
|
import { createNoticesFromResponse } from 'lib/notices';
|
2020-05-27 02:38:57 +00:00
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
function InstallJetpackCta( {
|
|
|
|
isJetpackInstalled,
|
|
|
|
isJetpackActivated,
|
|
|
|
isJetpackConnected,
|
2020-06-12 06:38:33 +00:00
|
|
|
installAndActivatePlugins,
|
|
|
|
isInstalling,
|
2020-06-10 16:46:46 +00:00
|
|
|
} ) {
|
|
|
|
const { updateUserPreferences, ...userPrefs } = useUserPreferences();
|
2020-05-27 02:38:57 +00:00
|
|
|
const [ isConnecting, setIsConnecting ] = useState( false );
|
|
|
|
const [ isDismissed, setIsDismissed ] = useState(
|
2020-06-10 16:46:46 +00:00
|
|
|
( userPrefs.homepage_stats || {} ).installJetpackDismissed
|
2020-05-27 02:38:57 +00:00
|
|
|
);
|
|
|
|
|
2020-06-12 06:38:33 +00:00
|
|
|
async function install() {
|
2020-05-27 02:38:57 +00:00
|
|
|
recordEvent( 'statsoverview_install_jetpack' );
|
2020-06-12 06:38:33 +00:00
|
|
|
|
|
|
|
installAndActivatePlugins( [ 'jetpack' ] )
|
|
|
|
.then( () => {
|
|
|
|
setIsConnecting( ! isJetpackConnected );
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
createNoticesFromResponse( error );
|
|
|
|
} );
|
2020-05-27 02:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function dismiss() {
|
|
|
|
if ( isInstalling || isConnecting ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
const homepageStats = userPrefs.homepage_stats || {};
|
2020-05-27 02:38:57 +00:00
|
|
|
|
|
|
|
homepageStats.installJetpackDismissed = true;
|
|
|
|
|
2020-06-10 16:46:46 +00:00
|
|
|
updateUserPreferences( { homepage_stats: homepageStats } );
|
2020-05-27 02:38:57 +00:00
|
|
|
|
|
|
|
setIsDismissed( true );
|
|
|
|
recordEvent( 'statsoverview_dismiss_install_jetpack' );
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConnector() {
|
|
|
|
return (
|
|
|
|
<Connect
|
|
|
|
autoConnect
|
|
|
|
onError={ () => setIsConnecting( false ) }
|
2020-06-10 02:59:05 +00:00
|
|
|
redirectUrl={ getAdminLink( 'admin.php?page=wc-admin' ) }
|
2020-05-27 02:38:57 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-10 02:59:05 +00:00
|
|
|
const doNotShow =
|
|
|
|
isDismissed ||
|
|
|
|
( isJetpackInstalled && isJetpackActivated && isJetpackConnected );
|
|
|
|
if ( doNotShow ) {
|
2020-05-27 02:38:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-10 02:59:05 +00:00
|
|
|
function getInstallJetpackText() {
|
|
|
|
if ( ! isJetpackInstalled ) {
|
|
|
|
return __( 'Get Jetpack', 'woocommerce-admin' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isJetpackActivated ) {
|
|
|
|
return __( 'Activate Jetpack', 'woocommerce-admin' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isJetpackConnected ) {
|
|
|
|
return __( 'Connect Jetpack', 'woocommerce-admin' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-05-27 02:38:57 +00:00
|
|
|
return (
|
|
|
|
<article className="woocommerce-stats-overview__install-jetpack-promo">
|
2020-06-18 23:44:00 +00:00
|
|
|
<div className="woocommerce-stats-overview__install-jetpack-promo__content">
|
|
|
|
<H>
|
|
|
|
{ __(
|
|
|
|
'Get traffic stats with Jetpack',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</H>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Keep an eye on your views and visitors metrics with ' +
|
|
|
|
'Jetpack. Requires Jetpack plugin and a WordPress.com ' +
|
|
|
|
'account.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-05-27 02:38:57 +00:00
|
|
|
<footer>
|
2020-06-16 01:39:45 +00:00
|
|
|
<Button isSecondary onClick={ install } isBusy={ isInstalling }>
|
2020-06-10 02:59:05 +00:00
|
|
|
{ getInstallJetpackText() }
|
2020-05-27 02:38:57 +00:00
|
|
|
</Button>
|
2020-06-18 23:44:00 +00:00
|
|
|
<Button isTertiary onClick={ dismiss } isBusy={ isInstalling }>
|
2020-05-27 02:38:57 +00:00
|
|
|
{ __( 'No thanks', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</footer>
|
|
|
|
|
|
|
|
{ isConnecting && getConnector() }
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallJetpackCta.propTypes = {
|
|
|
|
/**
|
|
|
|
* Is the Jetpack plugin connected.
|
|
|
|
*/
|
|
|
|
isJetpackConnected: PropTypes.bool.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withSelect( ( select ) => {
|
2020-06-10 02:59:05 +00:00
|
|
|
const {
|
|
|
|
isJetpackConnected,
|
2020-06-12 06:38:33 +00:00
|
|
|
isPluginsRequesting,
|
2020-06-10 02:59:05 +00:00
|
|
|
getInstalledPlugins,
|
|
|
|
getActivePlugins,
|
|
|
|
} = select( PLUGINS_STORE_NAME );
|
2020-05-27 02:38:57 +00:00
|
|
|
|
|
|
|
return {
|
2020-06-12 06:38:33 +00:00
|
|
|
isInstalling:
|
|
|
|
isPluginsRequesting( 'installPlugins' ) ||
|
|
|
|
isPluginsRequesting( 'activatePlugins' ),
|
2020-06-10 02:59:05 +00:00
|
|
|
isJetpackInstalled: getInstalledPlugins().includes( 'jetpack' ),
|
|
|
|
isJetpackActivated: getActivePlugins().includes( 'jetpack' ),
|
2020-05-27 02:38:57 +00:00
|
|
|
isJetpackConnected: isJetpackConnected(),
|
|
|
|
};
|
2020-06-12 06:38:33 +00:00
|
|
|
} ),
|
|
|
|
withDispatch( ( dispatch ) => {
|
|
|
|
const { installAndActivatePlugins } = dispatch( PLUGINS_STORE_NAME );
|
|
|
|
|
|
|
|
return {
|
|
|
|
installAndActivatePlugins,
|
|
|
|
};
|
2020-05-27 02:38:57 +00:00
|
|
|
} )
|
|
|
|
)( InstallJetpackCta );
|