Skip to content

Commit

Permalink
Use single price value instead of range in product list (saleor#2386)
Browse files Browse the repository at this point in the history
* Render single value when amount of money is matching

* Use Intl.NumberFormat in money formatting

* Extract messages
  • Loading branch information
Droniu committed Nov 2, 2022
1 parent da8e4e5 commit 7387019
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 45 deletions.
4 changes: 0 additions & 4 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7863,10 +7863,6 @@
"zSOvI0": {
"string": "Filters"
},
"zTdwWM": {
"context": "money",
"string": "{fromMoney} - {toMoney}"
},
"zWM89r": {
"context": "numeric attribute units of",
"string": "Units of"
Expand Down
26 changes: 23 additions & 3 deletions src/components/Money/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,32 @@ export function subtractMoney(init: IMoney, ...args: IMoney[]): IMoney {

export const formatMoney = (money: IMoney, locale: string) => {
try {
const formattedMoney = money.amount.toLocaleString(locale, {
currency: money.currency,
const formattedMoney = Intl.NumberFormat(locale, {
style: "currency",
});
currency: money.currency,
}).format(money.amount);
return formattedMoney;
} catch (error) {
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}`;
}
};
71 changes: 33 additions & 38 deletions src/components/MoneyRange/MoneyRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { useIntl } from "react-intl";

import { LocaleConsumer } from "../Locale";
import { formatMoney, IMoney } from "../Money";
import { formatMoney, formatMoneyRange, IMoney } from "../Money";

export interface MoneyRangeProps {
from?: IMoney;
Expand All @@ -14,43 +14,38 @@ export const MoneyRange: React.FC<MoneyRangeProps> = ({ from, to }) => {

return (
<LocaleConsumer>
{({ locale }) =>
from && to
? intl.formatMessage(
{
id: "zTdwWM",
defaultMessage: "{fromMoney} - {toMoney}",
description: "money",
},
{
fromMoney: formatMoney(from, locale),
toMoney: formatMoney(to, locale),
},
)
: from && !to
? intl.formatMessage(
{
id: "lW5uJO",
defaultMessage: "from {money}",
description: "money",
},
{
money: formatMoney(from, locale),
},
)
: !from && to
? intl.formatMessage(
{
id: "hptDxW",
defaultMessage: "to {money}",
description: "money",
},
{
money: formatMoney(to, locale),
},
)
: "-"
}
{({ locale }) => {
if (from && to) {
return from.amount === to.amount
? formatMoney(from, locale)
: formatMoneyRange(from, to, locale);
}
if (from && !to) {
return intl.formatMessage(
{
id: "lW5uJO",
defaultMessage: "from {money}",
description: "money",
},
{
money: formatMoney(from, locale),
},
);
}
if (!from && to) {
return intl.formatMessage(
{
id: "hptDxW",
defaultMessage: "to {money}",
description: "money",
},
{
money: formatMoney(to, locale),
},
);
}
return "-";
}}
</LocaleConsumer>
);
};
Expand Down

0 comments on commit 7387019

Please sign in to comment.