Currency package: make naming clearer (https://github.com/woocommerce/woocommerce-admin/pull/4560)
This commit is contained in:
parent
dd54268ad7
commit
03db742cdd
|
@ -7,7 +7,7 @@ import { mount, shallow } from 'enzyme';
|
||||||
* WooCommerce dependencies
|
* WooCommerce dependencies
|
||||||
*/
|
*/
|
||||||
import { numberFormat } from '@woocommerce/number';
|
import { numberFormat } from '@woocommerce/number';
|
||||||
import Currency from '@woocommerce/currency';
|
import CurrencyFactory from '@woocommerce/currency';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -16,7 +16,7 @@ import { Leaderboard } from '../';
|
||||||
import mockData from '../data/top-selling-products-mock-data';
|
import mockData from '../data/top-selling-products-mock-data';
|
||||||
import { CURRENCY } from '@woocommerce/wc-admin-settings';
|
import { CURRENCY } from '@woocommerce/wc-admin-settings';
|
||||||
|
|
||||||
const { formatCurrency, formatDecimal } = Currency( CURRENCY );
|
const { formatAmount, formatDecimal } = CurrencyFactory( CURRENCY );
|
||||||
|
|
||||||
const rows = mockData.map( ( row ) => {
|
const rows = mockData.map( ( row ) => {
|
||||||
const {
|
const {
|
||||||
|
@ -39,7 +39,7 @@ const rows = mockData.map( ( row ) => {
|
||||||
value: ordersCount,
|
value: ordersCount,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( netRevenue ),
|
display: formatAmount( netRevenue ),
|
||||||
value: formatDecimal( netRevenue ),
|
value: formatDecimal( netRevenue ),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@ -107,7 +107,7 @@ describe( 'Leaderboard', () => {
|
||||||
numberFormat( CURRENCY, mockData[ 0 ].orders_count )
|
numberFormat( CURRENCY, mockData[ 0 ].orders_count )
|
||||||
);
|
);
|
||||||
expect( tableItems.at( 3 ).text() ).toBe(
|
expect( tableItems.at( 3 ).text() ).toBe(
|
||||||
formatCurrency( mockData[ 0 ].net_revenue )
|
formatAmount( mockData[ 0 ].net_revenue )
|
||||||
);
|
);
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
|
@ -168,7 +168,7 @@ export class ReportChart extends Component {
|
||||||
const emptyMessage = emptySearchResults
|
const emptyMessage = emptySearchResults
|
||||||
? __( 'No data for the current search', 'woocommerce-admin' )
|
? __( 'No data for the current search', 'woocommerce-admin' )
|
||||||
: __( 'No data for the selected date range', 'woocommerce-admin' );
|
: __( 'No data for the selected date range', 'woocommerce-admin' );
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
return (
|
return (
|
||||||
<Chart
|
<Chart
|
||||||
allowedIntervals={ allowedIntervals }
|
allowedIntervals={ allowedIntervals }
|
||||||
|
@ -195,13 +195,13 @@ export class ReportChart extends Component {
|
||||||
}
|
}
|
||||||
tooltipValueFormat={ getTooltipValueFormat(
|
tooltipValueFormat={ getTooltipValueFormat(
|
||||||
selectedChart.type,
|
selectedChart.type,
|
||||||
formatCurrency
|
formatAmount
|
||||||
) }
|
) }
|
||||||
chartType={ getChartTypeForQuery( query ) }
|
chartType={ getChartTypeForQuery( query ) }
|
||||||
valueType={ selectedChart.type }
|
valueType={ selectedChart.type }
|
||||||
xFormat={ formats.xFormat }
|
xFormat={ formats.xFormat }
|
||||||
x2Format={ formats.x2Format }
|
x2Format={ formats.x2Format }
|
||||||
currency={ getCurrency() }
|
currency={ getCurrencyConfig() }
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -383,7 +383,7 @@ export default compose(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = charts && charts.map( chart => chart.key );
|
const fields = charts && charts.map( ( chart ) => chart.key );
|
||||||
|
|
||||||
const primaryData = getReportChartData( {
|
const primaryData = getReportChartData( {
|
||||||
endpoint,
|
endpoint,
|
||||||
|
|
|
@ -122,7 +122,7 @@ class ReportFilters extends Component {
|
||||||
<Filters
|
<Filters
|
||||||
query={ query }
|
query={ query }
|
||||||
siteLocale={ LOCALE.siteLocale }
|
siteLocale={ LOCALE.siteLocale }
|
||||||
currency={ Currency.getCurrency() }
|
currency={ Currency.getCurrencyConfig() }
|
||||||
path={ path }
|
path={ path }
|
||||||
filters={ filters }
|
filters={ filters }
|
||||||
advancedFilters={ advancedFilters }
|
advancedFilters={ advancedFilters }
|
||||||
|
|
|
@ -33,10 +33,10 @@ import { CurrencyContext } from 'lib/currency-context';
|
||||||
*/
|
*/
|
||||||
export class ReportSummary extends Component {
|
export class ReportSummary extends Component {
|
||||||
formatVal( val, type ) {
|
formatVal( val, type ) {
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
return type === 'currency'
|
return type === 'currency'
|
||||||
? formatCurrency( val )
|
? formatAmount( val )
|
||||||
: formatValue( getCurrency(), type, val );
|
: formatValue( getCurrencyConfig(), type, val );
|
||||||
}
|
}
|
||||||
|
|
||||||
getValues( key, type ) {
|
getValues( key, type ) {
|
||||||
|
@ -222,7 +222,7 @@ export default compose(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const fields = charts && charts.map( chart => chart.key );
|
const fields = charts && charts.map( ( chart ) => chart.key );
|
||||||
|
|
||||||
const { woocommerce_default_date_range: defaultDateRange } = select(
|
const { woocommerce_default_date_range: defaultDateRange } = select(
|
||||||
SETTINGS_STORE_NAME
|
SETTINGS_STORE_NAME
|
||||||
|
|
|
@ -71,9 +71,9 @@ class CategoriesReportTable extends Component {
|
||||||
const {
|
const {
|
||||||
render: renderCurrency,
|
render: renderCurrency,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
|
|
||||||
return map( categoryStats, ( categoryStat ) => {
|
return map( categoryStats, ( categoryStat ) => {
|
||||||
const {
|
const {
|
||||||
|
@ -138,8 +138,8 @@ class CategoriesReportTable extends Component {
|
||||||
net_revenue: netRevenue = 0,
|
net_revenue: netRevenue = 0,
|
||||||
orders_count: ordersCount = 0,
|
orders_count: ordersCount = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -161,7 +161,7 @@ class CategoriesReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'net sales', 'woocommerce-admin' ),
|
label: __( 'net sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( netRevenue ),
|
value: formatAmount( netRevenue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
|
|
@ -72,9 +72,9 @@ class CouponsReportTable extends Component {
|
||||||
const persistedQuery = getPersistedQuery( query );
|
const persistedQuery = getPersistedQuery( query );
|
||||||
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
||||||
const {
|
const {
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
|
|
||||||
return map( coupons, ( coupon ) => {
|
return map( coupons, ( coupon ) => {
|
||||||
|
@ -111,7 +111,11 @@ class CouponsReportTable extends Component {
|
||||||
} );
|
} );
|
||||||
const ordersLink = (
|
const ordersLink = (
|
||||||
<Link href={ ordersUrl } type="wc-admin">
|
<Link href={ ordersUrl } type="wc-admin">
|
||||||
{ formatValue( getCurrency(), 'number', ordersCount ) }
|
{ formatValue(
|
||||||
|
getCurrencyConfig(),
|
||||||
|
'number',
|
||||||
|
ordersCount
|
||||||
|
) }
|
||||||
</Link>
|
</Link>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -125,7 +129,7 @@ class CouponsReportTable extends Component {
|
||||||
value: ordersCount,
|
value: ordersCount,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( amount ),
|
display: formatAmount( amount ),
|
||||||
value: getCurrencyFormatDecimal( amount ),
|
value: getCurrencyFormatDecimal( amount ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -162,8 +166,8 @@ class CouponsReportTable extends Component {
|
||||||
orders_count: ordersCount = 0,
|
orders_count: ordersCount = 0,
|
||||||
amount = 0,
|
amount = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -185,7 +189,7 @@ class CouponsReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'amount discounted', 'woocommerce-admin' ),
|
label: __( 'amount discounted', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( amount ),
|
value: formatAmount( amount ),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -209,11 +213,7 @@ class CouponsReportTable extends Component {
|
||||||
getHeadersContent={ this.getHeadersContent }
|
getHeadersContent={ this.getHeadersContent }
|
||||||
getRowsContent={ this.getRowsContent }
|
getRowsContent={ this.getRowsContent }
|
||||||
getSummary={ this.getSummary }
|
getSummary={ this.getSummary }
|
||||||
summaryFields={ [
|
summaryFields={ [ 'coupons_count', 'orders_count', 'amount' ] }
|
||||||
'coupons_count',
|
|
||||||
'orders_count',
|
|
||||||
'amount',
|
|
||||||
] }
|
|
||||||
isRequesting={ isRequesting }
|
isRequesting={ isRequesting }
|
||||||
itemIdField="coupon_id"
|
itemIdField="coupon_id"
|
||||||
query={ query }
|
query={ query }
|
||||||
|
|
|
@ -115,9 +115,9 @@ class CustomersReportTable extends Component {
|
||||||
getRowsContent( customers ) {
|
getRowsContent( customers ) {
|
||||||
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
||||||
const {
|
const {
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
|
|
||||||
return customers.map( ( customer ) => {
|
return customers.map( ( customer ) => {
|
||||||
|
@ -193,18 +193,18 @@ class CustomersReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatValue(
|
display: formatValue(
|
||||||
getCurrency(),
|
getCurrencyConfig(),
|
||||||
'number',
|
'number',
|
||||||
ordersCount
|
ordersCount
|
||||||
),
|
),
|
||||||
value: ordersCount,
|
value: ordersCount,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( totalSpend ),
|
display: formatAmount( totalSpend ),
|
||||||
value: getCurrencyFormatDecimal( totalSpend ),
|
value: getCurrencyFormatDecimal( totalSpend ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( avgOrderValue ),
|
display: formatAmount( avgOrderValue ),
|
||||||
value: getCurrencyFormatDecimal( avgOrderValue ),
|
value: getCurrencyFormatDecimal( avgOrderValue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -234,8 +234,8 @@ class CustomersReportTable extends Component {
|
||||||
avg_total_spend: avgTotalSpend = 0,
|
avg_total_spend: avgTotalSpend = 0,
|
||||||
avg_avg_order_value: avgAvgOrderValue = 0,
|
avg_avg_order_value: avgAvgOrderValue = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -257,11 +257,11 @@ class CustomersReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'average lifetime spend', 'woocommerce-admin' ),
|
label: __( 'average lifetime spend', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( avgTotalSpend ),
|
value: formatAmount( avgTotalSpend ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'average order value', 'woocommerce-admin' ),
|
label: __( 'average order value', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( avgAvgOrderValue ),
|
value: formatAmount( avgAvgOrderValue ),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,7 @@ class CouponsReportTable extends Component {
|
||||||
const after = moment( dates.primary.after );
|
const after = moment( dates.primary.after );
|
||||||
const before = moment( dates.primary.before );
|
const before = moment( dates.primary.before );
|
||||||
const days = before.diff( after, 'days' ) + 1;
|
const days = before.diff( after, 'days' ) + 1;
|
||||||
const currency = this.context.getCurrency();
|
const currency = this.context.getCurrencyConfig();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|
|
@ -105,7 +105,7 @@ class OrdersReportTable extends Component {
|
||||||
const { query } = this.props;
|
const { query } = this.props;
|
||||||
const persistedQuery = getPersistedQuery( query );
|
const persistedQuery = getPersistedQuery( query );
|
||||||
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
||||||
const { render: renderCurrency, getCurrency } = this.context;
|
const { render: renderCurrency, getCurrencyConfig } = this.context;
|
||||||
|
|
||||||
return map( tableData, ( row ) => {
|
return map( tableData, ( row ) => {
|
||||||
const {
|
const {
|
||||||
|
@ -207,7 +207,7 @@ class OrdersReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatValue(
|
display: formatValue(
|
||||||
getCurrency(),
|
getCurrencyConfig(),
|
||||||
'number',
|
'number',
|
||||||
numItemsSold
|
numItemsSold
|
||||||
),
|
),
|
||||||
|
@ -242,8 +242,8 @@ class OrdersReportTable extends Component {
|
||||||
coupons_count: couponsCount = 0,
|
coupons_count: couponsCount = 0,
|
||||||
net_revenue: netRevenue = 0,
|
net_revenue: netRevenue = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -301,7 +301,7 @@ class OrdersReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'net sales', 'woocommerce-admin' ),
|
label: __( 'net sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( netRevenue ),
|
value: formatAmount( netRevenue ),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,9 +88,9 @@ class VariationsReportTable extends Component {
|
||||||
const { query } = this.props;
|
const { query } = this.props;
|
||||||
const persistedQuery = getPersistedQuery( query );
|
const persistedQuery = getPersistedQuery( query );
|
||||||
const {
|
const {
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
|
|
||||||
return map( data, ( row ) => {
|
return map( data, ( row ) => {
|
||||||
|
@ -134,11 +134,15 @@ class VariationsReportTable extends Component {
|
||||||
value: sku,
|
value: sku,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatValue( getCurrency(), 'number', itemsSold ),
|
display: formatValue(
|
||||||
|
getCurrencyConfig(),
|
||||||
|
'number',
|
||||||
|
itemsSold
|
||||||
|
),
|
||||||
value: itemsSold,
|
value: itemsSold,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( netRevenue ),
|
display: formatAmount( netRevenue ),
|
||||||
value: getCurrencyFormatDecimal( netRevenue ),
|
value: getCurrencyFormatDecimal( netRevenue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -186,8 +190,8 @@ class VariationsReportTable extends Component {
|
||||||
net_revenue: netRevenue = 0,
|
net_revenue: netRevenue = 0,
|
||||||
orders_count: ordersCount = 0,
|
orders_count: ordersCount = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -209,7 +213,7 @@ class VariationsReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'net sales', 'woocommerce-admin' ),
|
label: __( 'net sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( netRevenue ),
|
value: formatAmount( netRevenue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
|
|
@ -105,9 +105,9 @@ class ProductsReportTable extends Component {
|
||||||
const {
|
const {
|
||||||
render: renderCurrency,
|
render: renderCurrency,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
|
|
||||||
return map( data, ( row ) => {
|
return map( data, ( row ) => {
|
||||||
const {
|
const {
|
||||||
|
@ -281,8 +281,8 @@ class ProductsReportTable extends Component {
|
||||||
net_revenue: netRevenue = 0,
|
net_revenue: netRevenue = 0,
|
||||||
orders_count: ordersCount = 0,
|
orders_count: ordersCount = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -304,7 +304,7 @@ class ProductsReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'net sales', 'woocommerce-admin' ),
|
label: __( 'net sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( netRevenue ),
|
value: formatAmount( netRevenue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
|
|
@ -110,10 +110,10 @@ class RevenueReportTable extends Component {
|
||||||
getRowsContent( data = [] ) {
|
getRowsContent( data = [] ) {
|
||||||
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
const dateFormat = getSetting( 'dateFormat', defaultTableDateFormat );
|
||||||
const {
|
const {
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
render: renderCurrency,
|
render: renderCurrency,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
|
|
||||||
return data.map( ( row ) => {
|
return data.map( ( row ) => {
|
||||||
|
@ -138,7 +138,11 @@ class RevenueReportTable extends Component {
|
||||||
}
|
}
|
||||||
type="wp-admin"
|
type="wp-admin"
|
||||||
>
|
>
|
||||||
{ formatValue( getCurrency(), 'number', ordersCount ) }
|
{ formatValue(
|
||||||
|
getCurrencyConfig(),
|
||||||
|
'number',
|
||||||
|
ordersCount
|
||||||
|
) }
|
||||||
</Link>
|
</Link>
|
||||||
);
|
);
|
||||||
return [
|
return [
|
||||||
|
@ -160,11 +164,11 @@ class RevenueReportTable extends Component {
|
||||||
value: getCurrencyFormatDecimal( grossSales ),
|
value: getCurrencyFormatDecimal( grossSales ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( refunds ),
|
display: formatAmount( refunds ),
|
||||||
value: getCurrencyFormatDecimal( refunds ),
|
value: getCurrencyFormatDecimal( refunds ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatCurrency( coupons ),
|
display: formatAmount( coupons ),
|
||||||
value: getCurrencyFormatDecimal( coupons ),
|
value: getCurrencyFormatDecimal( coupons ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -198,8 +202,8 @@ class RevenueReportTable extends Component {
|
||||||
shipping = 0,
|
shipping = 0,
|
||||||
net_revenue: netRevenue = 0,
|
net_revenue: netRevenue = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n( 'day', 'days', totalResults, 'woocommerce-admin' ),
|
label: _n( 'day', 'days', totalResults, 'woocommerce-admin' ),
|
||||||
|
@ -216,31 +220,31 @@ class RevenueReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'gross sales', 'woocommerce-admin' ),
|
label: __( 'gross sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( grossSales ),
|
value: formatAmount( grossSales ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'returns', 'woocommerce-admin' ),
|
label: __( 'returns', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( refunds ),
|
value: formatAmount( refunds ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'coupons', 'woocommerce-admin' ),
|
label: __( 'coupons', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( coupons ),
|
value: formatAmount( coupons ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'net sales', 'woocommerce-admin' ),
|
label: __( 'net sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( netRevenue ),
|
value: formatAmount( netRevenue ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'taxes', 'woocommerce-admin' ),
|
label: __( 'taxes', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( taxes ),
|
value: formatAmount( taxes ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'shipping', 'woocommerce-admin' ),
|
label: __( 'shipping', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( shipping ),
|
value: formatAmount( shipping ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'total sales', 'woocommerce-admin' ),
|
label: __( 'total sales', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( totalSales ),
|
value: formatAmount( totalSales ),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,7 +128,7 @@ class StockReportTable extends Component {
|
||||||
{
|
{
|
||||||
display: manageStock
|
display: manageStock
|
||||||
? formatValue(
|
? formatValue(
|
||||||
this.context.getCurrency(),
|
this.context.getCurrencyConfig(),
|
||||||
'number',
|
'number',
|
||||||
stockQuantity
|
stockQuantity
|
||||||
)
|
)
|
||||||
|
@ -147,7 +147,7 @@ class StockReportTable extends Component {
|
||||||
instock = 0,
|
instock = 0,
|
||||||
onbackorder = 0,
|
onbackorder = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const currency = this.context.getCurrency();
|
const currency = this.context.getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
|
|
@ -73,7 +73,7 @@ class TaxesReportTable extends Component {
|
||||||
const {
|
const {
|
||||||
render: renderCurrency,
|
render: renderCurrency,
|
||||||
formatDecimal: getCurrencyFormatDecimal,
|
formatDecimal: getCurrencyFormatDecimal,
|
||||||
getCurrency,
|
getCurrencyConfig,
|
||||||
} = this.context;
|
} = this.context;
|
||||||
|
|
||||||
return map( taxes, ( tax ) => {
|
return map( taxes, ( tax ) => {
|
||||||
|
@ -126,7 +126,7 @@ class TaxesReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
display: formatValue(
|
display: formatValue(
|
||||||
getCurrency(),
|
getCurrencyConfig(),
|
||||||
'number',
|
'number',
|
||||||
ordersCount
|
ordersCount
|
||||||
),
|
),
|
||||||
|
@ -144,8 +144,8 @@ class TaxesReportTable extends Component {
|
||||||
shipping_tax: shippingTax = 0,
|
shipping_tax: shippingTax = 0,
|
||||||
orders_count: ordersCount = 0,
|
orders_count: ordersCount = 0,
|
||||||
} = totals;
|
} = totals;
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
@ -158,15 +158,15 @@ class TaxesReportTable extends Component {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'total tax', 'woocommerce-admin' ),
|
label: __( 'total tax', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( totalTax ),
|
value: formatAmount( totalTax ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'order tax', 'woocommerce-admin' ),
|
label: __( 'order tax', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( orderTax ),
|
value: formatAmount( orderTax ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: __( 'shipping tax', 'woocommerce-admin' ),
|
label: __( 'shipping tax', 'woocommerce-admin' ),
|
||||||
value: formatCurrency( shippingTax ),
|
value: formatAmount( shippingTax ),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: _n(
|
label: _n(
|
||||||
|
|
|
@ -134,8 +134,8 @@ class StorePerformance extends Component {
|
||||||
compare === 'previous_period'
|
compare === 'previous_period'
|
||||||
? __( 'Previous Period:', 'woocommerce-admin' )
|
? __( 'Previous Period:', 'woocommerce-admin' )
|
||||||
: __( 'Previous Year:', 'woocommerce-admin' );
|
: __( 'Previous Year:', 'woocommerce-admin' );
|
||||||
const { formatCurrency, getCurrency } = this.context;
|
const { formatAmount, getCurrencyConfig } = this.context;
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
return (
|
return (
|
||||||
<SummaryList>
|
<SummaryList>
|
||||||
{ () =>
|
{ () =>
|
||||||
|
@ -151,7 +151,7 @@ class StorePerformance extends Component {
|
||||||
primaryData,
|
primaryData,
|
||||||
secondaryData,
|
secondaryData,
|
||||||
currency,
|
currency,
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
persistedQuery,
|
persistedQuery,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ export const getIndicatorValues = ( {
|
||||||
primaryData,
|
primaryData,
|
||||||
secondaryData,
|
secondaryData,
|
||||||
currency,
|
currency,
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
persistedQuery,
|
persistedQuery,
|
||||||
} ) => {
|
} ) => {
|
||||||
const primaryItem = find(
|
const primaryItem = find(
|
||||||
|
@ -59,10 +59,10 @@ export const getIndicatorValues = ( {
|
||||||
|
|
||||||
const delta = calculateDelta( primaryItem.value, secondaryItem.value );
|
const delta = calculateDelta( primaryItem.value, secondaryItem.value );
|
||||||
const primaryValue = isCurrency
|
const primaryValue = isCurrency
|
||||||
? formatCurrency( primaryItem.value )
|
? formatAmount( primaryItem.value )
|
||||||
: formatValue( currency, primaryItem.format, primaryItem.value );
|
: formatValue( currency, primaryItem.format, primaryItem.value );
|
||||||
const secondaryValue = isCurrency
|
const secondaryValue = isCurrency
|
||||||
? formatCurrency( secondaryItem.value )
|
? formatAmount( secondaryItem.value )
|
||||||
: formatValue( currency, secondaryItem.format, secondaryItem.value );
|
: formatValue( currency, secondaryItem.format, secondaryItem.value );
|
||||||
return {
|
return {
|
||||||
primaryValue,
|
primaryValue,
|
||||||
|
|
|
@ -195,7 +195,7 @@ class OrdersPanel extends Component {
|
||||||
productsCount
|
productsCount
|
||||||
) }
|
) }
|
||||||
</span>
|
</span>
|
||||||
<span>{ Currency.formatCurrency( total ) }</span>
|
<span>{ Currency.formatAmount( total ) }</span>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
actions={
|
actions={
|
||||||
|
|
|
@ -35,12 +35,12 @@ export const StatsList = ( {
|
||||||
secondaryError,
|
secondaryError,
|
||||||
query,
|
query,
|
||||||
} ) => {
|
} ) => {
|
||||||
const { formatCurrency, getCurrency } = useContext( CurrencyContext );
|
const { formatAmount, getCurrencyConfig } = useContext( CurrencyContext );
|
||||||
if ( primaryError || secondaryError ) {
|
if ( primaryError || secondaryError ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const persistedQuery = getPersistedQuery( query );
|
const persistedQuery = getPersistedQuery( query );
|
||||||
const currency = getCurrency();
|
const currency = getCurrencyConfig();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul
|
<ul
|
||||||
|
@ -68,7 +68,7 @@ export const StatsList = ( {
|
||||||
primaryData,
|
primaryData,
|
||||||
secondaryData,
|
secondaryData,
|
||||||
currency,
|
currency,
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
persistedQuery,
|
persistedQuery,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
|
@ -7,23 +7,23 @@ import { applyFilters } from '@wordpress/hooks';
|
||||||
/**
|
/**
|
||||||
* WooCommerce dependencies
|
* WooCommerce dependencies
|
||||||
*/
|
*/
|
||||||
import Currency from '@woocommerce/currency';
|
import CurrencyFactory from '@woocommerce/currency';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
*/
|
*/
|
||||||
import { CURRENCY } from '@woocommerce/wc-admin-settings';
|
import { CURRENCY } from '@woocommerce/wc-admin-settings';
|
||||||
|
|
||||||
const appCurrency = Currency( CURRENCY );
|
const appCurrency = CurrencyFactory( CURRENCY );
|
||||||
|
|
||||||
export const getFilteredCurrencyInstance = ( query ) => {
|
export const getFilteredCurrencyInstance = ( query ) => {
|
||||||
const config = appCurrency.getCurrency();
|
const config = appCurrency.getCurrencyConfig();
|
||||||
const filteredConfig = applyFilters(
|
const filteredConfig = applyFilters(
|
||||||
'woocommerce_admin_report_currency',
|
'woocommerce_admin_report_currency',
|
||||||
config,
|
config,
|
||||||
query
|
query
|
||||||
);
|
);
|
||||||
return Currency( filteredConfig );
|
return CurrencyFactory( filteredConfig );
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CurrencyContext = createContext(
|
export const CurrencyContext = createContext(
|
||||||
|
|
|
@ -91,12 +91,12 @@ class BusinessDetails extends Component {
|
||||||
selling_venues: sellingVenues,
|
selling_venues: sellingVenues,
|
||||||
} = values;
|
} = values;
|
||||||
const businessExtensions = this.getBusinessExtensions( values );
|
const businessExtensions = this.getBusinessExtensions( values );
|
||||||
const { getCurrency } = this.context;
|
const { getCurrencyConfig } = this.context;
|
||||||
|
|
||||||
recordEvent( 'storeprofiler_store_business_details_continue', {
|
recordEvent( 'storeprofiler_store_business_details_continue', {
|
||||||
product_number: productCount,
|
product_number: productCount,
|
||||||
already_selling: sellingVenues,
|
already_selling: sellingVenues,
|
||||||
currency: getCurrency().code,
|
currency: getCurrencyConfig().code,
|
||||||
revenue,
|
revenue,
|
||||||
used_platform: otherPlatform,
|
used_platform: otherPlatform,
|
||||||
used_platform_name: otherPlatformName,
|
used_platform_name: otherPlatformName,
|
||||||
|
@ -255,8 +255,8 @@ class BusinessDetails extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
numberFormat( value ) {
|
numberFormat( value ) {
|
||||||
const { getCurrency } = this.context;
|
const { getCurrencyConfig } = this.context;
|
||||||
return formatValue( getCurrency(), 'number', value );
|
return formatValue( getCurrencyConfig(), 'number', value );
|
||||||
}
|
}
|
||||||
|
|
||||||
getNumberRangeString( min, max = false, format = this.numberFormat ) {
|
getNumberRangeString( min, max = false, format = this.numberFormat ) {
|
||||||
|
@ -398,7 +398,7 @@ class BusinessDetails extends Component {
|
||||||
isInstallingActivating,
|
isInstallingActivating,
|
||||||
hasInstallActivateError,
|
hasInstallActivateError,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const { formatCurrency } = this.context;
|
const { formatAmount } = this.context;
|
||||||
const productCountOptions = [
|
const productCountOptions = [
|
||||||
{
|
{
|
||||||
key: '0',
|
key: '0',
|
||||||
|
@ -431,7 +431,7 @@ class BusinessDetails extends Component {
|
||||||
label: sprintf(
|
label: sprintf(
|
||||||
/* translators: %s: $0 revenue amount */
|
/* translators: %s: $0 revenue amount */
|
||||||
__( "%s (I'm just getting started)", 'woocommerce-admin' ),
|
__( "%s (I'm just getting started)", 'woocommerce-admin' ),
|
||||||
formatCurrency( 0 )
|
formatAmount( 0 )
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -439,7 +439,7 @@ class BusinessDetails extends Component {
|
||||||
label: sprintf(
|
label: sprintf(
|
||||||
/* translators: %s: A given revenue amount, e.g., $2500 */
|
/* translators: %s: A given revenue amount, e.g., $2500 */
|
||||||
__( 'Up to %s', 'woocommerce-admin' ),
|
__( 'Up to %s', 'woocommerce-admin' ),
|
||||||
formatCurrency( this.convertCurrency( 2500 ) )
|
formatAmount( this.convertCurrency( 2500 ) )
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -447,7 +447,7 @@ class BusinessDetails extends Component {
|
||||||
label: this.getNumberRangeString(
|
label: this.getNumberRangeString(
|
||||||
this.convertCurrency( 2500 ),
|
this.convertCurrency( 2500 ),
|
||||||
this.convertCurrency( 10000 ),
|
this.convertCurrency( 10000 ),
|
||||||
formatCurrency
|
formatAmount
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -455,7 +455,7 @@ class BusinessDetails extends Component {
|
||||||
label: this.getNumberRangeString(
|
label: this.getNumberRangeString(
|
||||||
this.convertCurrency( 10000 ),
|
this.convertCurrency( 10000 ),
|
||||||
this.convertCurrency( 50000 ),
|
this.convertCurrency( 50000 ),
|
||||||
formatCurrency
|
formatAmount
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -463,7 +463,7 @@ class BusinessDetails extends Component {
|
||||||
label: this.getNumberRangeString(
|
label: this.getNumberRangeString(
|
||||||
this.convertCurrency( 50000 ),
|
this.convertCurrency( 50000 ),
|
||||||
this.convertCurrency( 250000 ),
|
this.convertCurrency( 250000 ),
|
||||||
formatCurrency
|
formatAmount
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -471,7 +471,7 @@ class BusinessDetails extends Component {
|
||||||
label: sprintf(
|
label: sprintf(
|
||||||
/* translators: %s: A given revenue amount, e.g., $250000 */
|
/* translators: %s: A given revenue amount, e.g., $250000 */
|
||||||
__( 'More than %s', 'woocommerce-admin' ),
|
__( 'More than %s', 'woocommerce-admin' ),
|
||||||
formatCurrency( this.convertCurrency( 250000 ) )
|
formatAmount( this.convertCurrency( 250000 ) )
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -131,7 +131,7 @@ class ShippingRates extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderInputPrefix() {
|
renderInputPrefix() {
|
||||||
const { symbolPosition, symbol } = this.context.getCurrency();
|
const { symbolPosition, symbol } = this.context.getCurrencyConfig();
|
||||||
if ( symbolPosition.indexOf( 'right' ) === 0 ) {
|
if ( symbolPosition.indexOf( 'right' ) === 0 ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ class ShippingRates extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
renderInputSuffix( rate ) {
|
renderInputSuffix( rate ) {
|
||||||
const { symbolPosition, symbol } = this.context.getCurrency();
|
const { symbolPosition, symbol } = this.context.getCurrencyConfig();
|
||||||
if ( symbolPosition.indexOf( 'right' ) === 0 ) {
|
if ( symbolPosition.indexOf( 'right' ) === 0 ) {
|
||||||
return (
|
return (
|
||||||
<span className="woocommerce-shipping-rate__control-suffix">
|
<span className="woocommerce-shipping-rate__control-suffix">
|
||||||
|
|
|
@ -387,13 +387,13 @@ export function getReportChartData( options ) {
|
||||||
* Returns a formatting function or string to be used by d3-format
|
* Returns a formatting function or string to be used by d3-format
|
||||||
*
|
*
|
||||||
* @param {string} type Type of number, 'currency', 'number', 'percent', 'average'
|
* @param {string} type Type of number, 'currency', 'number', 'percent', 'average'
|
||||||
* @param {Function} formatCurrency format currency function
|
* @param {Function} formatAmount format currency function
|
||||||
* @return {string|Function} returns a number format based on the type or an overriding formatting function
|
* @return {string|Function} returns a number format based on the type or an overriding formatting function
|
||||||
*/
|
*/
|
||||||
export function getTooltipValueFormat( type, formatCurrency ) {
|
export function getTooltipValueFormat( type, formatAmount ) {
|
||||||
switch ( type ) {
|
switch ( type ) {
|
||||||
case 'currency':
|
case 'currency':
|
||||||
return formatCurrency;
|
return formatAmount;
|
||||||
case 'percent':
|
case 'percent':
|
||||||
return '.0%';
|
return '.0%';
|
||||||
case 'number':
|
case 'number':
|
||||||
|
|
|
@ -11,7 +11,7 @@ import { sprintf, __, _x } from '@wordpress/i18n';
|
||||||
/**
|
/**
|
||||||
* WooCommerce dependencies
|
* WooCommerce dependencies
|
||||||
*/
|
*/
|
||||||
import Currency from '@woocommerce/currency';
|
import CurrencyFactory from '@woocommerce/currency';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -42,9 +42,9 @@ class NumberFilter extends Component {
|
||||||
const inputType = get( config, [ 'input', 'type' ], 'number' );
|
const inputType = get( config, [ 'input', 'type' ], 'number' );
|
||||||
|
|
||||||
if ( inputType === 'currency' ) {
|
if ( inputType === 'currency' ) {
|
||||||
const { formatCurrency } = Currency( currency );
|
const { formatAmount } = CurrencyFactory( currency );
|
||||||
rangeStart = formatCurrency( rangeStart );
|
rangeStart = formatAmount( rangeStart );
|
||||||
rangeEnd = formatCurrency( rangeEnd );
|
rangeEnd = formatAmount( rangeEnd );
|
||||||
}
|
}
|
||||||
|
|
||||||
let filterStr = rangeStart;
|
let filterStr = rangeStart;
|
||||||
|
|
|
@ -11,7 +11,7 @@ import PropTypes from 'prop-types';
|
||||||
*/
|
*/
|
||||||
import { updateQueryString } from '@woocommerce/navigation';
|
import { updateQueryString } from '@woocommerce/navigation';
|
||||||
import { getDateParamsFromQuery, getCurrentDates } from '@woocommerce/date';
|
import { getDateParamsFromQuery, getCurrentDates } from '@woocommerce/date';
|
||||||
import Currency from '@woocommerce/currency';
|
import CurrencyFactory from '@woocommerce/currency';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal dependencies
|
* Internal dependencies
|
||||||
|
@ -231,7 +231,7 @@ ReportFilters.defaultProps = {
|
||||||
query: {},
|
query: {},
|
||||||
showDatePicker: true,
|
showDatePicker: true,
|
||||||
onDateSelect: () => {},
|
onDateSelect: () => {},
|
||||||
currency: Currency().getCurrency(),
|
currency: CurrencyFactory().getCurrencyConfig(),
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ReportFilters;
|
export default ReportFilters;
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
## Breaking changes
|
## Breaking changes
|
||||||
- Currency is now a factory function instead of a class.
|
- Currency is now a factory function instead of a class.
|
||||||
|
|
||||||
- Add getCurrency method to retrieve currency config.
|
- Add getCurrencyConfig method to retrieve currency config.
|
||||||
|
|
||||||
# 2.0.0
|
# 2.0.0
|
||||||
|
|
||||||
|
|
|
@ -15,19 +15,19 @@ _This package assumes that your code will run in an **ES2015+** environment. If
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```JS
|
```JS
|
||||||
import Currency from '@woocommerce/currency';
|
import CurrencyFactory from '@woocommerce/currency';
|
||||||
|
|
||||||
const storeCurrency = Currency(); // pass store settings into constructor.
|
const storeCurrency = CurrencyFactory(); // pass store settings into constructor.
|
||||||
|
|
||||||
// Formats money with a given currency symbol. Uses site's currency settings for formatting,
|
// Formats money with a given currency symbol. Uses site's currency settings for formatting,
|
||||||
// from the settings api. Defaults to symbol=`$`, precision=2, decimalSeparator=`.`, thousandSeparator=`,`
|
// from the settings api. Defaults to symbol=`$`, precision=2, decimalSeparator=`.`, thousandSeparator=`,`
|
||||||
const total = storeCurrency.formatCurrency( 20.923 ); // '$20.92'
|
const total = storeCurrency.formatAmount( 20.923 ); // '$20.92'
|
||||||
|
|
||||||
// Get the rounded decimal value of a number at the precision used for the current currency,
|
// Get the rounded decimal value of a number at the precision used for the current currency,
|
||||||
// from the settings api. Defaults to 2.
|
// from the settings api. Defaults to 2.
|
||||||
const total = storeCurrency.formatDecimal( '6.2892' ); // 6.29 https://google.com/?q=test
|
const total = storeCurrency.formatDecimal( '6.2892' ); // 6.29 https://google.com/?q=test
|
||||||
|
|
||||||
// Get the string representation of a floating point number to the precision used by the current
|
// Get the string representation of a floating point number to the precision used by the current
|
||||||
// currency. This is different from `formatCurrency` by not returning the currency symbol.
|
// currency. This is different from `formatAmount` by not returning the currency symbol.
|
||||||
const total = storeCurrency.formatDecimalString( 1088.478 ); // '1088.48'
|
const total = storeCurrency.formatDecimalString( 1088.478 ); // '1088.48'
|
||||||
```
|
```
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { sprintf } from '@wordpress/i18n';
|
||||||
*/
|
*/
|
||||||
import { numberFormat } from '@woocommerce/number';
|
import { numberFormat } from '@woocommerce/number';
|
||||||
|
|
||||||
const Currency = ( currencySetting ) => {
|
const CurrencyFactory = ( currencySetting ) => {
|
||||||
let currency;
|
let currency;
|
||||||
|
|
||||||
setCurrency( currencySetting );
|
setCurrency( currencySetting );
|
||||||
|
@ -39,7 +39,7 @@ const Currency = ( currencySetting ) => {
|
||||||
* @param {number|string} number number to format
|
* @param {number|string} number number to format
|
||||||
* @return {?string} A formatted string.
|
* @return {?string} A formatted string.
|
||||||
*/
|
*/
|
||||||
function formatCurrency( number ) {
|
function formatAmount( number ) {
|
||||||
const formattedNumber = numberFormat( currency, number );
|
const formattedNumber = numberFormat( currency, number );
|
||||||
|
|
||||||
if ( formattedNumber === '' ) {
|
if ( formattedNumber === '' ) {
|
||||||
|
@ -78,11 +78,11 @@ const Currency = ( currencySetting ) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCurrency: () => {
|
getCurrencyConfig: () => {
|
||||||
return { ...currency };
|
return { ...currency };
|
||||||
},
|
},
|
||||||
setCurrency,
|
setCurrency,
|
||||||
formatCurrency,
|
formatAmount,
|
||||||
getPriceFormat,
|
getPriceFormat,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,7 +108,7 @@ const Currency = ( currencySetting ) => {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the string representation of a floating point number to the precision used by the current currency.
|
* Get the string representation of a floating point number to the precision used by the current currency.
|
||||||
* This is different from `formatCurrency` by not returning the currency symbol.
|
* This is different from `formatAmount` by not returning the currency symbol.
|
||||||
*
|
*
|
||||||
* @param {number|string} number A floating point number (or integer), or string that converts to a number
|
* @param {number|string} number A floating point number (or integer), or string that converts to a number
|
||||||
* @return {string} The original number rounded to a decimal point
|
* @return {string} The original number rounded to a decimal point
|
||||||
|
@ -137,16 +137,16 @@ const Currency = ( currencySetting ) => {
|
||||||
if ( number < 0 ) {
|
if ( number < 0 ) {
|
||||||
return (
|
return (
|
||||||
<span className="is-negative">
|
<span className="is-negative">
|
||||||
{ formatCurrency( number ) }
|
{ formatAmount( number ) }
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return formatCurrency( number );
|
return formatAmount( number );
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Currency;
|
export default CurrencyFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns currency data by country/region. Contains code, symbol, position, thousands separator, decimal separator, and precision.
|
* Returns currency data by country/region. Contains code, symbol, position, thousands separator, decimal separator, and precision.
|
||||||
|
|
|
@ -3,11 +3,11 @@
|
||||||
*/
|
*/
|
||||||
import Currency from '../src';
|
import Currency from '../src';
|
||||||
|
|
||||||
describe( 'formatCurrency', () => {
|
describe( 'formatAmount', () => {
|
||||||
it( 'should use defaults (USD) when currency not passed in', () => {
|
it( 'should use defaults (USD) when currency not passed in', () => {
|
||||||
const currency = Currency();
|
const currency = Currency();
|
||||||
expect( currency.formatCurrency( 9.99 ) ).toBe( '$9.99' );
|
expect( currency.formatAmount( 9.99 ) ).toBe( '$9.99' );
|
||||||
expect( currency.formatCurrency( 30 ) ).toBe( '$30.00' );
|
expect( currency.formatAmount( 30 ) ).toBe( '$30.00' );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
it( 'should uses store currency settings, not locale-based', () => {
|
it( 'should uses store currency settings, not locale-based', () => {
|
||||||
|
@ -19,16 +19,16 @@ describe( 'formatCurrency', () => {
|
||||||
thousandSeparator: '.',
|
thousandSeparator: '.',
|
||||||
decimalSeparator: ',',
|
decimalSeparator: ',',
|
||||||
} );
|
} );
|
||||||
expect( currency.formatCurrency( 9.49258 ) ).toBe( '9,493¥' );
|
expect( currency.formatAmount( 9.49258 ) ).toBe( '9,493¥' );
|
||||||
expect( currency.formatCurrency( 3000 ) ).toBe( '3.000,000¥' );
|
expect( currency.formatAmount( 3000 ) ).toBe( '3.000,000¥' );
|
||||||
expect( currency.formatCurrency( 3.0002 ) ).toBe( '3,000¥' );
|
expect( currency.formatAmount( 3.0002 ) ).toBe( '3,000¥' );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
it( "should return empty string when given an input that isn't a number", () => {
|
it( "should return empty string when given an input that isn't a number", () => {
|
||||||
const currency = Currency();
|
const currency = Currency();
|
||||||
expect( currency.formatCurrency( 'abc' ) ).toBe( '' );
|
expect( currency.formatAmount( 'abc' ) ).toBe( '' );
|
||||||
expect( currency.formatCurrency( false ) ).toBe( '' );
|
expect( currency.formatAmount( false ) ).toBe( '' );
|
||||||
expect( currency.formatCurrency( null ) ).toBe( '' );
|
expect( currency.formatAmount( null ) ).toBe( '' );
|
||||||
} );
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,7 @@ class Init {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the currency symbol (in addition to currency code) to each Order
|
* Add the currency symbol (in addition to currency code) to each Order
|
||||||
* object in REST API responses. For use in formatCurrency().
|
* object in REST API responses. For use in formatAmount().
|
||||||
*
|
*
|
||||||
* @param {WP_REST_Response} $response REST response object.
|
* @param {WP_REST_Response} $response REST response object.
|
||||||
* @returns {WP_REST_Response}
|
* @returns {WP_REST_Response}
|
||||||
|
|
Loading…
Reference in New Issue