-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat(fee-breakdown): Parse and display reductions and exemptions #4022
Changes from all commits
0c699f3
35e4df8
fc82d58
50b7831
be64a13
ae5c5a8
9eb735c
595d8dd
3e52523
d4ffe09
ab3f9e0
c408fc9
5a584ab
ab81f4f
d84e6a6
ae64367
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import Box from "@mui/material/Box"; | ||
import { styled } from "@mui/material/styles"; | ||
import Table from "@mui/material/Table"; | ||
import TableBody from "@mui/material/TableBody"; | ||
import TableCell, { tableCellClasses } from "@mui/material/TableCell"; | ||
import TableContainer from "@mui/material/TableContainer"; | ||
import TableHead from "@mui/material/TableHead"; | ||
import TableRow from "@mui/material/TableRow"; | ||
import Typography from "@mui/material/Typography"; | ||
import React from "react"; | ||
import { FONT_WEIGHT_SEMI_BOLD } from "theme"; | ||
|
||
import { formattedPriceWithCurrencySymbol } from "../../model"; | ||
import { useFeeBreakdown } from "./useFeeBreakdown"; | ||
|
||
const StyledTable = styled(Table)(() => ({ | ||
[`& .${tableCellClasses.root}`]: { | ||
paddingLeft: 0, | ||
paddingRight: 0, | ||
}, | ||
})); | ||
|
||
const BoldTableRow = styled(TableRow)(() => ({ | ||
[`& .${tableCellClasses.root}`]: { | ||
fontWeight: FONT_WEIGHT_SEMI_BOLD, | ||
}, | ||
})); | ||
|
||
const VAT_RATE = 0.2; | ||
|
||
const DESCRIPTION = | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."; | ||
|
||
const Header = () => ( | ||
<TableHead> | ||
<BoldTableRow> | ||
<TableCell>Description</TableCell> | ||
<TableCell align="right">Amount</TableCell> | ||
</BoldTableRow> | ||
</TableHead> | ||
); | ||
|
||
const ApplicationFee: React.FC<{ amount: number }> = ({ amount }) => ( | ||
<TableRow> | ||
<TableCell>Application fee</TableCell> | ||
<TableCell align="right"> | ||
{formattedPriceWithCurrencySymbol(amount)} | ||
</TableCell> | ||
</TableRow> | ||
); | ||
|
||
const Reductions: React.FC<{ amount?: number, reductions: string[] }> = ({ amount, reductions }) => { | ||
if (!amount) return null; | ||
|
||
return ( | ||
<> | ||
<TableRow> | ||
<TableCell>Reductions</TableCell> | ||
<TableCell align="right"> | ||
{formattedPriceWithCurrencySymbol(-amount)} | ||
</TableCell> | ||
</TableRow> | ||
{ | ||
reductions.map((reduction) => ( | ||
<TableRow> | ||
<TableCell colSpan={2}> | ||
<Box sx={{ pl: 2, color: "grey" }}>{reduction}</Box> | ||
</TableCell> | ||
</TableRow> | ||
)) | ||
} | ||
</> | ||
); | ||
}; | ||
|
||
// TODO: This won't show as if a fee is 0, we hide the whole Pay component from the user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll pick this up as a follow up PR 👍 |
||
const Exemptions: React.FC<{ amount: number, exemptions: string[] }> = ({ amount, exemptions }) => { | ||
if (!exemptions.length) return null; | ||
|
||
return ( | ||
<> | ||
<TableRow> | ||
<TableCell>Exemptions</TableCell> | ||
<TableCell align="right"> | ||
{formattedPriceWithCurrencySymbol(-amount)} | ||
</TableCell> | ||
</TableRow> | ||
{ | ||
exemptions.map((exemption) => ( | ||
<TableRow> | ||
<TableCell colSpan={2}> | ||
<Box sx={{ pl: 2, color: "grey" }}>{exemption}</Box> | ||
</TableCell> | ||
</TableRow> | ||
)) | ||
} | ||
</> | ||
); | ||
}; | ||
|
||
const VAT: React.FC<{ amount?: number }> = ({ amount }) => { | ||
if (!amount) return null; | ||
|
||
return ( | ||
<TableRow> | ||
<TableCell variant="footer">{`Includes VAT (${ | ||
VAT_RATE * 100 | ||
}%)`}</TableCell> | ||
<TableCell variant="footer" align="right"> | ||
{formattedPriceWithCurrencySymbol(amount)} | ||
</TableCell> | ||
</TableRow> | ||
); | ||
}; | ||
|
||
const Total: React.FC<{ amount: number }> = ({ amount }) => ( | ||
<BoldTableRow> | ||
<TableCell>Total</TableCell> | ||
<TableCell align="right"> | ||
{formattedPriceWithCurrencySymbol(amount)} | ||
</TableCell> | ||
</BoldTableRow> | ||
); | ||
|
||
export const FeeBreakdown: React.FC = () => { | ||
const breakdown = useFeeBreakdown(); | ||
if (!breakdown) return null; | ||
|
||
const { amount, reductions, exemptions } = breakdown; | ||
|
||
return ( | ||
<Box mt={3}> | ||
<Typography variant="h3" mb={1}> | ||
Fee breakdown | ||
</Typography> | ||
<Typography variant="body1" mb={2}> | ||
{DESCRIPTION} | ||
</Typography> | ||
<TableContainer> | ||
<StyledTable data-testid="fee-breakdown-table"> | ||
<Header /> | ||
<TableBody> | ||
<ApplicationFee amount={amount.applicationFee} /> | ||
<Reductions amount={amount.reduction} reductions={reductions}/> | ||
<Exemptions amount={amount.applicationFee} exemptions={exemptions}/> | ||
<Total amount={amount.total} /> | ||
<VAT amount={amount.vat} /> | ||
</TableBody> | ||
</StyledTable> | ||
</TableContainer> | ||
</Box> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface FeeBreakdown { | ||
amount: { | ||
applicationFee: number; | ||
total: number; | ||
reduction: number; | ||
vat: number | undefined; | ||
}; | ||
reductions: string[]; | ||
exemptions: string[]; | ||
} | ||
|
||
export interface PassportFeeFields { | ||
amount: { | ||
"application.fee.calculated": number; | ||
"application.fee.payable": number; | ||
"application.fee.payable.vat": number; | ||
}, | ||
reductions?: Record<string, boolean> | ||
exemptions?: Record<string, boolean> | ||
Comment on lines
+13
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change to this structure was pretty much required in order for this to be parsed by Zod. Initially I spend a fair bit of time trying to get a "raw" passport in here, but as the type of they keys was a list of known strings, and a list of unknown strings (i.e. just Here's an invalid / not meaningful TS interface that shows this - interface PassportFeeFields {
// Known keys
"application.fee.calculated": number;
"application.fee.payable": number;
"application.fee.payable.vat": number;
// Unknown keys
[key: string]: string[];
} As a result, I've added the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another valid and really nice approach here I haven't dug into (but could be worth trying) would be to use template literal types. I think this is a super interesting approach, but I suspect it will add more complexity not less. I'm also not sure how Zod would handle this, and I still have questions around if interface PassportFeeFields {
"application.fee.calculated": number;
"application.fee.payable": number;
"application.fee.payable.vat": number;
[key: `application.fee.reductions.${string}`]: string[];
[key: `application.fee.exemptions.${string}`]: string[];
} |
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll pick up displaying a meaningful value here, alongside associated validation, as another PR.
A meaningful value could be
.description
from the schema, or a.reason
from the passport.