2021-09-09 12:25:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { Button } from '@wordpress/components';
|
2021-11-30 18:02:54 +00:00
|
|
|
import { EllipsisMenu, Link } from '@woocommerce/components';
|
2021-09-09 12:25:13 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
|
|
|
import {
|
|
|
|
PLUGINS_STORE_NAME,
|
|
|
|
PAYMENT_GATEWAYS_STORE_NAME,
|
|
|
|
WCDataSelector,
|
|
|
|
PluginsStoreActions,
|
|
|
|
} from '@woocommerce/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
|
|
|
import { sanitize } from 'dompurify';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './payment-promotion-row.scss';
|
|
|
|
|
|
|
|
function sanitizeHTML( html: string ) {
|
|
|
|
return {
|
|
|
|
__html: sanitize( html, {
|
|
|
|
ALLOWED_TAGS: [ 'a', 'img', 'br' ],
|
|
|
|
ALLOWED_ATTR: [ 'href', 'src', 'class', 'alt', 'target' ],
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type PaymentPromotionRowProps = {
|
2021-11-30 18:02:54 +00:00
|
|
|
paymentMethod: {
|
|
|
|
gatewayId: string;
|
|
|
|
pluginSlug: string;
|
|
|
|
url: string;
|
|
|
|
};
|
2021-09-09 12:25:13 +00:00
|
|
|
title: string;
|
2021-11-18 04:53:50 +00:00
|
|
|
columns: {
|
|
|
|
className: string;
|
|
|
|
html: string;
|
|
|
|
width: string;
|
|
|
|
}[];
|
2021-09-09 12:25:13 +00:00
|
|
|
subTitleContent?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PaymentPromotionRow: React.FC< PaymentPromotionRowProps > = ( {
|
2021-11-30 18:02:54 +00:00
|
|
|
paymentMethod,
|
2021-09-09 12:25:13 +00:00
|
|
|
title,
|
|
|
|
subTitleContent,
|
2021-11-18 04:53:50 +00:00
|
|
|
columns,
|
2021-09-09 12:25:13 +00:00
|
|
|
} ) => {
|
2021-11-30 18:02:54 +00:00
|
|
|
const { gatewayId, pluginSlug, url } = paymentMethod;
|
2021-09-09 12:25:13 +00:00
|
|
|
const [ installing, setInstalling ] = useState( false );
|
2021-11-30 18:02:54 +00:00
|
|
|
const [ isVisible, setIsVisible ] = useState( true );
|
2021-09-09 12:25:13 +00:00
|
|
|
const { installAndActivatePlugins }: PluginsStoreActions = useDispatch(
|
|
|
|
PLUGINS_STORE_NAME
|
|
|
|
);
|
|
|
|
const { createNotice } = useDispatch( 'core/notices' );
|
2021-11-30 18:02:54 +00:00
|
|
|
const { updatePaymentGateway } = useDispatch( PAYMENT_GATEWAYS_STORE_NAME );
|
2021-09-09 12:25:13 +00:00
|
|
|
const { gatewayIsActive, paymentGateway } = useSelect(
|
|
|
|
( select: WCDataSelector ) => {
|
|
|
|
const { getPaymentGateway } = select( PAYMENT_GATEWAYS_STORE_NAME );
|
|
|
|
const activePlugins: string[] = select(
|
|
|
|
PLUGINS_STORE_NAME
|
|
|
|
).getActivePlugins();
|
|
|
|
const isActive =
|
|
|
|
activePlugins && activePlugins.includes( pluginSlug );
|
|
|
|
let paymentGatewayData;
|
|
|
|
if ( isActive ) {
|
|
|
|
paymentGatewayData = getPaymentGateway(
|
|
|
|
pluginSlug.replace( /\-/g, '_' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
gatewayIsActive: isActive,
|
|
|
|
paymentGateway: paymentGatewayData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if (
|
|
|
|
gatewayIsActive &&
|
|
|
|
paymentGateway &&
|
|
|
|
paymentGateway.settings_url
|
|
|
|
) {
|
|
|
|
window.location.href = paymentGateway.settings_url;
|
|
|
|
}
|
|
|
|
}, [ gatewayIsActive, paymentGateway ] );
|
|
|
|
|
|
|
|
const installPaymentGateway = () => {
|
|
|
|
if ( installing ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setInstalling( true );
|
|
|
|
recordEvent( 'settings_payments_recommendations_setup', {
|
|
|
|
extension_selected: pluginSlug,
|
|
|
|
} );
|
|
|
|
installAndActivatePlugins( [ pluginSlug ] ).catch(
|
|
|
|
( response: { message?: string } ) => {
|
|
|
|
if ( response.message ) {
|
|
|
|
createNotice( 'error', response.message );
|
|
|
|
}
|
|
|
|
setInstalling( false );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-11-30 18:02:54 +00:00
|
|
|
const onDismiss = () => {
|
|
|
|
setIsVisible( false );
|
|
|
|
recordEvent( 'settings_payments_promotions_dismiss', {
|
|
|
|
id: gatewayId,
|
|
|
|
} );
|
|
|
|
updatePaymentGateway( gatewayId, {
|
|
|
|
settings: {
|
|
|
|
is_dismissed: 'yes',
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( ! isVisible ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:25:13 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-11-18 04:53:50 +00:00
|
|
|
{ columns.map( ( column ) => {
|
|
|
|
if ( column.className.includes( 'name' ) ) {
|
|
|
|
return (
|
|
|
|
<td className="name" key={ column.className }>
|
2021-11-30 18:02:54 +00:00
|
|
|
<div className="wc-payment-gateway-method__name">
|
2021-11-18 04:53:50 +00:00
|
|
|
<Link
|
|
|
|
target="_blank"
|
|
|
|
type="external"
|
|
|
|
rel="noreferrer"
|
2021-11-30 18:02:54 +00:00
|
|
|
href={ url }
|
2021-11-18 04:53:50 +00:00
|
|
|
>
|
|
|
|
{ title }
|
|
|
|
</Link>
|
|
|
|
{ subTitleContent ? (
|
|
|
|
<div
|
2021-11-30 18:02:54 +00:00
|
|
|
className="pre-install-payment-gateway__subtitle"
|
2021-11-18 04:53:50 +00:00
|
|
|
dangerouslySetInnerHTML={ sanitizeHTML(
|
|
|
|
subTitleContent
|
|
|
|
) }
|
|
|
|
></div>
|
|
|
|
) : null }
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
);
|
|
|
|
} else if ( column.className.includes( 'status' ) ) {
|
|
|
|
return (
|
|
|
|
<td
|
2021-11-30 18:02:54 +00:00
|
|
|
className="pre-install-payment-gateway__status"
|
2021-11-18 04:53:50 +00:00
|
|
|
key={ column.className }
|
|
|
|
></td>
|
|
|
|
);
|
|
|
|
} else if ( column.className.includes( 'action' ) ) {
|
|
|
|
return (
|
|
|
|
<td className="action" key={ column.className }>
|
2021-11-30 18:02:54 +00:00
|
|
|
<div className="pre-install-payment-gateway__actions">
|
|
|
|
<EllipsisMenu
|
|
|
|
label={ __(
|
|
|
|
'Payment Promotion Options',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
className="pre-install-payment-gateway__actions-menu"
|
|
|
|
onToggle={ (
|
|
|
|
e:
|
|
|
|
| React.MouseEvent
|
|
|
|
| React.KeyboardEvent
|
|
|
|
) => e.stopPropagation() }
|
|
|
|
renderContent={ () => (
|
|
|
|
<div className="pre-install-payment-gateway__actions-menu-options">
|
|
|
|
<Button onClick={ onDismiss }>
|
|
|
|
{ __(
|
|
|
|
'Dismiss',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
className="button alignright"
|
|
|
|
onClick={ () => installPaymentGateway() }
|
|
|
|
isSecondary
|
|
|
|
isBusy={ installing }
|
|
|
|
aria-disabled={ installing }
|
|
|
|
>
|
|
|
|
{ __( 'Install', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
</div>
|
2021-11-18 04:53:50 +00:00
|
|
|
</td>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<td
|
|
|
|
key={ column.className }
|
|
|
|
className={ column.className }
|
|
|
|
width={ column.width }
|
|
|
|
dangerouslySetInnerHTML={
|
|
|
|
column.className.includes( 'sort' )
|
|
|
|
? {
|
|
|
|
__html: column.html,
|
|
|
|
}
|
|
|
|
: sanitizeHTML( column.html )
|
|
|
|
}
|
|
|
|
></td>
|
|
|
|
);
|
|
|
|
} ) }
|
2021-09-09 12:25:13 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|