Skip to content

Commit f9d99f8

Browse files
authored
fix: update total donation amount live when fee coverage option is selected (#13)
1 parent b693572 commit f9d99f8

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎apphelper/src/donations/components/FundDonation.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from "react";
44
import { FundDonationInterface, FundInterface, CurrencyHelper } from "@churchapps/helpers";
5-
import { FormControl, Grid, Icon, InputLabel, MenuItem, Select, TextField, Typography } from "@mui/material";
5+
import { FormControl, Grid, Icon, InputLabel, MenuItem, Select, TextField, Typography, IconButton } from "@mui/material";
66
import type { SelectChangeEvent } from "@mui/material";
77
import { Locale } from "../helpers";
88

@@ -11,6 +11,7 @@ interface Props {
1111
funds: FundInterface[],
1212
index: number,
1313
updatedFunction: (fundDonation: FundDonationInterface, index: number) => void,
14+
removeFunction?: (index: number) => void,
1415
params?: any,
1516
currency?: string,
1617
hideFundSelect?: boolean,
@@ -42,8 +43,8 @@ export const FundDonation: React.FC<Props> = (props) => {
4243

4344
return (
4445
<>
45-
<Grid container spacing={3}>
46-
<Grid size={{ xs: 12, md: props.hideFundSelect ? 12 : 6 }}>
46+
<Grid container spacing={1} alignItems="center" sx={{ mb: 2 }}>
47+
<Grid size={{ xs: props.removeFunction ? 10 : 12, md: props.hideFundSelect ? (props.removeFunction ? 11 : 12) : 5 }}>
4748
<TextField fullWidth name="amount" label={Locale.label("donation.fundDonations.amount")} type="number" disabled={props.params?.amount && props.params.amount !== ""} aria-label="amount" lang="en-150" value={props.fundDonation.amount || ""} onChange={handleChange} InputProps={{ startAdornment: <Icon><Typography>{props.currency ? CurrencyHelper.getCurrencySymbol(props.currency) : "$"}</Typography></Icon> }} />
4849
</Grid>
4950
{!props.hideFundSelect && (
@@ -56,6 +57,13 @@ export const FundDonation: React.FC<Props> = (props) => {
5657
</FormControl>
5758
</Grid>
5859
)}
60+
{props.removeFunction && (
61+
<Grid size={{ xs: 2, md: 1 }} sx={{ display: "flex", justifyContent: "flex-end" }}>
62+
<IconButton onClick={() => props.removeFunction?.(props.index)} color="error" aria-label="remove">
63+
<Icon>delete</Icon>
64+
</IconButton>
65+
</Grid>
66+
)}
5967
</Grid>
6068
</>
6169
);

‎apphelper/src/donations/components/FundDonations.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ export const FundDonations: React.FC<Props> = (props) => {
2323
props.updatedFunction(fundDonations);
2424
};
2525

26+
const removeRow = (index: number) => {
27+
const fundDonations = [...props.fundDonations];
28+
fundDonations.splice(index, 1);
29+
props.updatedFunction(fundDonations);
30+
};
31+
2632
const getRows = () => {
2733
const result = [];
2834
for (let i = 0; i < props.fundDonations.length; i++) {
2935
const fd = props.fundDonations[i];
30-
result.push(<FundDonation fundDonation={fd} funds={props.funds} updatedFunction={handleUpdated} params={props?.params} key={fd.fundId || i} index={i} currency={props?.currency} hideFundSelect={props?.hideFundSelect} />);
36+
const canRemove = props.fundDonations.length > 1;
37+
result.push(<FundDonation fundDonation={fd} funds={props.funds} updatedFunction={handleUpdated} removeFunction={canRemove ? removeRow : undefined} params={props?.params} key={fd.fundId || i} index={i} currency={props?.currency} hideFundSelect={props?.hideFundSelect} />);
3138
}
3239

3340
return result;

‎apphelper/src/donations/components/MultiGatewayDonationForm.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const MultiGatewayDonationInner: React.FC<Props> = (props) => {
6363
const [transactionFee, setTransactionFee] = useState<number>(0);
6464
const [payFee, setPayFee] = useState<number>(0);
6565
const [total, setTotal] = useState<number>(0);
66+
const [coverFee, setCoverFee] = useState<boolean>(false);
6667
const [paymentMethodName, setPaymentMethodName] = useState<string>(
6768
props?.paymentMethods?.length > 0 ? `${props.paymentMethods[0].name} ${props.paymentMethods[0].last4 ? `****${props.paymentMethods[0].last4}` : props.paymentMethods[0].email || ""}` : ""
6869
);
@@ -146,6 +147,7 @@ const MultiGatewayDonationInner: React.FC<Props> = (props) => {
146147
const d = { ...donation } as MultiGatewayDonationInterface;
147148
d.amount = checked ? fundsTotal + transactionFee : fundsTotal;
148149
const showFee = checked ? transactionFee : 0;
150+
setCoverFee(checked);
149151
setTotal(d.amount);
150152
setPayFee(showFee);
151153
setDonation(d);
@@ -352,9 +354,15 @@ const MultiGatewayDonationInner: React.FC<Props> = (props) => {
352354
setTotal(updatedAmount);
353355
setPayFee(fee);
354356
setDonation(prev => ({ ...prev, amount: updatedAmount }));
357+
} else if (coverFee) {
358+
// User has opted in to cover fees — recalculate total with the updated fee
359+
const updatedAmount = totalAmount + fee;
360+
setTotal(updatedAmount);
361+
setPayFee(fee);
362+
setDonation(prev => ({ ...prev, amount: updatedAmount }));
355363
}
356364
}, 500);
357-
}, [donation, funds, gateway, selectedGatewayObj?.id, selectedGateway, getTransactionFee]);
365+
}, [donation, funds, gateway, selectedGatewayObj?.id, selectedGateway, getTransactionFee, coverFee]);
358366

359367
useEffect(() => {
360368
loadFunds();
@@ -593,7 +601,7 @@ const MultiGatewayDonationInner: React.FC<Props> = (props) => {
593601
) : (
594602
<FormGroup>
595603
<FormControlLabel
596-
control={<Checkbox />}
604+
control={<Checkbox checked={coverFee} />}
597605
name="transaction-fee"
598606
label={Locale.label("donation.donationForm.cover").replace("{}", CurrencyHelper.formatCurrencyWithLocale(transactionFee, gateway?.currency || "USD"))}
599607
onChange={handleCheckChange}

0 commit comments

Comments
 (0)