Use single price value instead of range in product list (#2386)
* Render single value when amount of money is matching * Use Intl.NumberFormat in money formatting * Extract messages
This commit is contained in:
parent
da8e4e5a9c
commit
7387019288
3 changed files with 56 additions and 45 deletions
|
@ -7863,10 +7863,6 @@
|
||||||
"zSOvI0": {
|
"zSOvI0": {
|
||||||
"string": "Filters"
|
"string": "Filters"
|
||||||
},
|
},
|
||||||
"zTdwWM": {
|
|
||||||
"context": "money",
|
|
||||||
"string": "{fromMoney} - {toMoney}"
|
|
||||||
},
|
|
||||||
"zWM89r": {
|
"zWM89r": {
|
||||||
"context": "numeric attribute units of",
|
"context": "numeric attribute units of",
|
||||||
"string": "Units of"
|
"string": "Units of"
|
||||||
|
|
|
@ -18,12 +18,32 @@ export function subtractMoney(init: IMoney, ...args: IMoney[]): IMoney {
|
||||||
|
|
||||||
export const formatMoney = (money: IMoney, locale: string) => {
|
export const formatMoney = (money: IMoney, locale: string) => {
|
||||||
try {
|
try {
|
||||||
const formattedMoney = money.amount.toLocaleString(locale, {
|
const formattedMoney = Intl.NumberFormat(locale, {
|
||||||
currency: money.currency,
|
|
||||||
style: "currency",
|
style: "currency",
|
||||||
});
|
currency: money.currency,
|
||||||
|
}).format(money.amount);
|
||||||
return formattedMoney;
|
return formattedMoney;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return `${money.amount} ${money.currency}`;
|
return `${money.amount} ${money.currency}`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const formatMoneyRange = (
|
||||||
|
moneyFrom: IMoney,
|
||||||
|
moneyTo: IMoney,
|
||||||
|
locale: string,
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const formattedMoneyRange = (Intl.NumberFormat(locale, {
|
||||||
|
style: "currency",
|
||||||
|
currency: moneyFrom.currency,
|
||||||
|
}) as any).formatRange(moneyFrom.amount, moneyTo.amount);
|
||||||
|
// TODO: remove casting from formatRange when typescript
|
||||||
|
// is updated to 4.7 or higher
|
||||||
|
return formattedMoneyRange;
|
||||||
|
} catch (error) {
|
||||||
|
const formattedMoneyFrom = formatMoney(moneyFrom, locale);
|
||||||
|
const formattedMoneyTo = formatMoney(moneyTo, locale);
|
||||||
|
return `${formattedMoneyFrom} - ${formattedMoneyTo}`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ import React from "react";
|
||||||
import { useIntl } from "react-intl";
|
import { useIntl } from "react-intl";
|
||||||
|
|
||||||
import { LocaleConsumer } from "../Locale";
|
import { LocaleConsumer } from "../Locale";
|
||||||
import { formatMoney, IMoney } from "../Money";
|
import { formatMoney, formatMoneyRange, IMoney } from "../Money";
|
||||||
|
|
||||||
export interface MoneyRangeProps {
|
export interface MoneyRangeProps {
|
||||||
from?: IMoney;
|
from?: IMoney;
|
||||||
|
@ -14,43 +14,38 @@ export const MoneyRange: React.FC<MoneyRangeProps> = ({ from, to }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LocaleConsumer>
|
<LocaleConsumer>
|
||||||
{({ locale }) =>
|
{({ locale }) => {
|
||||||
from && to
|
if (from && to) {
|
||||||
? intl.formatMessage(
|
return from.amount === to.amount
|
||||||
{
|
? formatMoney(from, locale)
|
||||||
id: "zTdwWM",
|
: formatMoneyRange(from, to, locale);
|
||||||
defaultMessage: "{fromMoney} - {toMoney}",
|
}
|
||||||
description: "money",
|
if (from && !to) {
|
||||||
},
|
return intl.formatMessage(
|
||||||
{
|
{
|
||||||
fromMoney: formatMoney(from, locale),
|
id: "lW5uJO",
|
||||||
toMoney: formatMoney(to, locale),
|
defaultMessage: "from {money}",
|
||||||
},
|
description: "money",
|
||||||
)
|
},
|
||||||
: from && !to
|
{
|
||||||
? intl.formatMessage(
|
money: formatMoney(from, locale),
|
||||||
{
|
},
|
||||||
id: "lW5uJO",
|
);
|
||||||
defaultMessage: "from {money}",
|
}
|
||||||
description: "money",
|
if (!from && to) {
|
||||||
},
|
return intl.formatMessage(
|
||||||
{
|
{
|
||||||
money: formatMoney(from, locale),
|
id: "hptDxW",
|
||||||
},
|
defaultMessage: "to {money}",
|
||||||
)
|
description: "money",
|
||||||
: !from && to
|
},
|
||||||
? intl.formatMessage(
|
{
|
||||||
{
|
money: formatMoney(to, locale),
|
||||||
id: "hptDxW",
|
},
|
||||||
defaultMessage: "to {money}",
|
);
|
||||||
description: "money",
|
}
|
||||||
},
|
return "-";
|
||||||
{
|
}}
|
||||||
money: formatMoney(to, locale),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
: "-"
|
|
||||||
}
|
|
||||||
</LocaleConsumer>
|
</LocaleConsumer>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue