2019-05-07 19:25:51 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-05-30 07:15:39 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-05-27 16:37:02 +00:00
|
|
|
import { Component, createElement, Fragment } from '@wordpress/element';
|
2019-05-28 03:09:48 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-05-27 16:37:02 +00:00
|
|
|
import { pick } from 'lodash';
|
2019-05-28 03:09:48 +00:00
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
2019-05-07 19:25:51 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-05-07 19:25:51 +00:00
|
|
|
*/
|
2019-06-12 03:56:10 +00:00
|
|
|
import BusinessDetails from './steps/business-details';
|
2019-12-31 08:50:45 +00:00
|
|
|
import CartModal from '../components/cart-modal';
|
2019-06-12 03:56:10 +00:00
|
|
|
import Industry from './steps/industry';
|
2019-05-17 03:04:52 +00:00
|
|
|
import Plugins from './steps/plugins';
|
2019-06-12 03:56:10 +00:00
|
|
|
import ProductTypes from './steps/product-types';
|
|
|
|
import ProfileWizardHeader from './header';
|
2020-01-10 01:36:30 +00:00
|
|
|
import { QUERY_DEFAULTS } from 'wc-api/constants';
|
2019-05-27 16:37:02 +00:00
|
|
|
import Start from './steps/start';
|
2019-05-30 07:15:39 +00:00
|
|
|
import StoreDetails from './steps/store-details';
|
2019-06-26 02:22:44 +00:00
|
|
|
import Theme from './steps/theme';
|
2019-05-31 04:51:33 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2019-12-20 12:58:38 +00:00
|
|
|
import { getProductIdsForCart } from 'dashboard/utils';
|
2019-05-07 19:25:51 +00:00
|
|
|
import './style.scss';
|
2019-05-17 03:04:52 +00:00
|
|
|
|
2019-05-28 03:09:48 +00:00
|
|
|
class ProfileWizard extends Component {
|
|
|
|
constructor() {
|
|
|
|
super( ...arguments );
|
2019-12-20 12:58:38 +00:00
|
|
|
this.state = {
|
|
|
|
showCartModal: false,
|
|
|
|
cartRedirectUrl: null,
|
|
|
|
};
|
2019-05-28 03:09:48 +00:00
|
|
|
this.goToNextStep = this.goToNextStep.bind( this );
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:24:24 +00:00
|
|
|
componentDidUpdate( prevProps ) {
|
|
|
|
const { step: prevStep } = prevProps.query;
|
|
|
|
const { step } = this.props.query;
|
2019-12-20 12:58:38 +00:00
|
|
|
const { isError, isGetProfileItemsRequesting, createNotice } = this.props;
|
|
|
|
|
|
|
|
const isRequestError = ! isGetProfileItemsRequesting && prevProps.isRequesting && isError;
|
|
|
|
if ( isRequestError ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__( 'There was a problem finishing the profile wizard.', 'woocommerce-admin' )
|
|
|
|
);
|
|
|
|
}
|
2019-10-29 18:24:24 +00:00
|
|
|
|
|
|
|
if ( prevStep !== step ) {
|
|
|
|
window.document.documentElement.scrollTop = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:25:51 +00:00
|
|
|
componentDidMount() {
|
2019-05-17 03:04:52 +00:00
|
|
|
document.documentElement.classList.remove( 'wp-toolbar' );
|
2019-08-21 05:58:47 +00:00
|
|
|
document.body.classList.add( 'woocommerce-onboarding' );
|
2019-05-07 19:25:51 +00:00
|
|
|
document.body.classList.add( 'woocommerce-profile-wizard__body' );
|
2019-07-22 04:53:13 +00:00
|
|
|
document.body.classList.add( 'woocommerce-admin-full-screen' );
|
2019-05-07 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2019-12-20 12:58:38 +00:00
|
|
|
const { cartRedirectUrl } = this.state;
|
|
|
|
|
|
|
|
if ( cartRedirectUrl ) {
|
|
|
|
document.body.classList.add( 'woocommerce-admin-is-loading' );
|
|
|
|
window.location = cartRedirectUrl;
|
|
|
|
}
|
|
|
|
|
2019-05-17 03:04:52 +00:00
|
|
|
document.documentElement.classList.add( 'wp-toolbar' );
|
2019-08-21 05:58:47 +00:00
|
|
|
document.body.classList.remove( 'woocommerce-onboarding' );
|
2019-05-07 19:25:51 +00:00
|
|
|
document.body.classList.remove( 'woocommerce-profile-wizard__body' );
|
2019-07-22 04:53:13 +00:00
|
|
|
document.body.classList.remove( 'woocommerce-admin-full-screen' );
|
2019-05-07 19:25:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:26:08 +00:00
|
|
|
getSteps() {
|
|
|
|
const { profileItems } = this.props;
|
|
|
|
const steps = [];
|
|
|
|
|
|
|
|
steps.push( {
|
|
|
|
key: 'start',
|
|
|
|
container: Start,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'plugins',
|
|
|
|
container: Plugins,
|
|
|
|
isComplete: profileItems.hasOwnProperty( 'plugins' ) && null !== profileItems.plugins,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'store-details',
|
|
|
|
container: StoreDetails,
|
|
|
|
label: __( 'Store Details', 'woocommerce-admin' ),
|
|
|
|
isComplete:
|
|
|
|
profileItems.hasOwnProperty( 'setup_client' ) && null !== profileItems.setup_client,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'industry',
|
|
|
|
container: Industry,
|
|
|
|
label: __( 'Industry', 'woocommerce-admin' ),
|
|
|
|
isComplete: profileItems.hasOwnProperty( 'industry' ) && null !== profileItems.industry,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'product-types',
|
|
|
|
container: ProductTypes,
|
|
|
|
label: __( 'Product Types', 'woocommerce-admin' ),
|
|
|
|
isComplete:
|
|
|
|
profileItems.hasOwnProperty( 'product_types' ) && null !== profileItems.product_types,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'business-details',
|
|
|
|
container: BusinessDetails,
|
|
|
|
label: __( 'Business Details', 'woocommerce-admin' ),
|
|
|
|
isComplete:
|
|
|
|
profileItems.hasOwnProperty( 'product_count' ) && null !== profileItems.product_count,
|
|
|
|
} );
|
|
|
|
steps.push( {
|
|
|
|
key: 'theme',
|
|
|
|
container: Theme,
|
|
|
|
label: __( 'Theme', 'woocommerce-admin' ),
|
|
|
|
isComplete: profileItems.hasOwnProperty( 'theme' ) && null !== profileItems.theme,
|
|
|
|
} );
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
2019-05-28 03:09:48 +00:00
|
|
|
getCurrentStep() {
|
2019-05-17 03:04:52 +00:00
|
|
|
const { step } = this.props.query;
|
2019-11-06 00:26:08 +00:00
|
|
|
const currentStep = this.getSteps().find( s => s.key === step );
|
2019-05-17 03:04:52 +00:00
|
|
|
|
|
|
|
if ( ! currentStep ) {
|
2019-11-06 00:26:08 +00:00
|
|
|
return this.getSteps()[ 0 ];
|
2019-05-17 03:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return currentStep;
|
|
|
|
}
|
|
|
|
|
2019-05-31 04:51:33 +00:00
|
|
|
async goToNextStep() {
|
2019-05-28 03:09:48 +00:00
|
|
|
const currentStep = this.getCurrentStep();
|
2019-11-06 00:26:08 +00:00
|
|
|
const currentStepIndex = this.getSteps().findIndex( s => s.key === currentStep.key );
|
|
|
|
const nextStep = this.getSteps()[ currentStepIndex + 1 ];
|
2019-05-28 03:09:48 +00:00
|
|
|
|
2019-05-31 04:51:33 +00:00
|
|
|
if ( 'undefined' === typeof nextStep ) {
|
2020-01-10 01:36:30 +00:00
|
|
|
this.possiblyShowCart();
|
2019-05-31 04:51:33 +00:00
|
|
|
return;
|
2019-05-28 03:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return updateQueryString( { step: nextStep.key } );
|
|
|
|
}
|
|
|
|
|
2020-01-10 01:36:30 +00:00
|
|
|
possiblyShowCart() {
|
|
|
|
const { profileItems } = this.props;
|
2019-12-20 12:58:38 +00:00
|
|
|
|
|
|
|
// @todo This should also send profile information to woocommerce.com.
|
|
|
|
|
|
|
|
const productIds = getProductIdsForCart( profileItems );
|
|
|
|
if ( productIds.length ) {
|
|
|
|
this.setState( { showCartModal: true } );
|
|
|
|
} else {
|
2020-01-10 01:36:30 +00:00
|
|
|
this.completeProfiler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
completeProfiler() {
|
|
|
|
const { notes, updateNote, updateProfileItems } = this.props;
|
|
|
|
updateProfileItems( { completed: true } );
|
|
|
|
|
|
|
|
const profilerNote = notes.find(
|
|
|
|
note => 'wc-admin-onboarding-profiler-reminder' === note.name
|
|
|
|
);
|
|
|
|
if ( profilerNote ) {
|
|
|
|
updateNote( profilerNote.id, { status: 'actioned' } );
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
markCompleteAndPurchase( cartRedirectUrl ) {
|
|
|
|
this.setState( { cartRedirectUrl } );
|
2020-01-10 01:36:30 +00:00
|
|
|
this.completeProfiler();
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 19:25:51 +00:00
|
|
|
render() {
|
2019-05-17 03:04:52 +00:00
|
|
|
const { query } = this.props;
|
2019-12-20 12:58:38 +00:00
|
|
|
const { showCartModal } = this.state;
|
2019-05-28 03:09:48 +00:00
|
|
|
const step = this.getCurrentStep();
|
2019-05-17 03:04:52 +00:00
|
|
|
|
2019-05-28 03:09:48 +00:00
|
|
|
const container = createElement( step.container, {
|
|
|
|
query,
|
|
|
|
step,
|
|
|
|
goToNextStep: this.goToNextStep,
|
|
|
|
} );
|
2019-11-06 00:26:08 +00:00
|
|
|
const steps = this.getSteps().map( _step => pick( _step, [ 'key', 'label', 'isComplete' ] ) );
|
2019-05-27 16:37:02 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-12-20 12:58:38 +00:00
|
|
|
{ showCartModal && (
|
|
|
|
<CartModal
|
|
|
|
onClose={ () => this.setState( { showCartModal: false } ) }
|
|
|
|
onClickPurchaseNow={ cartRedirectUrl =>
|
|
|
|
this.markCompleteAndPurchase( cartRedirectUrl )
|
|
|
|
}
|
2020-01-10 01:36:30 +00:00
|
|
|
onClickPurchaseLater={ () => this.completeProfiler() }
|
2019-12-20 12:58:38 +00:00
|
|
|
/>
|
|
|
|
) }
|
2019-05-27 16:37:02 +00:00
|
|
|
<ProfileWizardHeader currentStep={ step.key } steps={ steps } />
|
|
|
|
<div className="woocommerce-profile-wizard__container">{ container }</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
2019-05-07 19:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 03:09:48 +00:00
|
|
|
|
|
|
|
export default compose(
|
2019-05-31 04:51:33 +00:00
|
|
|
withSelect( select => {
|
2020-01-10 01:36:30 +00:00
|
|
|
const { getNotes, getProfileItems, getProfileItemsError } = select( 'wc-api' );
|
|
|
|
|
|
|
|
const notesQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
type: 'update',
|
|
|
|
status: 'unactioned',
|
|
|
|
};
|
|
|
|
const notes = getNotes( notesQuery );
|
2019-05-31 04:51:33 +00:00
|
|
|
|
2019-11-06 00:26:08 +00:00
|
|
|
return {
|
|
|
|
isError: Boolean( getProfileItemsError() ),
|
2020-01-10 01:36:30 +00:00
|
|
|
notes,
|
2019-11-06 00:26:08 +00:00
|
|
|
profileItems: getProfileItems(),
|
|
|
|
};
|
2019-05-31 04:51:33 +00:00
|
|
|
} ),
|
2019-05-28 03:09:48 +00:00
|
|
|
withDispatch( dispatch => {
|
2020-01-10 01:36:30 +00:00
|
|
|
const { updateNote, updateProfileItems } = dispatch( 'wc-api' );
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
2019-05-28 03:09:48 +00:00
|
|
|
|
|
|
|
return {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice,
|
2020-01-10 01:36:30 +00:00
|
|
|
updateNote,
|
2019-05-31 04:51:33 +00:00
|
|
|
updateProfileItems,
|
2019-05-28 03:09:48 +00:00
|
|
|
};
|
|
|
|
} )
|
|
|
|
)( ProfileWizard );
|