2022-06-28 05:53:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { difference } from 'lodash';
|
2022-06-30 06:45:52 +00:00
|
|
|
import { useCallback, useEffect, useState } from '@wordpress/element';
|
2022-06-28 05:53:03 +00:00
|
|
|
import { Stepper } from '@woocommerce/components';
|
|
|
|
import { Card, CardBody, Button } from '@wordpress/components';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { Connect } from './components/connect';
|
|
|
|
import { Plugins } from './components/plugins';
|
|
|
|
import { StoreLocation } from './components/store-location';
|
|
|
|
import { WCSBanner } from './components/wcs-banner';
|
|
|
|
import { TaskProps, ShippingRecommendationProps } from './types';
|
2022-06-29 03:24:25 +00:00
|
|
|
import { redirectToWCSSettings } from './utils';
|
2022-06-28 05:53:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugins required to automate shipping.
|
|
|
|
*/
|
|
|
|
const AUTOMATION_PLUGINS = [ 'jetpack', 'woocommerce-services' ];
|
|
|
|
|
|
|
|
export const ShippingRecommendation: React.FC<
|
|
|
|
TaskProps & ShippingRecommendationProps
|
|
|
|
> = ( { activePlugins, isJetpackConnected, isResolving } ) => {
|
|
|
|
const [ pluginsToActivate, setPluginsToActivate ] = useState< string[] >(
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
const [ stepIndex, setStepIndex ] = useState( 0 );
|
|
|
|
const [ isRedirecting, setIsRedirecting ] = useState( false );
|
2022-06-30 06:45:52 +00:00
|
|
|
const [ locationStepRedirected, setLocationStepRedirected ] =
|
|
|
|
useState( false );
|
2022-06-28 05:53:03 +00:00
|
|
|
|
|
|
|
const nextStep = () => {
|
|
|
|
setStepIndex( stepIndex + 1 );
|
|
|
|
};
|
|
|
|
|
2022-06-29 03:24:25 +00:00
|
|
|
const redirect = () => {
|
|
|
|
setIsRedirecting( true );
|
|
|
|
redirectToWCSSettings();
|
2022-06-28 05:53:03 +00:00
|
|
|
};
|
|
|
|
|
2022-06-30 06:45:52 +00:00
|
|
|
const viewLocationStep = () => {
|
|
|
|
setStepIndex( 0 );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Skips to next step only once.
|
|
|
|
const onLocationComplete = () => {
|
|
|
|
if ( locationStepRedirected ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLocationStepRedirected( true );
|
|
|
|
nextStep();
|
|
|
|
};
|
|
|
|
|
2022-06-28 05:53:03 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const remainingPlugins = difference(
|
|
|
|
AUTOMATION_PLUGINS,
|
|
|
|
activePlugins
|
|
|
|
);
|
|
|
|
|
|
|
|
// Force redirect when all steps are completed.
|
|
|
|
if (
|
|
|
|
! isResolving &&
|
|
|
|
remainingPlugins.length === 0 &&
|
|
|
|
isJetpackConnected
|
|
|
|
) {
|
2022-06-29 03:24:25 +00:00
|
|
|
redirect();
|
2022-06-28 05:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( remainingPlugins.length <= pluginsToActivate.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setPluginsToActivate( remainingPlugins );
|
|
|
|
}, [ activePlugins, isJetpackConnected, isResolving, pluginsToActivate ] );
|
|
|
|
|
|
|
|
const steps = [
|
|
|
|
{
|
|
|
|
key: 'store_location',
|
|
|
|
label: __( 'Set store location', 'woocommerce' ),
|
|
|
|
description: __(
|
|
|
|
'The address from which your business operates',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
2022-06-30 06:45:52 +00:00
|
|
|
content: (
|
|
|
|
<StoreLocation
|
|
|
|
nextStep={ nextStep }
|
|
|
|
onLocationComplete={ onLocationComplete }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
onClick: viewLocationStep,
|
2022-06-28 05:53:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'plugins',
|
|
|
|
label: pluginsToActivate.includes( 'woocommerce-services' )
|
|
|
|
? __(
|
|
|
|
'Install Jetpack and WooCommerce Shipping',
|
|
|
|
'woocommerce'
|
|
|
|
)
|
|
|
|
: __( 'Install Jetpack', 'woocommerce' ),
|
|
|
|
description: __(
|
|
|
|
'Enable shipping label printing and discounted rates',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
content: (
|
|
|
|
<div>
|
|
|
|
<WCSBanner />
|
|
|
|
<Plugins
|
|
|
|
nextStep={ nextStep }
|
|
|
|
pluginsToActivate={ pluginsToActivate }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'connect',
|
|
|
|
label: __( 'Connect your store', 'woocommerce' ),
|
|
|
|
description: __(
|
|
|
|
'Connect your store to WordPress.com to enable WooCommerce Shipping',
|
|
|
|
'woocommerce'
|
|
|
|
),
|
|
|
|
content: isJetpackConnected ? (
|
2022-06-29 03:24:25 +00:00
|
|
|
<Button onClick={ redirect } isBusy={ isRedirecting } isPrimary>
|
2022-06-28 05:53:03 +00:00
|
|
|
{ __( 'Complete task', 'woocommerce' ) }
|
|
|
|
</Button>
|
|
|
|
) : (
|
2022-06-29 03:24:25 +00:00
|
|
|
<Connect onConnect={ redirect } />
|
2022-06-28 05:53:03 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const step = steps[ stepIndex ];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-task-shipping-recommendation">
|
|
|
|
<Card className="woocommerce-task-card">
|
|
|
|
<CardBody>
|
|
|
|
<Stepper
|
|
|
|
isPending={ isResolving }
|
|
|
|
isVertical={ true }
|
|
|
|
currentStep={ step.key }
|
|
|
|
steps={ steps }
|
|
|
|
/>
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|