Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format currency input with commas in household input #941

Merged
merged 7 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/api/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ export function cardinal(number) {
return number + (suffixes[(rem - 20) % 10] || suffixes[rem] || suffixes[0]);
}

export function currencyString(number, metadata, maximumFractionDigits) {
return number.toLocaleString("en-US", {
style: "currency",
currency: metadata.countryId === "uk" ? "GBP" : "USD",
maximumFractionDigits: maximumFractionDigits,
});
// returns the Unicode locale identifier for the country id in the metadata
export function localeCode(countryId) {
return countryId === "uk" ? "en-GB" : "en-US";
}

export function localeString(metadata) {
return metadata.countryId === "uk" ? "en" : "en-US";
// returns the ISO 4217 currency codes for the currency for the country id in
// the metadata
export function currencyCode(countryId) {
return countryId === "uk" ? "GBP" : "USD";
}
21 changes: 16 additions & 5 deletions src/pages/household/input/VariableEditor.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSearchParams } from "react-router-dom";
import { capitalize } from "../../../api/language";
import { capitalize, localeCode } from "../../../api/language";
import {
currencyMap,
getNewHouseholdId,
Expand Down Expand Up @@ -231,11 +231,22 @@ function HouseholdVariableEntityInput(props) {
style={{
width: mobile ? 150 : 200,
}}
addonBefore={isCurrency ? currencyMap[variable.unit] : undefined}
min={isCurrency ? 0 : undefined}
precision={variable.valueType === "float" ? 2 : undefined}
{...(isCurrency
? {
addonBefore: currencyMap[variable.unit],
}
: {})}
{...(variable.valueType === "float"
? {
formatter: (value, {userTyping}) =>
(+value).toLocaleString(localeCode(metadata.countryId), {
minimumFractionDigits: !userTyping && 2,
maximumFractionDigits: 2,
}),
}
: {})}
defaultValue={defaultValue}
autoFocus={true}
autoFocus
onChange={submitValue}
/>
);
Expand Down
12 changes: 9 additions & 3 deletions src/pages/policy/output/AverageImpactByDecile.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { cardinal, currencyString, localeString } from "../../../api/language";
import { cardinal, localeCode, currencyCode } from "../../../api/language";
import { formatVariableValue } from "../../../api/variables";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
Expand Down Expand Up @@ -33,7 +33,13 @@ export default function AverageImpactByDecile(props) {
value < 0 ? style.colors.DARK_GRAY : style.colors.BLUE,
),
},
text: yArray.map((value) => currencyString(value, metadata, 0)),
text: yArray.map((value) =>
value.toLocaleString(localeCode(metadata.countryId), {
style: "currency",
currency: currencyCode(metadata.countryId),
maximumFractionDigits: 0,
}),
),
textangle: 0,
...(useHoverCard
? {
Expand Down Expand Up @@ -102,7 +108,7 @@ export default function AverageImpactByDecile(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
locale: localeCode(metadata.countryId),
}}
style={{
width: "100%",
Expand Down
12 changes: 9 additions & 3 deletions src/pages/policy/output/AverageImpactByWealthDecile.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { cardinal, currencyString, localeString } from "../../../api/language";
import { cardinal, localeCode, currencyCode } from "../../../api/language";
import { formatVariableValue } from "../../../api/variables";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
Expand Down Expand Up @@ -33,7 +33,13 @@ export default function AverageImpactByWealthDecile(props) {
value < 0 ? style.colors.DARK_GRAY : style.colors.BLUE,
),
},
text: yArray.map((value) => currencyString(value, metadata, 0)),
text: yArray.map((value) =>
value.toLocaleString(localeCode(metadata.countryId), {
style: "currency",
currency: currencyCode(metadata.countryId),
maximumFractionDigits: 0,
}),
),
textangle: 0,
...(useHoverCard
? {
Expand Down Expand Up @@ -102,7 +108,7 @@ export default function AverageImpactByWealthDecile(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
locale: localeCode(metadata.countryId),
}}
style={{
width: "100%",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/policy/output/BudgetaryImpact.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { aggregateCurrency, localeString } from "../../../api/language";
import { aggregateCurrency, localeCode } from "../../../api/language";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
import DownloadableScreenshottable from "./DownloadableScreenshottable";
Expand Down Expand Up @@ -156,7 +156,7 @@ export default function BudgetaryImpact(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
locale: localeCode(metadata.countryId),
}}
style={{
width: "100%",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/policy/output/DetailedBudgetaryImpact.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext } from "react";
import Plot from "react-plotly.js";
import { ChartLogo } from "../../../api/charts";
import { aggregateCurrency, localeString } from "../../../api/language";
import { aggregateCurrency, localeCode } from "../../../api/language";
import HoverCard, { HoverCardContext } from "../../../layout/HoverCard";
import useMobile from "../../../layout/Responsive";
import Screenshottable from "../../../layout/Screenshottable";
Expand Down Expand Up @@ -129,7 +129,7 @@ export default function DetailedBudgetaryImpact(props) {
config={{
displayModeBar: false,
responsive: true,
locale: localeString(metadata),
locale: localeCode(metadata.countryId),
}}
style={{
width: "100%",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module.exports = {
moduleType: "locale",
name: "en",
name: "en-GB",
dictionary: {
"Click to enter Colorscale title": "Click to enter Colourscale title",
},
Expand Down
4 changes: 2 additions & 2 deletions src/redesign/components/PolicyEngine.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Header from "./Header";
import Testimonials from "./Testimonials";
import CalculatorInterstitial from "./CalculatorInterstitial";
import CitizensEconomicCouncil from "./CitizensEconomicCouncil";
import loc_en from "../../plotly_locales/locale-en.js";
import loc_en_gb from "../../plotly_locales/locale-en-gb.js";
import loc_en_us from "../../plotly_locales/locale-en-us.js";
import APIDocumentationPage from "./APIDocumentationPage";
import CookieConsent from "layout/CookieConsent";
Expand All @@ -40,7 +40,7 @@ function ScrollToTop() {

export default function PolicyEngine({ pathname }) {
var Plotly = require("plotly.js/dist/plotly.js");
Plotly.register(loc_en);
Plotly.register(loc_en_gb);
Plotly.register(loc_en_us);
const COUNTRIES = ["us", "uk", "ca", "ng", "il"];

Expand Down
Loading