import { useState, useEffect, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { useHistory } from 'react-router-dom'; import { useStateValue } from '../utils/StateProvider'; import { CheckCircleIcon } from '@heroicons/react/24/outline'; function PluginsInstallStep() { const [ processing, setProcessing ] = useState( { isProcessing: false, buttonText: __( 'Install & Activate', 'cartflows' ), } ); const { buttonText } = processing; const [ { action_button, selected_page_builder }, dispatch ] = useStateValue(); const history = useHistory(); const required_plugins = cartflows_wizard.plugins; let installed_plugins_count = 0; /** * Dispatcher function to change the button text on wizard footer. */ const dispatchChangeButtonText = useCallback( ( data ) => { dispatch( { status: 'SET_NEXT_STEP', action_button: data, } ); }, [] ); /** * Function used to change the footer button text and the primary button text while processing the request. */ const handleOnClickProcessing = function () { const processing_buttonText = __( 'Installing Required Plugins', 'cartflows' ); setProcessing( { isProcessing: true, buttonText: processing_buttonText, } ); dispatchChangeButtonText( { button_text: processing_buttonText, button_class: 'is-loading', } ); dispatch( { status: 'PROCESSING', } ); }; useEffect( () => { dispatchChangeButtonText( { button_text: __( 'Install & Activate', 'cartflows' ), button_class: '', } ); const installPluginsSuccess = function () { setProcessing( false ); if ( 'bricks-builder' === selected_page_builder ) { history.push( { pathname: 'index.php', search: `?page=cartflow-setup&step=optin`, } ); } else { history.push( { pathname: 'index.php', search: `?page=cartflow-setup&step=store-checkout`, } ); } dispatch( { status: 'RESET', } ); }; document.addEventListener( 'wcf-plugins-install-success', installPluginsSuccess, false ); const installPluginsProcess = function () { handleOnClickProcessing(); }; document.addEventListener( 'wcf-install-require-plugins-processing', installPluginsProcess, false ); return () => { document.removeEventListener( 'wcf-plugins-install-success', installPluginsSuccess ); document.removeEventListener( 'wcf-install-require-plugins-processing', installPluginsProcess ); }; }, [ dispatchChangeButtonText ] ); const handleRedirection = function ( e ) { e.preventDefault(); setProcessing( { isProcessing: true, buttonText: __( 'Continuing…', 'cartflows' ), } ); history.push( { pathname: 'index.php', search: `?page=cartflow-setup&step=store-checkout`, } ); setProcessing( false ); }; return (
{ __( 'Step 3 of 6', 'cartflows' ) }

{ __( 'Great job!', 'cartflows' ) } { __( "Now let's install some required plugins.", 'cartflows' ) }

{ __( "Since CartFlows uses WooCommerce, we'll set it up for you along with Cart Abandonment and Stripe Payments so you can recover abandoned orders and easily accept payments.", 'cartflows' ) }

{ __( 'The following plugins will be installed and activated for you:', 'cartflows' ) }

{ required_plugins.map( ( plugin, index ) => { const plugin_name = plugin.name, plugin_stat = plugin.status; // Skip the display of Spectra plugin if default page builder is set other than gutenberg, // and is not empty. if ( 'gutenberg' !== selected_page_builder && '' !== selected_page_builder && 'ultimate-addons-for-gutenberg' === plugin.slug ) { // Increasing the plugin count for the non-gutenberg page builders. installed_plugins_count++; return ''; } if ( 'active' === plugin_stat ) { installed_plugins_count++; } return (
); } ) }
{ Object.keys( required_plugins ).length === installed_plugins_count ? ( ) : ( ) }
); } export default PluginsInstallStep;