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 {
PLUGINS_STORE_NAME ,
WCDataSelector ,
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' ;
2022-01-06 12:53:30 +00:00
import { getAdminLink } from '@woocommerce/settings' ;
2021-04-20 17:17:19 +00:00
/ * *
* Internal dependencies
* /
import './payment-recommendations.scss' ;
import { createNoticesFromResponse } from '../lib/notices' ;
const SEE_MORE_LINK =
'https://woocommerce.com/product-category/woocommerce-extensions/payment-gateways/?utm_source=payments_recommendations' ;
export function getPaymentRecommendationData (
select : WCDataSelector
) : {
recommendedPlugins? : Plugin [ ] ;
2021-09-08 16:41:00 +00:00
isLoading : boolean ;
2021-04-20 17:17:19 +00:00
} {
const { getRecommendedPlugins } = select ( PLUGINS_STORE_NAME ) ;
2021-11-16 13:57:23 +00:00
const plugins = getRecommendedPlugins ( 'payments' ) ;
const isLoading = plugins === undefined ;
2021-04-20 17:17:19 +00:00
return {
recommendedPlugins : plugins ,
2021-09-08 16:41:00 +00:00
isLoading ,
2021-04-20 17:17:19 +00:00
} ;
}
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
) ;
2021-11-16 13:57:23 +00:00
const {
installAndActivatePlugins ,
dismissRecommendedPlugins ,
invalidateResolution ,
} : PluginsStoreActions = useDispatch ( PLUGINS_STORE_NAME ) ;
const { createNotice } = useDispatch ( 'core/notices' ) ;
const { recommendedPlugins , isLoading } = useSelect (
2021-04-20 17:17:19 +00:00
getPaymentRecommendationData
) ;
const triggeredPageViewRef = useRef ( false ) ;
const shouldShowRecommendations =
2021-11-16 13:57:23 +00:00
recommendedPlugins && recommendedPlugins . length > 0 ;
2021-04-20 17:17:19 +00:00
useEffect ( ( ) = > {
2021-09-08 16:41:00 +00:00
if (
( shouldShowRecommendations ||
( WcPayPromotionGateway && ! isLoading ) ) &&
! triggeredPageViewRef . current
) {
2021-04-20 17:17:19 +00:00
triggeredPageViewRef . current = true ;
2021-09-08 16:41:00 +00:00
const eventProps = ( recommendedPlugins || [ ] ) . reduce (
( 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
}
2021-09-08 16:41:00 +00:00
} , [ shouldShowRecommendations , WcPayPromotionGateway , isLoading ] ) ;
2021-04-20 17:17:19 +00:00
if ( ! shouldShowRecommendations ) {
return null ;
}
2021-11-16 13:57:23 +00:00
const dismissPaymentRecommendations = async ( ) = > {
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' ) ;
if ( success ) {
invalidateResolution ( 'getRecommendedPlugins' , [ 'payments' ] ) ;
} else {
createNotice (
'error' ,
__ (
'There was a problem hiding the "Recommended ways to get paid" card.' ,
'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 ( ( ) = > {
window . location . href = getAdminLink (
plugin [ 'setup-link' ] . replace ( '/wp-admin/' , '' )
) ;
} )
. catch ( ( response : { errors : Record < string , string > } ) = > {
createNoticesFromResponse ( response ) ;
setInstallingPlugin ( null ) ;
} ) ;
} ;
const pluginsList = ( recommendedPlugins || [ ] ) . map (
( plugin : Plugin ) = > {
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 }
>
{ plugin [ 'button-text' ] }
< / Button >
) ,
2021-11-16 13:57:23 +00:00
before : < img src = { plugin . image } alt = "" / > ,
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
{ __ (
'Recommended ways to get paid' ,
'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 ;