2019-05-17 03:04:52 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-08-23 12:56:57 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-05-30 06:31:07 +00:00
|
|
|
import { Button } from 'newspack-components';
|
2019-05-17 03:04:52 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2019-05-23 12:10:42 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-08-23 12:56:57 +00:00
|
|
|
import { difference, get } from 'lodash';
|
2019-05-23 12:10:42 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-23 12:56:57 +00:00
|
|
|
* WooCommerce depdencies
|
2019-05-17 03:04:52 +00:00
|
|
|
*/
|
2019-05-23 12:10:42 +00:00
|
|
|
import { H, Stepper, Card } from '@woocommerce/components';
|
2019-08-23 12:56:57 +00:00
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal depdencies
|
|
|
|
*/
|
2019-07-01 18:13:29 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-08-21 06:34:21 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-08-23 12:56:57 +00:00
|
|
|
import { pluginNames } from 'wc-api/onboarding/constants';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
const pluginsToInstall = [ 'jetpack', 'woocommerce-services' ];
|
|
|
|
// We want to use the cached version of activePlugins here, otherwise the list we are dealing with could update as plugins are activated.
|
|
|
|
const plugins = difference(
|
|
|
|
pluginsToInstall,
|
|
|
|
get( wcSettings, [ 'onboarding', 'activePlugins' ], [] )
|
|
|
|
);
|
2019-05-23 12:10:42 +00:00
|
|
|
|
|
|
|
class Plugins extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
step: 'install',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.activatePlugins = this.activatePlugins.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-08-23 12:56:57 +00:00
|
|
|
if ( 0 === plugins.length ) {
|
|
|
|
return updateQueryString( { step: 'store-details' } );
|
|
|
|
}
|
2019-08-21 06:34:21 +00:00
|
|
|
this.props.installPlugins( plugins );
|
2019-05-23 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
componentDidUpdate( prevProps ) {
|
2019-08-23 12:56:57 +00:00
|
|
|
const {
|
|
|
|
createNotice,
|
|
|
|
errors,
|
|
|
|
installedPlugins,
|
|
|
|
activatedPlugins,
|
|
|
|
jetpackConnectUrl,
|
|
|
|
} = this.props;
|
2019-08-21 06:34:21 +00:00
|
|
|
|
|
|
|
if ( jetpackConnectUrl ) {
|
|
|
|
window.location = jetpackConnectUrl;
|
2019-05-23 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
const newErrors = difference( errors, prevProps.errors );
|
|
|
|
newErrors.map( error => createNotice( 'error', error ) );
|
|
|
|
|
2019-05-23 12:10:42 +00:00
|
|
|
if (
|
2019-08-21 06:34:21 +00:00
|
|
|
prevProps.installedPlugins.length !== plugins.length &&
|
|
|
|
installedPlugins.length === plugins.length
|
2019-05-23 12:10:42 +00:00
|
|
|
) {
|
2019-08-21 06:34:21 +00:00
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( { step: 'activate' } );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
2019-05-23 12:10:42 +00:00
|
|
|
}
|
2019-08-23 12:56:57 +00:00
|
|
|
|
|
|
|
// If Jetpack was already connected, we can go to store details after WCS is activated.
|
|
|
|
if (
|
|
|
|
! plugins.includes( 'jetpack' ) &&
|
|
|
|
prevProps.activatedPlugins.length !== plugins.length &&
|
|
|
|
activatedPlugins.length === plugins.length
|
|
|
|
) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
return updateQueryString( { step: 'store-details' } );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
2019-05-23 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
async activatePlugins( event ) {
|
2019-05-23 12:10:42 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// Avoid double activating.
|
2019-08-21 06:34:21 +00:00
|
|
|
const { isRequesting } = this.props;
|
|
|
|
if ( isRequesting ) {
|
2019-05-23 12:10:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-01 18:13:29 +00:00
|
|
|
recordEvent( 'storeprofiler_install_plugin' );
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
this.props.activatePlugins( plugins );
|
2019-05-23 12:10:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
render() {
|
2019-08-21 06:34:21 +00:00
|
|
|
const { hasErrors, isRequesting } = this.props;
|
|
|
|
const { step } = this.state;
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
const pluginLabel = plugins.includes( 'jetpack' )
|
|
|
|
? Object.values( pluginNames ).join( ' & ' )
|
|
|
|
: pluginNames[ 'woocommerce-services' ];
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-05-27 16:37:02 +00:00
|
|
|
<H className="woocommerce-profile-wizard__header-title">
|
2019-08-08 18:41:26 +00:00
|
|
|
{ __( 'Enhance your store setup', 'woocommerce-admin' ) }
|
2019-05-27 16:37:02 +00:00
|
|
|
</H>
|
|
|
|
|
|
|
|
<Card className="woocommerce-profile-wizard__plugins-card">
|
|
|
|
<Stepper
|
2019-08-01 23:28:06 +00:00
|
|
|
isVertical={ true }
|
2019-05-27 16:37:02 +00:00
|
|
|
currentStep={ step }
|
2019-08-21 06:34:21 +00:00
|
|
|
isPending={ isRequesting && ! hasErrors }
|
2019-05-27 16:37:02 +00:00
|
|
|
steps={ [
|
|
|
|
{
|
2019-08-23 12:56:57 +00:00
|
|
|
label: sprintf( __( 'Install %s', 'woocommerce-admin' ), pluginLabel ),
|
2019-05-27 16:37:02 +00:00
|
|
|
key: 'install',
|
|
|
|
},
|
|
|
|
{
|
2019-08-23 12:56:57 +00:00
|
|
|
label: sprintf( __( 'Activate %s', 'woocommerce-admin' ), pluginLabel ),
|
2019-05-27 16:37:02 +00:00
|
|
|
key: 'activate',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="woocommerce-profile-wizard__plugins-actions">
|
2019-08-21 06:34:21 +00:00
|
|
|
{ hasErrors && (
|
2019-05-27 16:37:02 +00:00
|
|
|
<Button isPrimary onClick={ () => location.reload() }>
|
|
|
|
{ __( 'Retry', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
{ ! ( hasErrors && 'activate' === step ) && (
|
|
|
|
<Button isPrimary isBusy={ isRequesting } onClick={ this.activatePlugins }>
|
|
|
|
{ __( 'Activate & continue', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
2019-05-27 16:37:02 +00:00
|
|
|
</div>
|
|
|
|
</Card>
|
2019-05-17 03:04:52 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-05-23 12:10:42 +00:00
|
|
|
|
|
|
|
export default compose(
|
2019-08-21 06:34:21 +00:00
|
|
|
withSelect( select => {
|
|
|
|
const {
|
|
|
|
getJetpackConnectUrl,
|
|
|
|
isGetJetpackConnectUrlRequesting,
|
|
|
|
getJetpackConnectUrlError,
|
|
|
|
getPluginInstallations,
|
|
|
|
getPluginInstallationErrors,
|
|
|
|
getPluginActivations,
|
|
|
|
getPluginActivationErrors,
|
|
|
|
isPluginActivateRequesting,
|
|
|
|
isPluginInstallRequesting,
|
|
|
|
} = select( 'wc-api' );
|
|
|
|
|
|
|
|
const isRequesting =
|
|
|
|
isPluginActivateRequesting() || isPluginInstallRequesting() || getJetpackConnectUrlError();
|
|
|
|
|
|
|
|
const activationErrors = getPluginActivationErrors( plugins );
|
|
|
|
const activatedPlugins = Object.keys( getPluginActivations( plugins ) );
|
|
|
|
const installationErrors = getPluginInstallationErrors( plugins );
|
|
|
|
const installedPlugins = Object.keys( getPluginInstallations( plugins ) );
|
|
|
|
|
|
|
|
const isJetpackConnectUrlRequesting = isGetJetpackConnectUrlRequesting();
|
|
|
|
const jetpackConnectUrlError = getJetpackConnectUrlError();
|
|
|
|
let jetpackConnectUrl = null;
|
|
|
|
if ( activatedPlugins.includes( 'jetpack' ) ) {
|
|
|
|
jetpackConnectUrl = getJetpackConnectUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
const errors = [];
|
|
|
|
Object.keys( activationErrors ).map( plugin =>
|
|
|
|
errors.push( activationErrors[ plugin ].message )
|
|
|
|
);
|
|
|
|
Object.keys( installationErrors ).map( plugin =>
|
|
|
|
errors.push( installationErrors[ plugin ].message )
|
|
|
|
);
|
|
|
|
if ( jetpackConnectUrlError ) {
|
2019-08-23 12:56:57 +00:00
|
|
|
errors.push( jetpackConnectUrlError.message );
|
2019-08-21 06:34:21 +00:00
|
|
|
}
|
|
|
|
const hasErrors = Boolean( errors.length );
|
|
|
|
|
|
|
|
return {
|
|
|
|
activatedPlugins,
|
|
|
|
installedPlugins,
|
|
|
|
jetpackConnectUrl,
|
|
|
|
isJetpackConnectUrlRequesting,
|
|
|
|
errors,
|
|
|
|
hasErrors,
|
|
|
|
isRequesting,
|
|
|
|
};
|
|
|
|
} ),
|
2019-05-23 12:10:42 +00:00
|
|
|
withDispatch( dispatch => {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2019-08-21 06:34:21 +00:00
|
|
|
const { activatePlugins, installPlugins } = dispatch( 'wc-api' );
|
|
|
|
|
2019-05-23 12:10:42 +00:00
|
|
|
return {
|
2019-08-21 06:34:21 +00:00
|
|
|
activatePlugins,
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2019-08-21 06:34:21 +00:00
|
|
|
installPlugins,
|
2019-05-23 12:10:42 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Plugins );
|