2024-11-26 13:28:03 +01:00
import { _ _ , sprintf } from '@wordpress/i18n' ;
import { SelectControl , RadioControl } from '@wordpress/components' ;
import { PayPalCheckboxGroup } from '../../ReusableComponents/Fields' ;
import { useState , useMemo , useEffect } from '@wordpress/element' ;
2024-11-28 10:31:16 +01:00
import { PayPalScriptProvider , PayPalButtons } from '@paypal/react-paypal-js' ;
2024-11-26 13:28:03 +01:00
import {
defaultLocationSettings ,
paymentMethodOptions ,
colorOptions ,
shapeOptions ,
buttonLayoutOptions ,
buttonLabelOptions ,
} from '../../../data/settings/tab-styling-data' ;
2024-10-25 14:35:16 +02:00
const TabStyling = ( ) => {
2024-11-26 13:28:03 +01:00
const [ location , setLocation ] = useState ( 'cart' ) ;
2024-11-28 10:31:16 +01:00
const [ canRender , setCanRender ] = useState ( false ) ;
2024-11-26 13:28:03 +01:00
const [ locationSettings , setLocationSettings ] = useState ( {
... defaultLocationSettings ,
} ) ;
2024-11-28 10:31:16 +01:00
// Sometimes buttons won't render. This fixes the timing problem.
useEffect ( ( ) => {
const handleDOMContentLoaded = ( ) => setCanRender ( true ) ;
if (
document . readyState === 'interactive' ||
document . readyState === 'complete'
) {
handleDOMContentLoaded ( ) ;
} else {
document . addEventListener (
'DOMContentLoaded' ,
handleDOMContentLoaded
) ;
}
} , [ ] ) ;
2024-11-26 13:28:03 +01:00
const currentLocationSettings = useMemo ( ( ) => {
return locationSettings [ location ] ;
} , [ location , locationSettings ] ) ;
const locationOptions = useMemo ( ( ) => {
return Object . keys ( locationSettings ) . reduce (
( locationOptionsData , key ) => {
locationOptionsData . push ( {
value : locationSettings [ key ] . value ,
label : locationSettings [ key ] . label ,
} ) ;
return locationOptionsData ;
} ,
[ ]
) ;
} , [ ] ) ;
const updateButtonSettings = ( key , value ) => {
2024-11-28 10:31:16 +01:00
setLocationSettings ( {
... locationSettings ,
[ location ] : {
... currentLocationSettings ,
settings : {
... currentLocationSettings . settings ,
[ key ] : value ,
} ,
} ,
} ) ;
} ;
2024-11-26 13:28:03 +01:00
2024-11-28 10:31:16 +01:00
const updateButtonStyle = ( key , value ) => {
2024-11-26 13:28:03 +01:00
setLocationSettings ( {
... locationSettings ,
2024-11-28 10:31:16 +01:00
[ location ] : {
... currentLocationSettings ,
settings : {
... currentLocationSettings . settings ,
style : {
... currentLocationSettings . settings . style ,
[ key ] : value ,
} ,
} ,
} ,
2024-11-26 13:28:03 +01:00
} ) ;
} ;
2024-11-28 10:31:16 +01:00
if ( ! canRender ) {
return < > < / > ;
}
2024-11-26 13:28:03 +01:00
return (
< div className = "ppcp-r-styling" >
< div className = "ppcp-r-styling__settings" >
< SectionIntro / >
2024-11-28 10:31:16 +01:00
< SectionLocations
locationOptions = { locationOptions }
location = { location }
setLocation = { setLocation }
/ >
< SectionPaymentMethods
locationSettings = { currentLocationSettings }
updateButtonSettings = { updateButtonSettings }
/ >
< SectionButtonLayout
locationSettings = { currentLocationSettings }
updateButtonStyle = { updateButtonStyle }
/ >
< SectionButtonShape
locationSettings = { currentLocationSettings }
updateButtonStyle = { updateButtonStyle }
/ >
< SectionButtonLabel
locationSettings = { currentLocationSettings }
updateButtonStyle = { updateButtonStyle }
/ >
< SectionButtonColor
locationSettings = { currentLocationSettings }
updateButtonStyle = { updateButtonStyle }
/ >
< SectionButtonTagline
locationSettings = { currentLocationSettings }
updateButtonStyle = { updateButtonStyle }
/ >
2024-11-26 13:28:03 +01:00
< / d i v >
2024-11-27 08:02:06 +01:00
< div className = "ppcp-preview ppcp-r-button-preview ppcp-r-styling__preview" >
2024-11-28 10:31:16 +01:00
< div className = "ppcp-r-styling__preview-inner" >
< SectionButtonPreview
locationSettings = { currentLocationSettings }
/ >
< / d i v >
2024-11-26 13:28:03 +01:00
< / d i v >
< / d i v >
) ;
} ;
const TabStylingSection = ( props ) => {
let sectionTitleClassName = 'ppcp-r-styling__section' ;
if ( props ? . className ) {
sectionTitleClassName += ` ${ props . className } ` ;
}
return (
< div className = { sectionTitleClassName } >
< span className = "ppcp-r-styling__title" > { props . title } < / s p a n >
{ props ? . description && (
< p
dangerouslySetInnerHTML = { {
_ _html : props . description ,
} }
className = "ppcp-r-styling__description"
/ >
) }
{ props . children }
< / d i v >
) ;
} ;
const SectionIntro = ( ) => {
const buttonStyleDescription = sprintf (
// translators: %s: Link to Classic checkout page
_ _ (
'Customize the appearance of the PayPal smart buttons on the <a href="%s">[MISSING LINK]Classic Checkout page</a>. Checkout Buttons must be enabled to display the PayPal gateway on the Checkout page.'
) ,
'#'
) ;
return (
< TabStylingSection
className = "ppcp-r-styling__section--rc ppcp-r-styling__section--empty"
title = { _ _ ( 'Button Styling' , 'wooocommerce-paypal-payments' ) }
description = { buttonStyleDescription }
> < / T a b S t y l i n g S e c t i o n >
) ;
} ;
2024-11-28 10:31:16 +01:00
const SectionLocations = ( { locationOptions , location , setLocation } ) => {
return (
< TabStylingSection className = "ppcp-r-styling__section--rc" >
< SelectControl
className = "ppcp-r-styling__select"
value = { location }
onChange = { ( newLocation ) => setLocation ( newLocation ) }
label = { _ _ ( 'Locations' , 'woocommerce-paypal-payments' ) }
options = { locationOptions }
/ >
< / T a b S t y l i n g S e c t i o n >
) ;
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionPaymentMethods = ( {
locationSettings ,
updateButtonSettings ,
} ) => {
return (
< TabStylingSection
title = { _ _ ( 'Payment Methods' , 'woocommerce-paypal-payments' ) }
className = "ppcp-r-styling__section--rc"
>
< div className = "ppcp-r-styling__payment-method-checkboxes" >
< PayPalCheckboxGroup
value = { paymentMethodOptions }
changeCallback = { ( newValue ) =>
updateButtonSettings ( 'paymentMethods' , newValue )
}
currentValue = { locationSettings . settings . paymentMethods }
/ >
< / d i v >
< / T a b S t y l i n g S e c t i o n >
) ;
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonLayout = ( { locationSettings , updateButtonStyle } ) => {
const buttonLayoutIsAllowed =
locationSettings . settings . style ? . layout &&
locationSettings . settings . style ? . tagline === false ;
return (
buttonLayoutIsAllowed && (
< TabStylingSection
className = "ppcp-r-styling__section--rc"
title = { _ _ ( 'Button Layout' , 'woocommerce-paypal-payments' ) }
>
< RadioControl
className = "ppcp-r__horizontal-control"
onChange = { ( newValue ) =>
updateButtonStyle ( 'layout' , newValue )
}
selected = { locationSettings . settings . style . layout }
options = { buttonLayoutOptions }
/ >
< / T a b S t y l i n g S e c t i o n >
)
) ;
} ;
2024-11-26 13:28:03 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonShape = ( { locationSettings , updateButtonStyle } ) => {
return (
< TabStylingSection
title = { _ _ ( 'Shape' , 'woocommerce-paypal-payments' ) }
className = "ppcp-r-styling__section--rc"
>
< RadioControl
className = "ppcp-r__horizontal-control"
onChange = { ( newValue ) =>
updateButtonStyle ( 'shape' , newValue )
}
selected = { locationSettings . settings . style . shape }
options = { shapeOptions }
/ >
< / T a b S t y l i n g S e c t i o n >
) ;
2024-10-25 14:35:16 +02:00
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonLabel = ( { locationSettings , updateButtonStyle } ) => {
return (
< TabStylingSection >
< SelectControl
className = "ppcp-r-styling__select"
onChange = { ( newValue ) =>
updateButtonStyle ( 'label' , newValue )
}
value = { locationSettings . settings . style . label }
label = { _ _ ( 'Button Label' , 'woocommerce-paypal-payments' ) }
options = { buttonLabelOptions }
/ >
< / T a b S t y l i n g S e c t i o n >
) ;
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonColor = ( { locationSettings , updateButtonStyle } ) => {
return (
< TabStylingSection >
< SelectControl
className = " ppcp-r-styling__select"
label = { _ _ ( 'Button Color' , 'woocommerce-paypal-payments' ) }
onChange = { ( newValue ) =>
updateButtonStyle ( 'color' , newValue )
}
value = { locationSettings . settings . style . color }
options = { colorOptions }
/ >
< / T a b S t y l i n g S e c t i o n >
) ;
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonTagline = ( { locationSettings , updateButtonStyle } ) => {
const taglineIsAllowed =
locationSettings . settings . style . hasOwnProperty ( 'tagline' ) &&
locationSettings . settings . style ? . layout === 'horizontal' ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
return (
taglineIsAllowed && (
< TabStylingSection
title = { _ _ ( 'Tagline' , 'woocommerce-paypal-payments' ) }
className = "ppcp-r-styling__section--rc"
>
< PayPalCheckboxGroup
value = { [
{
value : 'tagline' ,
label : _ _ (
'Enable Tagline' ,
'woocommerce-paypal-payments'
) ,
} ,
] }
changeCallback = { ( newValue ) => {
updateButtonStyle ( 'tagline' , newValue ) ;
} }
currentValue = { locationSettings . settings . style . tagline }
/ >
< / T a b S t y l i n g S e c t i o n >
)
) ;
} ;
2024-11-27 08:02:06 +01:00
2024-11-28 10:31:16 +01:00
const SectionButtonPreview = ( { locationSettings } ) => {
return (
< PayPalScriptProvider
options = { {
clientId : 'test' ,
merchantId : 'QTQX5NP6N9WZU' ,
components : 'buttons,googlepay' ,
2024-12-09 17:03:47 +04:00
'disable-funding' : 'card' ,
2024-11-28 10:31:16 +01:00
'buyer-country' : 'US' ,
currency : 'USD' ,
} }
>
< PayPalButtons
style = { locationSettings . settings . style }
forceReRender = { [ locationSettings . settings . style ] }
>
Error
< / P a y P a l B u t t o n s >
< / P a y P a l S c r i p t P r o v i d e r >
) ;
} ;
2024-10-25 14:35:16 +02:00
export default TabStyling ;