2021-11-02 17:33:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-02-21 02:34:25 +00:00
|
|
|
import interpolateComponents from '@automattic/interpolate-components';
|
2021-11-02 17:33:42 +00:00
|
|
|
import { Link, Plugins as PluginInstaller } from '@woocommerce/components';
|
2022-04-21 05:10:56 +00:00
|
|
|
import { OPTIONS_STORE_NAME, InstallPluginsResponse } from '@woocommerce/data';
|
2021-11-02 17:33:42 +00:00
|
|
|
import { recordEvent, queueRecordEvent } from '@woocommerce/tracks';
|
|
|
|
import { Text } from '@woocommerce/experimental';
|
2022-04-25 05:49:11 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2021-11-05 20:32:02 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2021-11-02 17:33:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-11-05 20:32:02 +00:00
|
|
|
import { createNoticesFromResponse } from '~/lib/notices';
|
|
|
|
import { SetupStepProps } from './setup';
|
2022-05-24 03:20:04 +00:00
|
|
|
|
|
|
|
const isWcConnectOptions = (
|
|
|
|
wcConnectOptions: unknown
|
|
|
|
): wcConnectOptions is {
|
|
|
|
[ key: string ]: unknown;
|
|
|
|
} => typeof wcConnectOptions === 'object' && wcConnectOptions !== null;
|
2021-11-02 17:33:42 +00:00
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
export const Plugins: React.FC< SetupStepProps > = ( {
|
2021-11-02 17:33:42 +00:00
|
|
|
nextStep,
|
|
|
|
onDisable,
|
|
|
|
onManual,
|
|
|
|
pluginsToActivate,
|
|
|
|
} ) => {
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
2022-04-25 05:49:11 +00:00
|
|
|
const { isResolving, tosAccepted } = useSelect( ( select ) => {
|
2022-06-21 08:37:34 +00:00
|
|
|
const { getOption, hasFinishedResolution } =
|
|
|
|
select( OPTIONS_STORE_NAME );
|
2022-05-24 03:20:04 +00:00
|
|
|
const wcConnectOptions = getOption( 'wc_connect_options' );
|
2021-11-02 17:33:42 +00:00
|
|
|
|
2022-04-25 05:49:11 +00:00
|
|
|
return {
|
|
|
|
isResolving:
|
|
|
|
! hasFinishedResolution( 'getOption', [
|
|
|
|
'woocommerce_setup_jetpack_opted_in',
|
|
|
|
] ) ||
|
|
|
|
! hasFinishedResolution( 'getOption', [
|
|
|
|
'wc_connect_options',
|
|
|
|
] ),
|
|
|
|
tosAccepted:
|
2022-05-24 03:20:04 +00:00
|
|
|
( isWcConnectOptions( wcConnectOptions ) &&
|
|
|
|
wcConnectOptions?.tos_accepted ) ||
|
2022-04-25 05:49:11 +00:00
|
|
|
getOption( 'woocommerce_setup_jetpack_opted_in' ) === '1',
|
|
|
|
};
|
|
|
|
} );
|
2021-11-02 17:33:42 +00:00
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
useEffect( () => {
|
|
|
|
if ( ! tosAccepted || pluginsToActivate.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextStep();
|
|
|
|
}, [ isResolving ] );
|
|
|
|
|
2021-11-02 17:33:42 +00:00
|
|
|
const agreementText = pluginsToActivate.includes( 'woocommerce-services' )
|
|
|
|
? __(
|
|
|
|
'By installing Jetpack and WooCommerce Tax you agree to the {{link}}Terms of Service{{/link}}.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-11-02 17:33:42 +00:00
|
|
|
)
|
|
|
|
: __(
|
|
|
|
'By installing Jetpack you agree to the {{link}}Terms of Service{{/link}}.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-11-02 17:33:42 +00:00
|
|
|
);
|
|
|
|
|
2021-11-05 20:32:02 +00:00
|
|
|
if ( isResolving ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:33:42 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PluginInstaller
|
2022-04-25 06:16:39 +00:00
|
|
|
// @ts-expect-error PluginInstaller has onComplete props but it is a pure js component and doesn't export the right types.
|
2022-04-21 05:10:56 +00:00
|
|
|
onComplete={ (
|
|
|
|
activatedPlugins: string[],
|
|
|
|
response: InstallPluginsResponse
|
|
|
|
) => {
|
2021-11-02 17:33:42 +00:00
|
|
|
createNoticesFromResponse( response );
|
|
|
|
recordEvent( 'tasklist_tax_install_extensions', {
|
|
|
|
install_extensions: true,
|
|
|
|
} );
|
|
|
|
updateOptions( {
|
|
|
|
woocommerce_setup_jetpack_opted_in: true,
|
|
|
|
} );
|
|
|
|
nextStep();
|
|
|
|
} }
|
2022-04-21 05:10:56 +00:00
|
|
|
onError={ ( errors: unknown, response: unknown ) =>
|
2021-11-02 17:33:42 +00:00
|
|
|
createNoticesFromResponse( response )
|
|
|
|
}
|
|
|
|
onSkip={ () => {
|
|
|
|
queueRecordEvent( 'tasklist_tax_install_extensions', {
|
|
|
|
install_extensions: false,
|
|
|
|
} );
|
|
|
|
onManual();
|
|
|
|
} }
|
2022-03-30 09:00:04 +00:00
|
|
|
skipText={ __( 'Set up manually', 'woocommerce' ) }
|
2021-11-02 17:33:42 +00:00
|
|
|
onAbort={ () => onDisable() }
|
2022-03-30 09:00:04 +00:00
|
|
|
abortText={ __( "I don't charge sales tax", 'woocommerce' ) }
|
2021-11-02 17:33:42 +00:00
|
|
|
/>
|
|
|
|
{ ! tosAccepted && (
|
|
|
|
<Text
|
|
|
|
variant="caption"
|
|
|
|
className="woocommerce-task__caption"
|
|
|
|
size="12"
|
|
|
|
lineHeight="16px"
|
|
|
|
>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: agreementText,
|
|
|
|
components: {
|
|
|
|
link: (
|
|
|
|
<Link
|
|
|
|
href={ 'https://wordpress.com/tos/' }
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
2022-04-21 04:44:19 +00:00
|
|
|
>
|
|
|
|
<></>
|
|
|
|
</Link>
|
2021-11-02 17:33:42 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</Text>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|