2021-09-21 19:33:44 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button, Spinner } from '@wordpress/components';
|
|
|
|
import { updateQueryString } from '@woocommerce/navigation';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useState } from '@wordpress/element';
|
2022-01-25 21:47:36 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getPluginTrackKey } from '~/utils';
|
2021-09-21 19:33:44 +00:00
|
|
|
|
|
|
|
export const Action = ( {
|
|
|
|
hasSetup = false,
|
|
|
|
needsSetup = true,
|
|
|
|
id,
|
|
|
|
isEnabled = false,
|
|
|
|
isLoading = false,
|
|
|
|
isInstalled = false,
|
|
|
|
isRecommended = false,
|
|
|
|
hasPlugins,
|
|
|
|
manageUrl = null,
|
|
|
|
markConfigured,
|
|
|
|
onSetUp = () => {},
|
|
|
|
onSetupCallback,
|
2022-04-12 02:52:11 +00:00
|
|
|
setupButtonText = __( 'Get started', 'woocommerce' ),
|
2022-04-13 13:39:44 +00:00
|
|
|
externalLink = null,
|
2021-09-21 19:33:44 +00:00
|
|
|
} ) => {
|
|
|
|
const [ isBusy, setIsBusy ] = useState( false );
|
|
|
|
|
|
|
|
const classes = 'woocommerce-task-payment__action';
|
|
|
|
|
|
|
|
if ( isLoading ) {
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleClick = async () => {
|
|
|
|
onSetUp( id );
|
|
|
|
|
2022-01-25 21:47:36 +00:00
|
|
|
recordEvent( 'tasklist_payment_setup', {
|
|
|
|
selected: getPluginTrackKey( id ),
|
|
|
|
} );
|
|
|
|
|
2022-04-13 13:39:44 +00:00
|
|
|
if ( ! hasPlugins && externalLink ) {
|
|
|
|
window.location.href = externalLink;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
if ( onSetupCallback ) {
|
|
|
|
setIsBusy( true );
|
|
|
|
await new Promise( onSetupCallback )
|
|
|
|
.then( () => {
|
|
|
|
setIsBusy( false );
|
|
|
|
} )
|
|
|
|
.catch( () => {
|
|
|
|
setIsBusy( false );
|
|
|
|
} );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateQueryString( {
|
|
|
|
id,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const ManageButton = () => (
|
|
|
|
<Button
|
|
|
|
className={ classes }
|
|
|
|
isSecondary
|
|
|
|
role="button"
|
|
|
|
href={ manageUrl }
|
|
|
|
onClick={ () => recordEvent( 'tasklist_payment_manage', { id } ) }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Manage', 'woocommerce' ) }
|
2021-09-21 19:33:44 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
|
|
|
const SetupButton = () => (
|
|
|
|
<Button
|
|
|
|
className={ classes }
|
|
|
|
isPrimary={ isRecommended }
|
|
|
|
isSecondary={ ! isRecommended }
|
|
|
|
isBusy={ isBusy }
|
|
|
|
disabled={ isBusy }
|
|
|
|
onClick={ () => handleClick() }
|
|
|
|
>
|
|
|
|
{ setupButtonText }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2022-04-13 13:39:44 +00:00
|
|
|
const EnableButton = () => (
|
|
|
|
<Button
|
|
|
|
className={ classes }
|
|
|
|
isSecondary
|
|
|
|
onClick={ () => markConfigured( id ) }
|
|
|
|
>
|
|
|
|
{ __( 'Enable', 'woocommerce' ) }
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
if ( ! hasSetup ) {
|
|
|
|
if ( ! isEnabled ) {
|
2022-04-13 13:39:44 +00:00
|
|
|
return <EnableButton />;
|
2021-09-21 19:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return <ManageButton />;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This isolates core gateways that include setup
|
|
|
|
if ( ! hasPlugins ) {
|
|
|
|
if ( isEnabled ) {
|
|
|
|
return <ManageButton />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <SetupButton />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! needsSetup ) {
|
2022-04-14 00:37:53 +00:00
|
|
|
if ( ! isEnabled ) {
|
|
|
|
return <EnableButton />;
|
|
|
|
}
|
|
|
|
|
2021-09-21 19:33:44 +00:00
|
|
|
return <ManageButton />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isInstalled && hasPlugins ) {
|
|
|
|
return (
|
2021-11-02 13:03:04 +00:00
|
|
|
<Button
|
|
|
|
className={ classes }
|
|
|
|
isPrimary={ isRecommended }
|
|
|
|
isSecondary={ ! isRecommended }
|
|
|
|
isBusy={ isBusy }
|
|
|
|
disabled={ isBusy }
|
|
|
|
onClick={ () => handleClick() }
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
{ __( 'Finish setup', 'woocommerce' ) }
|
2021-11-02 13:03:04 +00:00
|
|
|
</Button>
|
2021-09-21 19:33:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <SetupButton />;
|
|
|
|
};
|