2022-01-12 06:46:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-06-16 14:32:58 +00:00
|
|
|
import { Notice } from '@wordpress/components';
|
2022-01-12 06:46:33 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2022-04-25 05:49:11 +00:00
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2023-06-16 14:32:58 +00:00
|
|
|
import { OPTIONS_STORE_NAME } from '@woocommerce/data';
|
2022-01-12 06:46:33 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2022-07-06 06:20:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Banner from './banner';
|
2022-09-23 12:36:12 +00:00
|
|
|
import ApmList, { Apm } from './apms';
|
2022-01-12 06:46:33 +00:00
|
|
|
import './style.scss';
|
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2022-09-23 12:36:12 +00:00
|
|
|
import FrequentlyAskedQuestionsSimple from './faq-simple';
|
2022-01-12 06:46:33 +00:00
|
|
|
|
|
|
|
interface activatePromoResponse {
|
|
|
|
success: boolean;
|
|
|
|
}
|
|
|
|
|
2023-06-16 14:32:58 +00:00
|
|
|
const ConnectAccountPage = () => {
|
|
|
|
const incentive = getAdminSetting( 'wcpayWelcomePageIncentive' );
|
|
|
|
const { updateOptions } = useDispatch( OPTIONS_STORE_NAME );
|
|
|
|
const { installAndActivatePlugins } = useDispatch( 'wc/admin/plugins' );
|
2022-01-12 06:46:33 +00:00
|
|
|
const [ isSubmitted, setSubmitted ] = useState( false );
|
2023-06-16 14:32:58 +00:00
|
|
|
const [ errorMessage, setErrorMessage ] = useState( '' );
|
|
|
|
const [ enabledApms, setEnabledApms ] = useState( new Set< Apm >() );
|
2022-01-12 06:46:33 +00:00
|
|
|
|
2023-06-16 14:32:58 +00:00
|
|
|
const { isJetpackConnected, connectUrl } = useSelect( ( select ) => {
|
|
|
|
return {
|
|
|
|
isJetpackConnected:
|
|
|
|
select( 'wc/admin/plugins' ).isJetpackConnected(),
|
|
|
|
connectUrl:
|
2023-06-19 15:50:12 +00:00
|
|
|
'admin.php?wcpay-connect=1&promo=' +
|
|
|
|
encodeURIComponent( incentive.id ) +
|
|
|
|
'&_wpnonce=' +
|
|
|
|
getAdminSetting( 'wcpay_welcome_page_connect_nonce' ),
|
2023-06-16 14:32:58 +00:00
|
|
|
};
|
|
|
|
} );
|
2022-01-12 06:46:33 +00:00
|
|
|
|
2023-06-16 14:32:58 +00:00
|
|
|
/**
|
|
|
|
* Record page view and save viewed timestamp.
|
|
|
|
*/
|
|
|
|
useEffect( () => {
|
|
|
|
recordEvent( 'page_view', {
|
|
|
|
path: 'payments_connect_core_test',
|
|
|
|
incentive_id: incentive.id,
|
|
|
|
} );
|
|
|
|
updateOptions( {
|
|
|
|
wcpay_welcome_page_viewed_timestamp: Math.floor(
|
|
|
|
Date.now() / 1000
|
|
|
|
),
|
|
|
|
} );
|
|
|
|
// We only want to record this once, so we don't want to re-run this effect.
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [] );
|
2022-01-12 06:46:33 +00:00
|
|
|
|
|
|
|
const activatePromo = async () => {
|
|
|
|
const activatePromoRequest: activatePromoResponse = await apiFetch( {
|
2023-06-16 14:32:58 +00:00
|
|
|
path: `/wc-analytics/admin/notes/experimental-activate-promo/${ incentive.id }`,
|
2022-01-12 06:46:33 +00:00
|
|
|
method: 'POST',
|
|
|
|
} );
|
|
|
|
if ( activatePromoRequest?.success ) {
|
|
|
|
window.location.href = connectUrl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSetup = async () => {
|
|
|
|
setSubmitted( true );
|
|
|
|
recordEvent( 'wcpay_connect_account_clicked', {
|
|
|
|
wpcom_connection: isJetpackConnected ? 'Yes' : 'No',
|
2023-06-19 11:04:14 +00:00
|
|
|
incentive_id: incentive.id,
|
2022-01-12 06:46:33 +00:00
|
|
|
} );
|
|
|
|
|
2022-09-23 12:36:12 +00:00
|
|
|
const pluginsToInstall = [ ...enabledApms ].map(
|
|
|
|
( apm ) => apm.extension
|
|
|
|
);
|
|
|
|
|
2022-01-12 06:46:33 +00:00
|
|
|
try {
|
|
|
|
const installAndActivateResponse = await installAndActivatePlugins(
|
2022-09-23 12:36:12 +00:00
|
|
|
[ 'woocommerce-payments' ].concat( pluginsToInstall )
|
2022-01-12 06:46:33 +00:00
|
|
|
);
|
|
|
|
if ( installAndActivateResponse?.success ) {
|
2022-09-23 12:36:12 +00:00
|
|
|
recordEvent( 'wcpay_extension_installed', {
|
|
|
|
extensions: [ ...enabledApms ]
|
|
|
|
.map( ( apm ) => apm.id )
|
|
|
|
.join( ', ' ),
|
2023-06-19 11:04:14 +00:00
|
|
|
incentive_id: incentive.id,
|
2022-09-23 12:36:12 +00:00
|
|
|
} );
|
2022-01-12 06:46:33 +00:00
|
|
|
await activatePromo();
|
|
|
|
} else {
|
|
|
|
throw new Error( installAndActivateResponse.message );
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
2022-04-19 09:07:55 +00:00
|
|
|
if ( e instanceof Error ) {
|
2023-06-16 14:32:58 +00:00
|
|
|
setErrorMessage( e.message );
|
|
|
|
setSubmitted( false );
|
2022-04-19 09:07:55 +00:00
|
|
|
} else {
|
|
|
|
throw new Error( `Unexpected error occurred. ${ e }` );
|
|
|
|
}
|
2022-01-12 06:46:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-16 14:32:58 +00:00
|
|
|
if ( ! incentive ) return null;
|
2022-01-12 06:46:33 +00:00
|
|
|
|
|
|
|
return (
|
2023-06-16 14:32:58 +00:00
|
|
|
<div className="woopayments-welcome-page">
|
|
|
|
{ errorMessage && (
|
|
|
|
<Notice status="error" isDismissible={ false }>
|
|
|
|
{ errorMessage }
|
|
|
|
</Notice>
|
|
|
|
) }
|
|
|
|
<Banner isSubmitted={ isSubmitted } handleSetup={ handleSetup } />
|
|
|
|
<ApmList
|
|
|
|
enabledApms={ enabledApms }
|
|
|
|
setEnabledApms={ setEnabledApms }
|
|
|
|
/>
|
|
|
|
<FrequentlyAskedQuestionsSimple />
|
2022-01-12 06:46:33 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ConnectAccountPage;
|