woocommerce/plugins/woocommerce-admin/client/task-lists/fills/experimental-shipping-recom.../shipping-recommendation.tsx

146 lines
3.4 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { difference } from 'lodash';
import { useEffect, useState } from '@wordpress/element';
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';
import { redirectToWCSSettings } from './utils';
/**
* Plugins required to automate shipping.
*/
Remove dependency on Jetpack from Shipping and Tax task list items (#39992) * Remove dependency on Jetpack from WCS&T's task list item - Remove installing Jetpack-the-plugin from WooCommerce Shipping & Tax WC Home task list and recommended extensions area. - Update Jetpack auth link generation to use getJetpackAuthUrl(). - Remove references of Jetpack-the-plugin from WCS&T onboarding as the extension will now use the Jetpack Connection package for establishing a connection between WPCOM infrastructure and a Woo site. * Update WooCommerce Tax flow in WC Home task list * Inline the agreementText variable * Add missing pluginSlugs prop to <Plugins> instance for WC Tax * Fix WC Tax extension name in API response * Remove Jetpack from copy in Tax task header * Fix MD034/no-bare-urls MD linting violation * Fix experimental shipping recommendation tests * Add changelogs requested by linter * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Remove mention of Jetpack from ExperimentalShippingRecommendation * Fix text wrapping bug in WC Tax onboarding task * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Use isResolving() to detect pending auth URL fetch * Replace unused hasErrors with ref to error * Fix lint * Jetpack auth URL prefetching * Revert "Jetpack auth URL prefetching" This reverts commit 2b79000e203f99ae5ceb8c4a3e6a1d1d6fbe4dc8. * Add PrefetchJetpackAuthUrl component to remove Connect button load time * Replace URL prefetching in component with useEffect * Fix: Accept redirect URL as param instead of prop * Fix lint * Remove prefetching. Make Connect button always available * Fix ExperimentalShippingRecommendation incompatibility with new Connect behavior * Handle empty object errors in Connect * Display TOS above WooCommerce Shipping & Tax installation buttons (#40863) --------- Co-authored-by: github-actions <github-actions@github.com>
2023-10-25 14:39:43 +00:00
const AUTOMATION_PLUGINS = [ '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 );
const [ locationStepRedirected, setLocationStepRedirected ] =
useState( false );
const nextStep = () => {
setStepIndex( stepIndex + 1 );
};
const redirect = () => {
setIsRedirecting( true );
redirectToWCSSettings();
};
const viewLocationStep = () => {
setStepIndex( 0 );
};
// Skips to next step only once.
const onLocationComplete = () => {
if ( locationStepRedirected ) {
return;
}
setLocationStepRedirected( true );
nextStep();
};
useEffect( () => {
const remainingPlugins = difference(
AUTOMATION_PLUGINS,
activePlugins
);
// Force redirect when all steps are completed.
if (
! isResolving &&
remainingPlugins.length === 0 &&
isJetpackConnected
) {
redirect();
}
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'
),
content: (
<StoreLocation
nextStep={ nextStep }
onLocationComplete={ onLocationComplete }
/>
),
onClick: viewLocationStep,
},
{
key: 'plugins',
Remove dependency on Jetpack from Shipping and Tax task list items (#39992) * Remove dependency on Jetpack from WCS&T's task list item - Remove installing Jetpack-the-plugin from WooCommerce Shipping & Tax WC Home task list and recommended extensions area. - Update Jetpack auth link generation to use getJetpackAuthUrl(). - Remove references of Jetpack-the-plugin from WCS&T onboarding as the extension will now use the Jetpack Connection package for establishing a connection between WPCOM infrastructure and a Woo site. * Update WooCommerce Tax flow in WC Home task list * Inline the agreementText variable * Add missing pluginSlugs prop to <Plugins> instance for WC Tax * Fix WC Tax extension name in API response * Remove Jetpack from copy in Tax task header * Fix MD034/no-bare-urls MD linting violation * Fix experimental shipping recommendation tests * Add changelogs requested by linter * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Remove mention of Jetpack from ExperimentalShippingRecommendation * Fix text wrapping bug in WC Tax onboarding task * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Use isResolving() to detect pending auth URL fetch * Replace unused hasErrors with ref to error * Fix lint * Jetpack auth URL prefetching * Revert "Jetpack auth URL prefetching" This reverts commit 2b79000e203f99ae5ceb8c4a3e6a1d1d6fbe4dc8. * Add PrefetchJetpackAuthUrl component to remove Connect button load time * Replace URL prefetching in component with useEffect * Fix: Accept redirect URL as param instead of prop * Fix lint * Remove prefetching. Make Connect button always available * Fix ExperimentalShippingRecommendation incompatibility with new Connect behavior * Handle empty object errors in Connect * Display TOS above WooCommerce Shipping & Tax installation buttons (#40863) --------- Co-authored-by: github-actions <github-actions@github.com>
2023-10-25 14:39:43 +00:00
label: __( 'Install WooCommerce Shipping', '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 ? (
<Button onClick={ redirect } isBusy={ isRedirecting } isPrimary>
{ __( 'Complete task', 'woocommerce' ) }
</Button>
) : (
Remove dependency on Jetpack from Shipping and Tax task list items (#39992) * Remove dependency on Jetpack from WCS&T's task list item - Remove installing Jetpack-the-plugin from WooCommerce Shipping & Tax WC Home task list and recommended extensions area. - Update Jetpack auth link generation to use getJetpackAuthUrl(). - Remove references of Jetpack-the-plugin from WCS&T onboarding as the extension will now use the Jetpack Connection package for establishing a connection between WPCOM infrastructure and a Woo site. * Update WooCommerce Tax flow in WC Home task list * Inline the agreementText variable * Add missing pluginSlugs prop to <Plugins> instance for WC Tax * Fix WC Tax extension name in API response * Remove Jetpack from copy in Tax task header * Fix MD034/no-bare-urls MD linting violation * Fix experimental shipping recommendation tests * Add changelogs requested by linter * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Remove mention of Jetpack from ExperimentalShippingRecommendation * Fix text wrapping bug in WC Tax onboarding task * Add changefile(s) from automation for the following project(s): @woocommerce/components, woocommerce * Use isResolving() to detect pending auth URL fetch * Replace unused hasErrors with ref to error * Fix lint * Jetpack auth URL prefetching * Revert "Jetpack auth URL prefetching" This reverts commit 2b79000e203f99ae5ceb8c4a3e6a1d1d6fbe4dc8. * Add PrefetchJetpackAuthUrl component to remove Connect button load time * Replace URL prefetching in component with useEffect * Fix: Accept redirect URL as param instead of prop * Fix lint * Remove prefetching. Make Connect button always available * Fix ExperimentalShippingRecommendation incompatibility with new Connect behavior * Handle empty object errors in Connect * Display TOS above WooCommerce Shipping & Tax installation buttons (#40863) --------- Co-authored-by: github-actions <github-actions@github.com>
2023-10-25 14:39:43 +00:00
<Connect />
),
},
];
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>
);
};