2022-12-15 13:04:28 +00:00
/ * *
* External dependencies
* /
import { registerPlugin , PluginArea } from '@wordpress/plugins' ;
import { _ _ } from '@wordpress/i18n' ;
import interpolateComponents from '@automattic/interpolate-components' ;
import {
Button ,
Card ,
CardBody ,
createSlotFill ,
SlotFillProvider ,
} from '@wordpress/components' ;
import { Icon , closeSmall } from '@wordpress/icons' ;
import { useEffect , useState } from '@wordpress/element' ;
import { useDispatch } from '@wordpress/data' ;
import { store as noticesStore } from '@wordpress/notices' ;
2022-12-16 08:57:09 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2022-12-15 13:04:28 +00:00
/ * *
* Internal dependencies
* /
import './conflict-error-slotfill.scss' ;
import warningIcon from './alert-triangle-icon.svg' ;
const { Fill , Slot } = createSlotFill ( '__EXPERIMENTAL__WcAdminConflictError' ) ;
const LearnMore = ( ) => (
< Button
href = "https://woocommerce.com/document/setting-up-taxes-in-woocommerce/"
target = "_blank"
>
{ _ _ ( 'Learn more' , 'woocommerce' ) }
< / B u t t o n >
) ;
const SettingsErrorFill = ( ) => {
const [ dismissedConflictWarning , setDismissedConflictWarning ] =
useState ( false ) ;
const [ pricesEnteredWithTaxSetting , setMainVal ] = useState (
document . forms . mainform . elements . woocommerce _prices _include _tax
. value === 'yes'
? 'incl'
: 'excl'
) ;
const [ displayPricesInShopWithTaxSetting , setDisplayShop ] = useState (
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
window . jQuery ( '#woocommerce_tax_display_shop' ) . val ( )
) ;
const [ displayPricesInCartWithTaxSetting , setDisplayCart ] = useState (
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
window . jQuery ( '#woocommerce_tax_display_cart' ) . val ( )
) ;
const { createNotice } = useDispatch ( noticesStore ) ;
const handleApplyRecommendedSettings = ( ) => {
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
// eslint-disable-next-line no-undef
window
. jQuery ( '#woocommerce_tax_display_shop' )
. val ( pricesEnteredWithTaxSetting )
. trigger ( 'change' ) ;
window
. jQuery ( '#woocommerce_tax_display_cart' )
. val ( pricesEnteredWithTaxSetting )
. trigger ( 'change' ) ;
createNotice (
'success' ,
_ _ ( 'Recommended settings applied.' , 'woocommerce' )
) ;
2022-12-16 08:57:09 +00:00
recordEvent ( 'tax_settings_conflict_recommended_settings_clicked' ) ;
2022-12-15 13:04:28 +00:00
} ;
const ApplyRecommendedSettingsButton = ( ) => (
< Button variant = "primary" onClick = { handleApplyRecommendedSettings } >
{ _ _ ( 'Use recommended settings' , 'woocommerce' ) }
< / B u t t o n >
) ;
useEffect ( ( ) => {
document
. querySelectorAll ( "input[name='woocommerce_prices_include_tax']" )
. forEach ( ( input ) => {
input . addEventListener ( 'change' , ( ) =>
setMainVal (
document . forms . mainform . elements
. woocommerce _prices _include _tax . value === 'yes'
? 'incl'
: 'excl'
)
) ;
} ) ;
} , [ ] ) ;
useEffect ( ( ) => {
window
. jQuery ( '#woocommerce_tax_display_shop' )
. on ( 'click change' , ( ) =>
setDisplayShop (
document . getElementById ( 'woocommerce_tax_display_shop' )
. value
)
) ;
} , [ ] ) ;
useEffect ( ( ) => {
window
. jQuery ( '#woocommerce_tax_display_cart' )
. on ( 'click change' , ( ) =>
setDisplayCart (
document . getElementById ( 'woocommerce_tax_display_cart' )
. value
)
) ;
} , [ ] ) ;
const [ isConflict , setIsConflict ] = useState ( false ) ;
useEffect ( ( ) => {
if (
displayPricesInShopWithTaxSetting === pricesEnteredWithTaxSetting &&
displayPricesInCartWithTaxSetting === pricesEnteredWithTaxSetting
) {
setIsConflict ( false ) ;
} else {
setIsConflict ( true ) ;
2022-12-16 08:57:09 +00:00
recordEvent ( 'tax_settings_conflict' , {
main : pricesEnteredWithTaxSetting ,
shop : displayPricesInShopWithTaxSetting ,
cart : displayPricesInCartWithTaxSetting ,
} ) ;
2022-12-15 13:04:28 +00:00
}
} , [
displayPricesInCartWithTaxSetting ,
displayPricesInShopWithTaxSetting ,
pricesEnteredWithTaxSetting ,
] ) ;
if ( ! isConflict || dismissedConflictWarning ) {
return < Fill > < / F i l l > ;
}
return (
< Fill >
< div className = "woocommerce_tax_settings_conflict_error" >
< Card >
< CardBody className = "woocommerce_tax_settings_conflict_error_card_body" >
< div >
< img
className = "woocommerce_tax_settings_conflict_error_card_body__warning_icon"
src = { warningIcon }
alt = "Warning Icon"
/ >
< / d i v >
< div >
< div className = "woocommerce_tax_settings_conflict_error_card_body__body_text" >
< p style = { { fontSize : 13 } } >
{ interpolateComponents ( {
mixedString : _ _ (
'{{b}}Inconsistent tax settings:{{/b}} To avoid possible rounding errors, prices should be entered and displayed consistently in all locations either including, or excluding taxes.' ,
'woocommerce'
) ,
components : {
b : < b / > ,
} ,
} ) }
< / p >
< / d i v >
< div className = "woocommerce_tax_settings_conflict_error_card_body__buttons" >
< ApplyRecommendedSettingsButton / > < LearnMore / >
< / d i v >
< / d i v >
< div >
< Button
className = "woocommerce_tax_settings_conflict_error_card_body__close_icon"
2022-12-16 08:57:09 +00:00
onClick = { ( ) => {
setDismissedConflictWarning ( true ) ;
recordEvent (
'tax_settings_conflict_dismissed'
) ;
} }
2022-12-15 13:04:28 +00:00
>
< Icon icon = { closeSmall } / >
< / B u t t o n >
< / d i v >
< / C a r d B o d y >
< / C a r d >
< / d i v >
< / F i l l >
) ;
} ;
export const WcAdminConflictErrorSlot = ( ) => {
return (
< >
< SlotFillProvider >
< Slot / >
< PluginArea scope = "woocommerce-settings" / >
< / S l o t F i l l P r o v i d e r >
< / >
) ;
} ;
registerPlugin ( 'woocommerce-admin-tax-settings-conflict-warning' , {
scope : 'woocommerce-settings' ,
render : SettingsErrorFill ,
} ) ;