/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, Children } from '@wordpress/element';
import { Text } from '@woocommerce/experimental';
import { PLUGINS_STORE_NAME } from '@woocommerce/data';
import ExternalIcon from 'gridicons/dist/external';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore VisuallyHidden is present, it's just not typed
// eslint-disable-next-line @woocommerce/dependency-group
import { CardFooter, Button, VisuallyHidden } from '@wordpress/components';
/**
* Internal dependencies
*/
import { createNoticesFromResponse } from '../lib/notices';
import {
DismissableList,
DismissableListHeading,
} from '../settings-recommendations/dismissable-list';
import WooCommerceServicesItem from './woocommerce-services-item';
import './shipping-recommendations.scss';
const useInstallPlugin = () => {
const [ pluginsBeingSetup, setPluginsBeingSetup ] = useState<
Array< string >
>( [] );
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
const handleSetup = ( slugs: string[] ): PromiseLike< void > => {
if ( pluginsBeingSetup.length > 0 ) {
return Promise.resolve();
}
setPluginsBeingSetup( slugs );
return installAndActivatePlugins( slugs )
.then( () => {
setPluginsBeingSetup( [] );
} )
.catch( ( response: { errors: Record< string, string > } ) => {
createNoticesFromResponse( response );
setPluginsBeingSetup( [] );
return Promise.reject();
} );
};
return [ pluginsBeingSetup, handleSetup ] as const;
};
export const ShippingRecommendationsList: React.FC = ( { children } ) => (
{ __( 'Recommended shipping solutions', 'woocommerce' ) }
{ __(
'We recommend adding one of the following shipping extensions to your store. The extension will be installed and activated for you when you click "Get started".',
'woocommerce'
) }
{ Children.map( children, ( item ) => (
- { item }
) ) }
);
const ShippingRecommendations: React.FC = () => {
const [ pluginsBeingSetup, setupPlugin ] = useInstallPlugin();
const activePlugins = useSelect( ( select ) =>
select( PLUGINS_STORE_NAME ).getActivePlugins()
);
if ( activePlugins.includes( 'woocommerce-services' ) ) {
return null;
}
return (
);
};
export default ShippingRecommendations;