2019-08-01 18:09:08 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Component } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
import { omit } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* WooCommerce dependencies
|
2019-08-01 18:09:08 +00:00
|
|
|
*/
|
|
|
|
import { getHistory, getNewPath } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-08-01 18:09:08 +00:00
|
|
|
*/
|
2019-10-01 15:09:13 +00:00
|
|
|
import { WC_ADMIN_NAMESPACE } from 'wc-api/constants';
|
2019-08-01 18:09:08 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
|
|
|
|
|
|
|
class Connect extends Component {
|
|
|
|
componentDidMount() {
|
|
|
|
document.body.classList.add( 'woocommerce-admin-is-loading' );
|
|
|
|
const { query } = this.props;
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( query.deny === '1' ) {
|
2019-08-01 18:09:08 +00:00
|
|
|
this.errorMessage(
|
|
|
|
__(
|
|
|
|
'You must click approve to install your extensions and connect to WooCommerce.com.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! query[ 'wccom-connected' ] || ! query.request_token ) {
|
|
|
|
this.request();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
baseQuery() {
|
|
|
|
const { query } = this.props;
|
|
|
|
const baseQuery = omit( { ...query, page: 'wc-admin' }, [
|
|
|
|
'task',
|
|
|
|
'wccom-connected',
|
|
|
|
'request_token',
|
|
|
|
'deny',
|
|
|
|
] );
|
|
|
|
return getNewPath( {}, '/', baseQuery );
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage(
|
|
|
|
message = __(
|
|
|
|
'There was an error connecting to WooCommerce.com. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
document.body.classList.remove( 'woocommerce-admin-is-loading' );
|
|
|
|
getHistory().push( this.baseQuery() );
|
|
|
|
this.props.createNotice( 'error', message );
|
|
|
|
}
|
|
|
|
|
|
|
|
async request() {
|
|
|
|
try {
|
|
|
|
const connectResponse = await apiFetch( {
|
2019-10-01 15:09:13 +00:00
|
|
|
path: `${ WC_ADMIN_NAMESPACE }/onboarding/plugins/request-wccom-connect`,
|
2019-08-01 18:09:08 +00:00
|
|
|
method: 'POST',
|
|
|
|
} );
|
|
|
|
if ( connectResponse && connectResponse.connectAction ) {
|
|
|
|
window.location = connectResponse.connectAction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new Error();
|
|
|
|
} catch ( err ) {
|
|
|
|
this.errorMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async finish() {
|
|
|
|
const { query } = this.props;
|
|
|
|
try {
|
|
|
|
const connectResponse = await apiFetch( {
|
2019-10-01 15:09:13 +00:00
|
|
|
path: `${ WC_ADMIN_NAMESPACE }/onboarding/plugins/finish-wccom-connect`,
|
2019-08-01 18:09:08 +00:00
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
request_token: query.request_token,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
if ( connectResponse && connectResponse.success ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
await this.props.updateProfileItems( {
|
|
|
|
wccom_connected: true,
|
|
|
|
} );
|
2019-08-01 18:09:08 +00:00
|
|
|
if ( ! this.props.isProfileItemsError ) {
|
|
|
|
this.props.createNotice(
|
|
|
|
'success',
|
|
|
|
__(
|
|
|
|
'Store connected to WooCommerce.com and extensions are being installed.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// @todo Show a notice for when extensions are correctly installed.
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
document.body.classList.remove(
|
|
|
|
'woocommerce-admin-is-loading'
|
|
|
|
);
|
2019-08-01 18:09:08 +00:00
|
|
|
getHistory().push( this.baseQuery() );
|
|
|
|
} else {
|
|
|
|
this.errorMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new Error();
|
|
|
|
} catch ( err ) {
|
|
|
|
this.errorMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withSelect( ( select ) => {
|
2019-08-01 18:09:08 +00:00
|
|
|
const { getProfileItemsError } = select( 'wc-api' );
|
|
|
|
|
|
|
|
const isProfileItemsError = Boolean( getProfileItemsError() );
|
|
|
|
|
|
|
|
return { isProfileItemsError };
|
|
|
|
} ),
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-08-01 18:09:08 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
const { updateProfileItems } = dispatch( 'wc-api' );
|
|
|
|
return {
|
|
|
|
createNotice,
|
|
|
|
updateProfileItems,
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( Connect );
|