2020-03-06 12:27:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-01-20 20:35:53 +00:00
|
|
|
import classnames from 'classnames';
|
2020-03-06 12:27:54 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-03-05 14:03:48 +00:00
|
|
|
import type { Currency } from '@woocommerce/price-format';
|
|
|
|
import type { CartTotalsTaxLineItem } from '@woocommerce/type-defs/cart';
|
|
|
|
import { ReactElement } from 'react';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-01-20 20:35:53 +00:00
|
|
|
import TotalsItem from '../item';
|
2021-05-17 14:00:57 +00:00
|
|
|
import './style.scss';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
2021-03-05 14:03:48 +00:00
|
|
|
interface Values {
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
tax_lines: CartTotalsTaxLineItem[];
|
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
total_tax: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TotalsTaxesProps {
|
|
|
|
className?: string;
|
|
|
|
currency: Currency;
|
2021-05-17 14:00:57 +00:00
|
|
|
showRateAfterTaxName: boolean;
|
2021-03-05 14:03:48 +00:00
|
|
|
values: Values | Record< string, never >;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TotalsTaxes = ( {
|
|
|
|
currency,
|
|
|
|
values,
|
|
|
|
className,
|
2021-05-17 14:00:57 +00:00
|
|
|
showRateAfterTaxName,
|
2021-03-05 14:03:48 +00:00
|
|
|
}: TotalsTaxesProps ): ReactElement | null => {
|
2020-04-24 13:44:44 +00:00
|
|
|
const { total_tax: totalTax, tax_lines: taxLines } = values;
|
2020-03-06 12:27:54 +00:00
|
|
|
|
2021-04-22 11:37:27 +00:00
|
|
|
if ( ! getSetting( 'taxesEnabled', true ) ) {
|
2020-04-22 13:16:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-17 14:00:57 +00:00
|
|
|
const itemisedTaxItems: ReactElement | null = getSetting(
|
|
|
|
'displayItemizedTaxes',
|
|
|
|
false
|
|
|
|
) ? (
|
|
|
|
<>
|
|
|
|
{ taxLines.map( ( { name, rate }, i ) => {
|
|
|
|
const label = `${ name }${
|
|
|
|
showRateAfterTaxName ? ` ${ rate }` : ''
|
|
|
|
}`;
|
|
|
|
return (
|
|
|
|
<TotalsItem
|
|
|
|
key={ `tax-line-${ i }` }
|
|
|
|
className="wc-block-components-totals-taxes__tax-line"
|
|
|
|
currency={ currency }
|
|
|
|
label={ label }
|
|
|
|
value={ null }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }{ ' ' }
|
|
|
|
</>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-04-24 13:44:44 +00:00
|
|
|
<TotalsItem
|
2021-01-20 20:35:53 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-totals-taxes',
|
|
|
|
className
|
|
|
|
) }
|
2020-04-24 13:44:44 +00:00
|
|
|
currency={ currency }
|
|
|
|
label={ __( 'Taxes', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ parseInt( totalTax, 10 ) }
|
2021-05-17 14:00:57 +00:00
|
|
|
description={ itemisedTaxItems }
|
2020-04-24 13:44:44 +00:00
|
|
|
/>
|
|
|
|
</>
|
2020-03-06 12:27:54 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-12-09 07:29:34 +00:00
|
|
|
export default TotalsTaxes;
|