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

feat(fee-breakdown): Parse and display reductions and exemptions #4022

Closed
wants to merge 16 commits into from
Closed
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
8 changes: 6 additions & 2 deletions editor.planx.uk/src/@planx/components/Pay/Public/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Typography from "@mui/material/Typography";
import { PaymentStatus } from "@opensystemslab/planx-core/types";
import Card from "@planx/components/shared/Preview/Card";
import SaveResumeButton from "@planx/components/shared/Preview/SaveResumeButton";
import { hasFeatureFlag } from "lib/featureFlags";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useState } from "react";
import { ApplicationPath } from "types";
Expand All @@ -19,7 +20,7 @@ import {
getDefaultContent,
Pay,
} from "../model";
import { FeeBreakdown } from "./FeeBreakdown";
import { FeeBreakdown } from "./FeeBreakdown/FeeBreakdown";
import InviteToPayForm, { InviteToPayFormProps } from "./InviteToPayForm";
import { PAY_API_ERROR_UNSUPPORTED_TEAM } from "./Pay";

Expand Down Expand Up @@ -140,6 +141,9 @@ export default function Confirm(props: Props) {
changePage,
};

const showFeeBreakdown =
props.showFeeBreakdown && hasFeatureFlag("FEE_BREAKDOWN");

return (
<Box textAlign="left" width="100%">
<>
Expand Down Expand Up @@ -181,7 +185,7 @@ export default function Confirm(props: Props) {
/>
</Typography>
</FormWrapper>
{props.showFeeBreakdown && <FeeBreakdown />}
{showFeeBreakdown && <FeeBreakdown />}
</Banner>
)}
{page === "Pay" ? (
Expand Down
111 changes: 0 additions & 111 deletions editor.planx.uk/src/@planx/components/Pay/Public/FeeBreakdown.tsx

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>
Copy link
Contributor Author

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.

</TableCell>
</TableRow>
))
}
</>
);
};

// TODO: This won't show as if a fee is 0, we hide the whole Pay component from the user
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 string) it wasn't possible to parse in a meaningful way.

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[];
}
image

As a result, I've added the preProcessPassport() function to map a raw passport to this data shape.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 application.fee should be hardcoded or dynamic. One to explore another time I think!

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[];
}

};
Loading
Loading