2019-05-17 03:04:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-08-23 12:56:57 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2019-12-02 17:39:22 +00:00
|
|
|
import { Button } from '@wordpress/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-10-07 11:51:25 +00:00
|
|
|
import { difference } from 'lodash';
|
2019-05-23 12:10:42 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* WooCommerce dependencies
|
2019-05-17 03:04:52 +00:00
|
|
|
*/
|
2019-05-23 12:10:42 +00:00
|
|
|
import { H, Stepper, Card } from '@woocommerce/components';
|
2020-03-03 09:44:26 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
2019-08-23 12:56:57 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-08-23 12:56:57 +00:00
|
|
|
*/
|
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' ];
|
2019-10-07 11:51:25 +00:00
|
|
|
const { activePlugins = [] } = getSetting( 'onboarding', {} );
|
2019-08-23 12:56:57 +00:00
|
|
|
// We want to use the cached version of activePlugins here, otherwise the list we are dealing with could update as plugins are activated.
|
2019-10-07 11:51:25 +00:00
|
|
|
const plugins = difference( pluginsToInstall, 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() {
|
2020-03-03 09:44:26 +00:00
|
|
|
const { goToNextStep } = this.props;
|
|
|
|
if ( plugins.length === 0 ) {
|
|
|
|
goToNextStep();
|
|
|
|
return;
|
2019-08-23 12:56:57 +00:00
|
|
|
}
|
2019-10-24 16:20:32 +00:00
|
|
|
|
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,
|
2020-03-03 09:44:26 +00:00
|
|
|
goToNextStep,
|
2019-08-23 12:56:57 +00:00
|
|
|
installedPlugins,
|
|
|
|
activatedPlugins,
|
|
|
|
} = this.props;
|
2019-08-21 06:34:21 +00:00
|
|
|
|
|
|
|
const newErrors = difference( errors, prevProps.errors );
|
2020-02-14 02:23:21 +00:00
|
|
|
newErrors.map( ( error ) => createNotice( 'error', error ) );
|
2019-08-21 06:34:21 +00:00
|
|
|
|
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
|
|
|
|
2020-03-03 09:44:26 +00:00
|
|
|
// Complete this step if all plugins are active.
|
2019-08-23 12:56:57 +00:00
|
|
|
if (
|
|
|
|
prevProps.activatedPlugins.length !== plugins.length &&
|
2020-03-03 09:44:26 +00:00
|
|
|
activatedPlugins.length === plugins.length
|
2019-08-23 12:56:57 +00:00
|
|
|
) {
|
2020-03-03 09:44:26 +00:00
|
|
|
goToNextStep();
|
2019-08-23 12:56:57 +00:00
|
|
|
}
|
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() {
|
2020-03-03 09:44:26 +00:00
|
|
|
const { hasErrors, isRequesting } = this.props;
|
2019-08-21 06:34:21 +00:00
|
|
|
const { step } = this.state;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const pluginLabel = plugins
|
|
|
|
.map( ( pluginSlug ) => pluginNames[ pluginSlug ] )
|
|
|
|
.join( ' & ' );
|
2019-08-23 12:56:57 +00:00
|
|
|
|
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={ [
|
|
|
|
{
|
2020-02-14 02:23:21 +00:00
|
|
|
label: sprintf(
|
|
|
|
__( 'Install %s', 'woocommerce-admin' ),
|
|
|
|
pluginLabel
|
|
|
|
),
|
2019-05-27 16:37:02 +00:00
|
|
|
key: 'install',
|
|
|
|
},
|
|
|
|
{
|
2020-02-14 02:23:21 +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 && (
|
2020-02-14 02:23:21 +00:00
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
onClick={ () => window.location.reload() }
|
|
|
|
>
|
2019-05-27 16:37:02 +00:00
|
|
|
{ __( 'Retry', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
{ ! hasErrors && step === 'activate' && (
|
|
|
|
<Button
|
|
|
|
isPrimary
|
2020-03-03 09:44:26 +00:00
|
|
|
isBusy={ isRequesting }
|
2020-02-14 02:23:21 +00:00
|
|
|
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(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2019-08-21 06:34:21 +00:00
|
|
|
const {
|
|
|
|
getPluginInstallations,
|
|
|
|
getPluginInstallationErrors,
|
|
|
|
getPluginActivations,
|
|
|
|
getPluginActivationErrors,
|
|
|
|
isPluginActivateRequesting,
|
|
|
|
isPluginInstallRequesting,
|
|
|
|
} = select( 'wc-api' );
|
|
|
|
const activationErrors = getPluginActivationErrors( plugins );
|
|
|
|
const activatedPlugins = Object.keys( getPluginActivations( plugins ) );
|
|
|
|
const installationErrors = getPluginInstallationErrors( plugins );
|
2020-02-14 02:23:21 +00:00
|
|
|
const installedPlugins = Object.keys(
|
|
|
|
getPluginInstallations( plugins )
|
|
|
|
);
|
2019-08-21 06:34:21 +00:00
|
|
|
|
|
|
|
const errors = [];
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( activationErrors ).map( ( plugin ) =>
|
2019-08-21 06:34:21 +00:00
|
|
|
errors.push( activationErrors[ plugin ].message )
|
|
|
|
);
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( installationErrors ).map( ( plugin ) =>
|
2019-08-21 06:34:21 +00:00
|
|
|
errors.push( installationErrors[ plugin ].message )
|
|
|
|
);
|
|
|
|
const hasErrors = Boolean( errors.length );
|
|
|
|
|
2019-11-14 14:14:53 +00:00
|
|
|
const isRequesting =
|
2020-03-03 09:44:26 +00:00
|
|
|
isPluginActivateRequesting() || isPluginInstallRequesting();
|
2019-11-14 14:14:53 +00:00
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
return {
|
|
|
|
activatedPlugins,
|
|
|
|
installedPlugins,
|
|
|
|
errors,
|
|
|
|
hasErrors,
|
|
|
|
isRequesting,
|
|
|
|
};
|
|
|
|
} ),
|
2020-02-14 02:23:21 +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 );
|