2019-05-17 03:04:52 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-05-23 12:10:42 +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 apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { forEach } from 'lodash';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal depdencies
|
|
|
|
*/
|
2019-05-23 12:10:42 +00:00
|
|
|
import { H, Stepper, Card } from '@woocommerce/components';
|
2019-05-28 14:45:52 +00:00
|
|
|
import { NAMESPACE } from 'wc-api/onboarding/constants';
|
2019-07-01 18:13:29 +00:00
|
|
|
import { recordEvent } from 'lib/tracks';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
2019-05-23 12:10:42 +00:00
|
|
|
const plugins = [ 'jetpack', 'woocommerce-services' ];
|
|
|
|
|
|
|
|
class Plugins extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
step: 'install',
|
|
|
|
isPending: true,
|
|
|
|
isError: false,
|
|
|
|
pluginsInstalled: 0,
|
|
|
|
pluginsActivated: 0,
|
|
|
|
connectUrl: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.activatePlugins = this.activatePlugins.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.installPlugins();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate( prevProps, prevState ) {
|
|
|
|
if (
|
|
|
|
this.state.pluginsInstalled !== prevState.pluginsInstalled &&
|
|
|
|
this.state.pluginsInstalled === plugins.length
|
|
|
|
) {
|
|
|
|
/* eslint-disable react/no-did-update-set-state */
|
|
|
|
this.setState( {
|
|
|
|
step: 'activate',
|
|
|
|
isPending: false,
|
|
|
|
} );
|
|
|
|
/* eslint-enable react/no-did-update-set-state */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this.state.pluginsActivated !== prevState.pluginsActivated &&
|
|
|
|
this.state.pluginsActivated === plugins.length
|
|
|
|
) {
|
|
|
|
this.connectJetpack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
installPlugins() {
|
|
|
|
forEach( plugins, async plugin => {
|
|
|
|
const response = await this.doPluginAction( 'install', plugin );
|
|
|
|
if ( 'success' === response.status ) {
|
|
|
|
this.setState( state => ( {
|
|
|
|
pluginsInstalled: state.pluginsInstalled + 1,
|
|
|
|
} ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
activatePlugins( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// Avoid double activating.
|
|
|
|
const { isPending } = this.state;
|
|
|
|
if ( isPending ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState( {
|
|
|
|
isPending: true,
|
|
|
|
} );
|
|
|
|
|
2019-07-01 18:13:29 +00:00
|
|
|
recordEvent( 'storeprofiler_install_plugin' );
|
|
|
|
|
2019-05-23 12:10:42 +00:00
|
|
|
forEach( plugins, async plugin => {
|
|
|
|
const response = await this.doPluginAction( 'activate', plugin );
|
|
|
|
if ( 'success' === response.status ) {
|
|
|
|
this.setState( state => ( {
|
|
|
|
pluginsActivated: state.pluginsActivated + 1,
|
|
|
|
} ) );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
getErrorMessage( action, plugin ) {
|
|
|
|
return 'install' === action
|
|
|
|
? sprintf(
|
|
|
|
__( 'There was an error installing %s. Please try again.', 'woocommerce-admin' ),
|
|
|
|
this.getPluginName( plugin )
|
|
|
|
)
|
|
|
|
: sprintf(
|
|
|
|
__( 'There was an error activating %s. Please try again.', 'woocommerce-admin' ),
|
|
|
|
this.getPluginName( plugin )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async doPluginAction( action, plugin ) {
|
|
|
|
try {
|
|
|
|
const pluginResponse = await apiFetch( {
|
2019-05-28 14:45:52 +00:00
|
|
|
path: `${ NAMESPACE }/onboarding/plugins/${ action }`,
|
2019-05-23 12:10:42 +00:00
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
plugin,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
return pluginResponse;
|
|
|
|
} catch ( err ) {
|
2019-07-10 16:58:51 +00:00
|
|
|
this.props.addNotice( {
|
|
|
|
status: 'error',
|
|
|
|
message: this.getErrorMessage( action, plugin ),
|
|
|
|
} );
|
2019-05-23 12:10:42 +00:00
|
|
|
this.setState( {
|
|
|
|
isPending: false,
|
|
|
|
isError: true,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async connectJetpack() {
|
|
|
|
try {
|
|
|
|
const connectResponse = await apiFetch( {
|
2019-05-28 14:45:52 +00:00
|
|
|
path: `${ NAMESPACE }/onboarding/plugins/connect-jetpack`,
|
2019-05-23 12:10:42 +00:00
|
|
|
} );
|
|
|
|
if ( connectResponse && connectResponse.connectAction ) {
|
|
|
|
window.location = connectResponse.connectAction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new Error();
|
|
|
|
} catch ( err ) {
|
2019-07-10 16:58:51 +00:00
|
|
|
this.props.addNotice( {
|
|
|
|
status: 'error',
|
|
|
|
message: this.getErrorMessage( 'activate', 'jetpack' ),
|
|
|
|
} );
|
2019-05-23 12:10:42 +00:00
|
|
|
this.setState( {
|
|
|
|
isPending: false,
|
|
|
|
isError: true,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getPluginName( plugin ) {
|
|
|
|
switch ( plugin ) {
|
|
|
|
case 'jetpack':
|
|
|
|
return __( 'Jetpack', 'woocommerce-admin' );
|
|
|
|
case 'woocommerce-services':
|
|
|
|
return __( 'WooCommerce Services', 'woocommerce-admin' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
render() {
|
2019-05-23 12:10:42 +00:00
|
|
|
const { step, isPending, isError } = this.state;
|
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">
|
|
|
|
{ __( 'Install plugins', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
|
|
|
|
|
|
|
<Card className="woocommerce-profile-wizard__plugins-card">
|
|
|
|
<Stepper
|
|
|
|
direction="vertical"
|
|
|
|
currentStep={ step }
|
|
|
|
isPending={ isPending }
|
|
|
|
steps={ [
|
|
|
|
{
|
|
|
|
label: __( 'Install Jetpack and WooCommerce Services', 'woocommerce-admin' ),
|
|
|
|
key: 'install',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: __( 'Activate Jetpack and WooCommerce Services', 'woocommerce-admin' ),
|
|
|
|
key: 'activate',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="woocommerce-profile-wizard__plugins-actions">
|
|
|
|
{ isError && (
|
|
|
|
<Button isPrimary onClick={ () => location.reload() }>
|
|
|
|
{ __( 'Retry', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
) }
|
|
|
|
|
|
|
|
{ ! isError &&
|
|
|
|
'activate' === step && (
|
|
|
|
<Button isPrimary isBusy={ isPending } onClick={ this.activatePlugins }>
|
|
|
|
{ __( 'Activate & continue', 'woocommerce-admin' ) }
|
2019-05-23 12:10:42 +00:00
|
|
|
</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(
|
|
|
|
withDispatch( dispatch => {
|
2019-07-10 16:58:51 +00:00
|
|
|
const { addNotice } = dispatch( 'wc-admin' );
|
2019-05-23 12:10:42 +00:00
|
|
|
return {
|
2019-07-10 16:58:51 +00:00
|
|
|
addNotice,
|
2019-05-23 12:10:42 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Plugins );
|