2021-04-20 17:17:19 +00:00
/ * *
* External dependencies
* /
import { __ } from '@wordpress/i18n' ;
import { useSelect , useDispatch } from '@wordpress/data' ;
import { decodeEntities } from '@wordpress/html-entities' ;
2021-07-26 18:34:08 +00:00
import { Card , CardHeader , CardFooter , Button } from '@wordpress/components' ;
2021-04-20 17:17:19 +00:00
import { useEffect , useRef , useState } from '@wordpress/element' ;
import { EllipsisMenu , List , Pill } from '@woocommerce/components' ;
import { Text } from '@woocommerce/experimental' ;
import {
2022-03-07 13:15:06 +00:00
ONBOARDING_STORE_NAME ,
PAYMENT_GATEWAYS_STORE_NAME ,
2021-04-20 17:17:19 +00:00
PLUGINS_STORE_NAME ,
Plugin ,
2021-11-16 13:57:23 +00:00
PluginsStoreActions ,
2021-04-20 17:17:19 +00:00
} from '@woocommerce/data' ;
import { recordEvent } from '@woocommerce/tracks' ;
2021-05-20 11:31:14 +00:00
import ExternalIcon from 'gridicons/dist/external' ;
2021-04-20 17:17:19 +00:00
/ * *
* Internal dependencies
* /
import './payment-recommendations.scss' ;
import { createNoticesFromResponse } from '../lib/notices' ;
2022-03-07 13:15:06 +00:00
import { getPluginSlug } from '~/utils' ;
2021-04-20 17:17:19 +00:00
const SEE_MORE_LINK =
'https://woocommerce.com/product-category/woocommerce-extensions/payment-gateways/?utm_source=payments_recommendations' ;
2021-09-08 16:41:00 +00:00
const WcPayPromotionGateway = document . querySelector (
'[data-gateway_id="pre_install_woocommerce_payments_promotion"]'
) ;
2021-04-20 17:17:19 +00:00
const PaymentRecommendations : React.FC = ( ) = > {
const [ installingPlugin , setInstallingPlugin ] = useState < string | null > (
null
) ;
2022-03-07 13:15:06 +00:00
const [ isDismissed , setIsDismissed ] = useState < boolean > ( false ) ;
const [ isInstalled , setIsInstalled ] = useState < boolean > ( false ) ;
2021-11-16 13:57:23 +00:00
const {
installAndActivatePlugins ,
dismissRecommendedPlugins ,
} : PluginsStoreActions = useDispatch ( PLUGINS_STORE_NAME ) ;
const { createNotice } = useDispatch ( 'core/notices' ) ;
2022-03-07 13:15:06 +00:00
const {
installedPaymentGateway ,
installedPaymentGateways ,
paymentGatewaySuggestions ,
isResolving ,
} = useSelect (
( select ) = > {
const installingGatewayId =
isInstalled && getPluginSlug ( installingPlugin ) ;
return {
installedPaymentGateway :
installingGatewayId &&
select ( PAYMENT_GATEWAYS_STORE_NAME ) . getPaymentGateway (
installingGatewayId
) ,
installedPaymentGateways : select ( PAYMENT_GATEWAYS_STORE_NAME )
. getPaymentGateways ( )
. reduce ( ( gateways , gateway ) = > {
if ( installingGatewayId === gateway . id ) {
return gateways ;
}
gateways [ gateway . id ] = true ;
return gateways ;
} , { } ) ,
isResolving : select ( ONBOARDING_STORE_NAME ) . isResolving (
'getPaymentGatewaySuggestions'
) ,
paymentGatewaySuggestions : select (
ONBOARDING_STORE_NAME
) . getPaymentGatewaySuggestions ( ) ,
} ;
} ,
[ isInstalled ]
2021-04-20 17:17:19 +00:00
) ;
2022-03-07 13:15:06 +00:00
2021-04-20 17:17:19 +00:00
const triggeredPageViewRef = useRef ( false ) ;
const shouldShowRecommendations =
2022-03-07 13:15:06 +00:00
paymentGatewaySuggestions &&
paymentGatewaySuggestions . length > 0 &&
! isDismissed ;
2021-04-20 17:17:19 +00:00
useEffect ( ( ) = > {
2021-09-08 16:41:00 +00:00
if (
( shouldShowRecommendations ||
2022-03-07 13:15:06 +00:00
( WcPayPromotionGateway && ! isResolving ) ) &&
2021-09-08 16:41:00 +00:00
! triggeredPageViewRef . current
) {
2021-04-20 17:17:19 +00:00
triggeredPageViewRef . current = true ;
2022-03-07 13:15:06 +00:00
const eventProps = ( paymentGatewaySuggestions || [ ] ) . reduce (
2021-09-08 16:41:00 +00:00
( props , plugin ) = > {
2021-11-16 13:57:23 +00:00
if ( plugin . plugins && plugin . plugins . length > 0 ) {
2021-09-08 16:41:00 +00:00
return {
. . . props ,
2021-11-16 13:57:23 +00:00
[ plugin . plugins [ 0 ] . replace ( /\-/g , '_' ) +
2021-09-08 16:41:00 +00:00
'_displayed' ] : true ,
} ;
}
return props ;
} ,
{
woocommerce_payments_displayed : ! ! WcPayPromotionGateway ,
}
) ;
recordEvent (
'settings_payments_recommendations_pageview' ,
eventProps
) ;
2021-04-20 17:17:19 +00:00
}
2022-03-07 13:15:06 +00:00
} , [ shouldShowRecommendations , WcPayPromotionGateway , isResolving ] ) ;
useEffect ( ( ) = > {
if ( ! installedPaymentGateway ) {
return ;
}
window . location . href = installedPaymentGateway . settings_url ;
} , [ installedPaymentGateway ] ) ;
2021-04-20 17:17:19 +00:00
if ( ! shouldShowRecommendations ) {
return null ;
}
2021-11-16 13:57:23 +00:00
const dismissPaymentRecommendations = async ( ) = > {
2022-03-07 13:15:06 +00:00
setIsDismissed ( true ) ;
2021-04-20 17:17:19 +00:00
recordEvent ( 'settings_payments_recommendations_dismiss' , { } ) ;
2021-11-16 13:57:23 +00:00
const success = await dismissRecommendedPlugins ( 'payments' ) ;
2022-03-07 13:15:06 +00:00
if ( ! success ) {
setIsDismissed ( false ) ;
2021-11-16 13:57:23 +00:00
createNotice (
'error' ,
__ (
2022-03-07 13:15:06 +00:00
'There was a problem hiding the "Additional ways to get paid" card.' ,
2021-11-16 13:57:23 +00:00
'woocommerce-admin'
)
) ;
}
2021-04-20 17:17:19 +00:00
} ;
const setupPlugin = ( plugin : Plugin ) = > {
if ( installingPlugin ) {
return ;
}
2021-11-16 13:57:23 +00:00
setInstallingPlugin ( plugin . id ) ;
2021-04-20 17:17:19 +00:00
recordEvent ( 'settings_payments_recommendations_setup' , {
2021-11-16 13:57:23 +00:00
extension_selected : plugin.plugins [ 0 ] ,
2021-04-20 17:17:19 +00:00
} ) ;
2021-11-16 13:57:23 +00:00
installAndActivatePlugins ( [ plugin . plugins [ 0 ] ] )
2021-04-20 17:17:19 +00:00
. then ( ( ) = > {
2022-03-07 13:15:06 +00:00
setIsInstalled ( true ) ;
2021-04-20 17:17:19 +00:00
} )
. catch ( ( response : { errors : Record < string , string > } ) = > {
createNoticesFromResponse ( response ) ;
setInstallingPlugin ( null ) ;
} ) ;
} ;
2022-03-07 13:15:06 +00:00
const pluginsList = ( paymentGatewaySuggestions || [ ] )
. filter ( ( plugin : Plugin ) = > {
return (
! installedPaymentGateways [ plugin . id ] &&
plugin . plugins ? . length &&
( ! window . wcAdminFeatures [ 'wc-pay-promotion' ] ||
! plugin . id . startsWith ( 'woocommerce_payments' ) )
) ;
} )
. map ( ( plugin : Plugin ) = > {
2021-04-20 17:17:19 +00:00
return {
2021-11-16 13:57:23 +00:00
key : plugin.id ,
2021-04-20 17:17:19 +00:00
title : (
< >
{ plugin . title }
{ plugin . recommended && (
< Pill >
{ __ ( 'Recommended' , 'woocommerce-admin' ) }
< / Pill >
) }
< / >
) ,
2021-11-16 13:57:23 +00:00
content : decodeEntities ( plugin . content ) ,
2021-04-20 17:17:19 +00:00
after : (
< Button
isSecondary
onClick = { ( ) = > setupPlugin ( plugin ) }
2021-11-16 13:57:23 +00:00
isBusy = { installingPlugin === plugin . id }
2021-04-20 17:17:19 +00:00
disabled = { ! ! installingPlugin }
>
2022-03-07 13:15:06 +00:00
{ plugin . actionText ||
__ ( 'Get started' , 'woocommerce-admin' ) }
2021-04-20 17:17:19 +00:00
< / Button >
) ,
2022-03-07 13:15:06 +00:00
before : (
< img src = { plugin . square_image || plugin . image } alt = "" / >
) ,
2021-04-20 17:17:19 +00:00
} ;
2022-03-07 13:15:06 +00:00
} ) ;
2021-04-20 17:17:19 +00:00
return (
2021-07-26 18:34:08 +00:00
< Card size = "medium" className = "woocommerce-recommended-payments-card" >
< CardHeader >
2021-04-20 17:17:19 +00:00
< div className = "woocommerce-recommended-payments-card__header" >
2021-06-28 01:14:59 +00:00
< Text
variant = "title.small"
as = "p"
size = "20"
lineHeight = "28px"
>
2021-04-20 17:17:19 +00:00
{ __ (
2022-03-07 13:15:06 +00:00
'Additional ways to get paid' ,
2021-04-20 17:17:19 +00:00
'woocommerce-admin'
) }
< / Text >
< Text
className = {
'woocommerce-recommended-payments__header-heading'
}
variant = "caption"
2021-06-28 01:14:59 +00:00
as = "p"
size = "12"
lineHeight = "16px"
2021-04-20 17:17:19 +00:00
>
{ __ (
'We recommend adding one of the following payment extensions to your store. The extension will be installed and activated for you when you click "Get started".' ,
'woocommerce-admin'
) }
< / Text >
< / div >
< div className = "woocommerce-card__menu woocommerce-card__header-item" >
< EllipsisMenu
label = { __ ( 'Task List Options' , 'woocommerce-admin' ) }
renderContent = { ( ) = > (
< div className = "woocommerce-review-activity-card__section-controls" >
< Button
onClick = { dismissPaymentRecommendations }
>
{ __ ( 'Hide this' , 'woocommerce-admin' ) }
< / Button >
< / div >
) }
/ >
< / div >
< / CardHeader >
2021-07-26 18:34:08 +00:00
< List items = { pluginsList } / >
2021-04-20 17:17:19 +00:00
< CardFooter >
2021-06-08 02:06:02 +00:00
< Button href = { SEE_MORE_LINK } target = "_blank" isTertiary >
2021-04-20 17:17:19 +00:00
{ __ ( 'See more options' , 'woocommerce-admin' ) }
2021-05-20 11:31:14 +00:00
< ExternalIcon size = { 18 } / >
2021-04-20 17:17:19 +00:00
< / Button >
< / CardFooter >
< / Card >
) ;
} ;
export default PaymentRecommendations ;