2019-05-22 16:19:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-09 23:16:21 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2021-01-07 23:57:09 +00:00
|
|
|
import { Button, Card, CardBody, CardFooter } from '@wordpress/components';
|
2021-03-16 17:27:15 +00:00
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2020-07-22 00:11:11 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2021-03-16 17:27:15 +00:00
|
|
|
import { Link } from '@woocommerce/components';
|
2020-05-29 10:01:14 +00:00
|
|
|
import {
|
|
|
|
pluginNames,
|
|
|
|
ONBOARDING_STORE_NAME,
|
|
|
|
PLUGINS_STORE_NAME,
|
2020-06-11 19:22:20 +00:00
|
|
|
OPTIONS_STORE_NAME,
|
2020-05-29 10:01:14 +00:00
|
|
|
} from '@woocommerce/data';
|
2020-08-20 04:59:52 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2021-01-07 23:57:09 +00:00
|
|
|
import { Text } from '@woocommerce/experimental';
|
2019-08-23 12:56:57 +00:00
|
|
|
|
|
|
|
/**
|
2019-11-14 14:35:55 +00:00
|
|
|
* Internal dependencies
|
2019-08-23 12:56:57 +00:00
|
|
|
*/
|
2021-03-16 17:27:15 +00:00
|
|
|
import { Benefits } from './benefits';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { createNoticesFromResponse } from '../../../lib/notices';
|
2020-03-09 23:16:21 +00:00
|
|
|
import Logo from './logo';
|
2019-05-22 16:19:56 +00:00
|
|
|
|
2021-03-16 17:27:15 +00:00
|
|
|
export const BenefitsLayout = ( { goToNextStep } ) => {
|
|
|
|
const {
|
|
|
|
activePlugins,
|
|
|
|
isJetpackConnected,
|
|
|
|
isProfileItemsError,
|
|
|
|
isUpdatingProfileItems,
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
const { getOnboardingError, isOnboardingRequesting } = select(
|
|
|
|
ONBOARDING_STORE_NAME
|
2020-04-01 15:24:51 +00:00
|
|
|
);
|
2021-03-16 17:27:15 +00:00
|
|
|
const { getActivePlugins } = select( PLUGINS_STORE_NAME );
|
|
|
|
|
|
|
|
return {
|
|
|
|
activePlugins: getActivePlugins(),
|
|
|
|
isJetpackConnected: select(
|
|
|
|
PLUGINS_STORE_NAME
|
|
|
|
).isJetpackConnected(),
|
|
|
|
isProfileItemsError: Boolean(
|
|
|
|
getOnboardingError( 'updateProfileItems' )
|
|
|
|
),
|
|
|
|
isUpdatingProfileItems: isOnboardingRequesting(
|
|
|
|
'updateProfileItems'
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const { updateProfileItems } = useDispatch( ONBOARDING_STORE_NAME );
|
|
|
|
const { installAndActivatePlugins } = useDispatch( PLUGINS_STORE_NAME );
|
|
|
|
|
|
|
|
const pluginsRemaining = [ 'jetpack', 'woocommerce-services' ].filter(
|
|
|
|
( plugin ) => ! activePlugins.includes( plugin )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cache the initial plugin list so we don't change benefits midway through activation.
|
|
|
|
const [ pluginsToInstall ] = useState( pluginsRemaining );
|
|
|
|
const [ isInstalling, setIsInstalling ] = useState( false );
|
|
|
|
|
|
|
|
const isJetpackActive = ! pluginsToInstall.includes( 'jetpack' );
|
|
|
|
const isWcsActive = ! pluginsToInstall.includes( 'woocommerce-services' );
|
|
|
|
const isComplete = isWcsActive && isJetpackActive && isJetpackConnected;
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
// Skip this step if already complete.
|
|
|
|
if ( isComplete ) {
|
|
|
|
goToNextStep();
|
|
|
|
return;
|
2020-04-01 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:19:32 +00:00
|
|
|
recordEvent( 'storeprofiler_plugins_to_install', {
|
2021-03-16 17:27:15 +00:00
|
|
|
plugins: pluginsToInstall,
|
2020-04-21 09:19:32 +00:00
|
|
|
} );
|
2021-03-16 17:27:15 +00:00
|
|
|
}, [] );
|
2020-04-21 09:19:32 +00:00
|
|
|
|
2021-03-16 17:27:15 +00:00
|
|
|
if ( isComplete ) {
|
|
|
|
return null;
|
2019-05-22 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 17:27:15 +00:00
|
|
|
const skipPluginInstall = async () => {
|
|
|
|
const plugins = isJetpackActive ? 'skipped-wcs' : 'skipped';
|
|
|
|
await updateProfileItems( { plugins } );
|
2019-05-31 04:51:33 +00:00
|
|
|
|
2019-10-10 14:05:13 +00:00
|
|
|
if ( isProfileItemsError ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice(
|
|
|
|
'error',
|
2020-02-14 02:23:21 +00:00
|
|
|
__(
|
2021-02-10 23:57:51 +00:00
|
|
|
'There was a problem updating your preferences',
|
2020-02-14 02:23:21 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2019-07-23 03:26:46 +00:00
|
|
|
);
|
2019-10-10 14:05:13 +00:00
|
|
|
} else {
|
2020-03-03 09:44:26 +00:00
|
|
|
recordEvent( 'storeprofiler_install_plugins', {
|
|
|
|
install: false,
|
2020-02-14 02:23:21 +00:00
|
|
|
plugins,
|
|
|
|
} );
|
2019-05-31 04:51:33 +00:00
|
|
|
}
|
2020-05-29 15:18:15 +00:00
|
|
|
|
|
|
|
goToNextStep();
|
2021-03-16 17:27:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const startPluginInstall = () => {
|
|
|
|
const plugins = isJetpackActive ? 'installed-wcs' : 'installed';
|
2019-05-22 16:19:56 +00:00
|
|
|
|
2021-03-16 17:27:15 +00:00
|
|
|
setIsInstalling( true );
|
2020-06-12 06:38:33 +00:00
|
|
|
|
2020-03-03 09:44:26 +00:00
|
|
|
recordEvent( 'storeprofiler_install_plugins', {
|
|
|
|
install: true,
|
|
|
|
plugins,
|
|
|
|
} );
|
2020-06-12 06:38:33 +00:00
|
|
|
|
2020-06-25 12:01:33 +00:00
|
|
|
Promise.all( [
|
2021-03-16 17:27:15 +00:00
|
|
|
pluginsToInstall.length
|
|
|
|
? installAndActivatePlugins( pluginsToInstall )
|
|
|
|
: null,
|
|
|
|
updateProfileItems( { plugins } ),
|
2020-06-12 06:38:33 +00:00
|
|
|
updateOptions( {
|
|
|
|
woocommerce_setup_jetpack_opted_in: true,
|
|
|
|
} ),
|
2020-06-25 12:01:33 +00:00
|
|
|
] )
|
2021-03-16 17:27:15 +00:00
|
|
|
.then( () => {
|
|
|
|
setIsInstalling( false );
|
|
|
|
goToNextStep();
|
|
|
|
} )
|
2020-06-25 12:01:33 +00:00
|
|
|
.catch( ( pluginError, profileError ) => {
|
|
|
|
if ( pluginError ) {
|
|
|
|
createNoticesFromResponse( pluginError );
|
|
|
|
}
|
|
|
|
if ( profileError ) {
|
|
|
|
createNotice(
|
|
|
|
'error',
|
|
|
|
__(
|
2021-02-10 23:57:51 +00:00
|
|
|
'There was a problem updating your preferences',
|
2020-06-25 12:01:33 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-03-16 17:27:15 +00:00
|
|
|
setIsInstalling( false );
|
2020-06-25 12:01:33 +00:00
|
|
|
goToNextStep();
|
|
|
|
} );
|
2021-03-16 17:27:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const pluginNamesString = pluginsToInstall
|
|
|
|
.map( ( pluginSlug ) => pluginNames[ pluginSlug ] )
|
|
|
|
.join( ' ' + __( 'and', 'woocommerce-admin' ) + ' ' );
|
|
|
|
const isAcceptingTos = ! isWcsActive;
|
|
|
|
const pluralizedPlugins = _n(
|
|
|
|
'plugin',
|
|
|
|
'plugins',
|
|
|
|
pluginsToInstall.length,
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card className="woocommerce-profile-wizard__benefits-card">
|
|
|
|
<CardBody justify="center">
|
|
|
|
<Logo />
|
|
|
|
<div className="woocommerce-profile-wizard__step-header">
|
|
|
|
<Text variant="title.small" as="h2">
|
|
|
|
{ sprintf(
|
|
|
|
/* translators: %s = names of plugins to install, e.g. Jetpack and Woocommerce Services */
|
|
|
|
__(
|
|
|
|
'Enhance your store with %s',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
pluginNamesString
|
|
|
|
) }
|
|
|
|
</Text>
|
2019-05-22 16:19:56 +00:00
|
|
|
</div>
|
2021-03-16 17:27:15 +00:00
|
|
|
<Benefits
|
|
|
|
isJetpackSetup={ isJetpackActive && isJetpackConnected }
|
|
|
|
isWcsSetup={ isWcsActive }
|
|
|
|
/>
|
|
|
|
</CardBody>
|
|
|
|
<CardFooter isBorderless justify="center">
|
|
|
|
<Button
|
|
|
|
isPrimary
|
|
|
|
isBusy={ isInstalling }
|
|
|
|
disabled={ isUpdatingProfileItems || isInstalling }
|
|
|
|
onClick={ startPluginInstall }
|
|
|
|
>
|
|
|
|
{ __( 'Yes please!', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
isSecondary
|
|
|
|
isBusy={ isUpdatingProfileItems && ! isInstalling }
|
|
|
|
disabled={ isUpdatingProfileItems || isInstalling }
|
|
|
|
className="woocommerce-profile-wizard__skip"
|
|
|
|
onClick={ skipPluginInstall }
|
|
|
|
>
|
|
|
|
{ __( 'No thanks', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</CardFooter>
|
|
|
|
|
|
|
|
{ !! pluginsToInstall.length && (
|
2020-12-21 19:34:22 +00:00
|
|
|
<CardFooter isBorderless justify="center">
|
|
|
|
<p className="woocommerce-profile-wizard__benefits-install-notice">
|
|
|
|
{ isAcceptingTos
|
|
|
|
? interpolateComponents( {
|
|
|
|
mixedString: sprintf(
|
2021-03-16 17:27:15 +00:00
|
|
|
/* translators: %1$s: names of plugins to install, e.g. Jetpack and Woocommerce Services, %2$s: singular or plural form of 'plugins' */
|
2020-12-21 19:34:22 +00:00
|
|
|
__(
|
2021-03-16 17:27:15 +00:00
|
|
|
'%1$s %2$s will be installed & activated for free, and you agree to our {{link}}Terms of Service{{/link}}.',
|
2020-12-21 19:34:22 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
pluginNamesString,
|
|
|
|
pluralizedPlugins
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
|
|
|
href="https://wordpress.com/tos/"
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
} )
|
|
|
|
: sprintf(
|
2021-03-16 17:27:15 +00:00
|
|
|
/* translators: %1$s: names of plugins to install, e.g. Jetpack and Woocommerce Services, %2$s: singular or plural form of 'plugins' */
|
2020-07-22 00:11:11 +00:00
|
|
|
__(
|
2021-03-16 17:27:15 +00:00
|
|
|
'%1$s %2$s will be installed & activated for free.',
|
2020-07-22 00:11:11 +00:00
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
pluginNamesString,
|
|
|
|
pluralizedPlugins
|
2020-12-21 19:34:22 +00:00
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</CardFooter>
|
2021-03-16 17:27:15 +00:00
|
|
|
) }
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|