From d09dbc03ec63b0b7870da6bb69e109016e362cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Wed, 11 Dec 2024 11:16:15 +0000 Subject: [PATCH] refactor: Extract more structure from children --- .../@planx/components/Pay/Public/Confirm.tsx | 276 ++++++++---------- .../components/Pay/Public/InviteToPayForm.tsx | 7 +- .../src/@planx/components/Pay/Public/Pay.tsx | 1 - 3 files changed, 130 insertions(+), 154 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/Pay/Public/Confirm.tsx b/editor.planx.uk/src/@planx/components/Pay/Public/Confirm.tsx index 36e998fac4..4870717c32 100644 --- a/editor.planx.uk/src/@planx/components/Pay/Public/Confirm.tsx +++ b/editor.planx.uk/src/@planx/components/Pay/Public/Confirm.tsx @@ -24,7 +24,12 @@ import { FeeBreakdown } from "./FeeBreakdown/FeeBreakdown"; import InviteToPayForm, { InviteToPayFormProps } from "./InviteToPayForm"; import { PAY_API_ERROR_UNSUPPORTED_TEAM } from "./Pay"; -type State = "error" | "informationOnly" | "inviteToPay" | "zeroFee" | "pay"; +type ComponentState = + | "error" + | "informationOnly" + | "inviteToPay" + | "zeroFee" + | "pay"; export interface Props extends Omit { title?: string; @@ -75,111 +80,81 @@ const Error: React.FC = ({ onConfirm, error }) => { }; const PayBody: React.FC = (props) => { - const path = useStore((state) => state.path); - const isSaveReturn = path === ApplicationPath.SaveAndReturn; const defaults = getDefaultContent(); return ( - - - - {props.instructionsTitle || defaults.instructionsTitle} - - + <> + + {props.instructionsTitle || defaults.instructionsTitle} + + + + {props.showInviteToPay && ( - {props.showInviteToPay && ( - - )} - {isSaveReturn && } - - + )} + ); }; const InformationOnly: React.FC = (props) => { - const path = useStore((state) => state.path); - const isSaveReturn = path === ApplicationPath.SaveAndReturn; const defaults = getDefaultContent(); return ( - - - - {props.instructionsTitle || defaults.instructionsTitle} - - - - {isSaveReturn && } - - - ); -}; - -const ZeroFee: React.FC = (props) => { - const path = useStore((state) => state.path); - const isSaveReturn = path === ApplicationPath.SaveAndReturn; - const defaults = getDefaultContent(); - - return ( - - - - {props.instructionsTitle || defaults.instructionsTitle} - - - - {isSaveReturn && } - - + <> + + {props.instructionsTitle || defaults.instructionsTitle} + + + + ); }; -const getInitialState = (props: Props): State => { +const ZeroFee: React.FC = (props) => ( + +); + +const getInitialState = (props: Props): ComponentState => { if (props.error) return "error"; if (props.hidePay) return "informationOnly"; if (props.fee === 0) return "zeroFee"; @@ -189,17 +164,17 @@ const getInitialState = (props: Props): State => { export default function Confirm(props: Props) { const theme = useTheme(); - const [state, setState] = useState(getInitialState(props)); + const [componentState, setComponentState] = useState( + getInitialState(props), + ); + + const toggleToPayPage = () => setComponentState("pay"); + const toggleToInviteToPayPage = () => setComponentState("inviteToPay"); const defaults = getDefaultContent(); - const changePage = () => { - if (state === "pay" && !props.paymentStatus) { - setState("inviteToPay"); - } else { - setState("pay"); - } - }; + const path = useStore((state) => state.path); + const isSaveReturn = path === ApplicationPath.SaveAndReturn; const inviteToPayFormProps: InviteToPayFormProps = { nomineeTitle: props.nomineeTitle, @@ -208,63 +183,68 @@ export default function Confirm(props: Props) { yourDetailsDescription: props.yourDetailsDescription, yourDetailsLabel: props.yourDetailsLabel, paymentStatus: props.paymentStatus, - changePage, + changePage: toggleToPayPage, }; return ( - <> - - - {state === "inviteToPay" ? props.secondaryPageTitle : props.title} - - - {state !== "inviteToPay" && !props.hideFeeBanner && ( - - - - {props.bannerTitle || defaults.bannerTitle} - - - {isNaN(props.fee) - ? "Unknown" - : formattedPriceWithCurrencySymbol(props.fee)} - - - - - - {hasFeatureFlag("FEE_BREAKDOWN") && } - - )} - { + + + {componentState === "inviteToPay" + ? props.secondaryPageTitle + : props.title} + + + {componentState !== "inviteToPay" && !props.hideFeeBanner && ( + + + + {props.bannerTitle || defaults.bannerTitle} + + + {isNaN(props.fee) + ? "Unknown" + : formattedPriceWithCurrencySymbol(props.fee)} + + + + + + {hasFeatureFlag("FEE_BREAKDOWN") && } + + )} + + { - pay: , - informationOnly: , - inviteToPay: , - error: , - zeroFee: , - }[state] - } - + { + pay: , + informationOnly: , + inviteToPay: , + error: , + zeroFee: , + }[componentState] + } + {isSaveReturn && } + + ); } diff --git a/editor.planx.uk/src/@planx/components/Pay/Public/InviteToPayForm.tsx b/editor.planx.uk/src/@planx/components/Pay/Public/InviteToPayForm.tsx index f7a2db6531..8610f6f636 100644 --- a/editor.planx.uk/src/@planx/components/Pay/Public/InviteToPayForm.tsx +++ b/editor.planx.uk/src/@planx/components/Pay/Public/InviteToPayForm.tsx @@ -7,8 +7,6 @@ import type { PaymentRequest, PaymentStatus, } from "@opensystemslab/planx-core/types"; -import Card from "@planx/components/shared/Preview/Card"; -import SaveResumeButton from "@planx/components/shared/Preview/SaveResumeButton"; import { WarningContainer } from "@planx/components/shared/Preview/WarningContainer"; import DelayedLoadingIndicator from "components/DelayedLoadingIndicator/DelayedLoadingIndicator"; import { useFormik } from "formik"; @@ -164,7 +162,7 @@ const InviteToPayForm: React.FC = ({ return isLoading ? ( ) : ( - + <> {nomineeTitle} {nomineeDescription && ( @@ -279,8 +277,7 @@ const InviteToPayForm: React.FC = ({ > {"I want to pay for this application myself"} - {isSaveReturn && } - + ); }; diff --git a/editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx b/editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx index 0500811b6b..86af30606f 100644 --- a/editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx +++ b/editor.planx.uk/src/@planx/components/Pay/Public/Pay.tsx @@ -307,7 +307,6 @@ function Component(props: Props) { } showInviteToPay={showPayOptions && isTeamSupported} paymentStatus={govUkPayment?.state?.status} - hidePay={fee === 0 || props.hidePay} /> ) : (