-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web): fix line breaks in currency values
- Loading branch information
Showing
3 changed files
with
115 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import * as React from "react"; | ||
const formatDef = new Intl.NumberFormat("de", { | ||
currency: "EUR", | ||
style: "currency", | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
}); | ||
|
||
export const useFormatCurrency = () => { | ||
return React.useCallback((value: number, currencySymbol: string) => { | ||
return `${value.toFixed(2)} ${currencySymbol}`; | ||
return formatDef.format(value).replace("€", currencySymbol); | ||
}, []); | ||
}; |
105 changes: 105 additions & 0 deletions
105
frontend/apps/web/src/pages/accounts/BalanceBarGraph.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as React from "react"; | ||
import { useFormatCurrency } from "@/hooks"; | ||
import { useAppSelector } from "@/store"; | ||
import { selectAccountBalances, useSortedAccounts } from "@abrechnung/redux"; | ||
import { Theme, useMediaQuery } from "@mui/material"; | ||
import { useTheme } from "@mui/material/styles"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { Bar, BarChart, Cell, LabelList, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; | ||
import { CategoricalChartFunc } from "recharts/types/chart/generateCategoricalChart"; | ||
import { Group } from "@abrechnung/api"; | ||
|
||
export type BalanceBarGraphProps = { | ||
group: Group; | ||
}; | ||
|
||
type Data = { | ||
name: string; | ||
id: number; | ||
balance: number; | ||
totalPaid: number; | ||
totalConsumed: number; | ||
}; | ||
|
||
export const BalanceBarGraph: React.FC<BalanceBarGraphProps> = ({ group }) => { | ||
const formatCurrency = useFormatCurrency(); | ||
const theme: Theme = useTheme(); | ||
const isSmallScreen = useMediaQuery(theme.breakpoints.down("sm")); | ||
const navigate = useNavigate(); | ||
|
||
const personalAccounts = useSortedAccounts(group.id, "name", "personal"); | ||
const balances = useAppSelector((state) => selectAccountBalances(state, group.id)); | ||
|
||
const colorGreen = theme.palette.mode === "light" ? theme.palette.success.light : theme.palette.success.dark; | ||
const colorRed = theme.palette.mode === "light" ? theme.palette.error.light : theme.palette.error.dark; | ||
|
||
const roundTwoDecimals = (val: number) => +val.toFixed(2); | ||
|
||
const chartData: Data[] = personalAccounts.map((account) => { | ||
const balance = balances[account.id]; | ||
return { | ||
name: account.name, | ||
balance: roundTwoDecimals(balance?.balance ?? 0), | ||
totalPaid: roundTwoDecimals(balance?.totalPaid ?? 0), | ||
totalConsumed: roundTwoDecimals(balance?.totalConsumed ?? 0), | ||
id: account.id, | ||
}; | ||
}); | ||
|
||
const chartHeight = Object.keys(balances).length * 30 + 100; | ||
|
||
// TODO determine the rendered width of the account names and take the maximum | ||
const yaxiswidth = isSmallScreen | ||
? Math.max(Math.max(...personalAccounts.map((account) => account.name.length)), 20) | ||
: Math.max(...personalAccounts.map((account) => account.name.length)) * 7 + 5; | ||
|
||
const handleBarClick: CategoricalChartFunc = (data) => { | ||
const id = data.activePayload?.[0].payload.id; | ||
navigate(`/groups/${group.id}/accounts/${id}`); | ||
}; | ||
|
||
return ( | ||
<div className="area-chart-wrapper" style={{ width: "100%", height: `${chartHeight}px` }}> | ||
<ResponsiveContainer> | ||
<BarChart | ||
data={chartData} | ||
margin={{ | ||
top: 20, | ||
right: 20, | ||
bottom: 20, | ||
left: 30, | ||
}} | ||
layout="vertical" | ||
onClick={handleBarClick} | ||
> | ||
<XAxis stroke={theme.palette.text.primary} type="number" unit={group.currency_symbol} /> | ||
<YAxis dataKey="name" stroke={theme.palette.text.primary} type="category" width={yaxiswidth} /> | ||
<Tooltip | ||
formatter={(label) => formatCurrency(parseFloat(String(label)), group.currency_symbol)} | ||
labelStyle={{ | ||
color: theme.palette.text.primary, | ||
}} | ||
itemStyle={{ | ||
color: theme.palette.text.primary, | ||
}} | ||
contentStyle={{ | ||
backgroundColor: theme.palette.background.paper, | ||
borderColor: theme.palette.divider, | ||
borderRadius: theme.shape.borderRadius, | ||
}} | ||
/> | ||
<Bar dataKey="balance"> | ||
{chartData.map((entry, index) => { | ||
return <Cell key={`cell-${index}`} fill={entry["balance"] >= 0 ? colorGreen : colorRed} />; | ||
})} | ||
<LabelList | ||
dataKey={(entry) => formatCurrency((entry as Data).balance, group.currency_symbol)} | ||
position="insideLeft" | ||
fill={theme.palette.text.primary} | ||
/> | ||
</Bar> | ||
</BarChart> | ||
</ResponsiveContainer> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters