Skip to content

Commit aa86320

Browse files
committed
Added error handling to Gateway save
1 parent 1f67686 commit aa86320

File tree

4 files changed

+41
-99
lines changed

4 files changed

+41
-99
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/settings/components/ChurchSettingsEdit.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ export const ChurchSettingsEdit: React.FC<Props> = (props) => {
1717
const [church, setChurch] = React.useState({} as ChurchInterface);
1818
const [errors, setErrors] = React.useState([]);
1919
const [saveTrigger, setSaveTrigger] = React.useState<Date | null>(null);
20+
const childErrorsRef = React.useRef<string[]>([]);
2021

2122
const handleSave = async () => {
2223
if (validate()) {
24+
setErrors([]);
25+
childErrorsRef.current = [];
2326
setSaveTrigger(new Date());
27+
await new Promise(resolve => setTimeout(resolve, 500));
28+
if (childErrorsRef.current.length > 0) return;
2429
const resp = await ApiHelper.post("/churches", [church], "MembershipApi");
2530
if (resp.errors !== undefined) setErrors(resp.errors);
2631
else props.updatedFunction();
@@ -76,9 +81,14 @@ export const ChurchSettingsEdit: React.FC<Props> = (props) => {
7681
setChurch(c);
7782
};
7883

84+
const handleGivingError = (givingErrors: string[]) => {
85+
childErrorsRef.current = givingErrors;
86+
setErrors(givingErrors);
87+
};
88+
7989
const giveSection = () => {
8090
if (!UserHelper.checkAccess(Permissions.givingApi.settings.edit)) return null;
81-
return <GivingSettingsEdit churchId={church?.id || ""} saveTrigger={saveTrigger} />;
91+
return <GivingSettingsEdit churchId={church?.id || ""} saveTrigger={saveTrigger} onError={handleGivingError} />;
8292
};
8393

8494
React.useEffect(() => setChurch(props.church), [props.church]);

src/settings/components/GivingSettingsEdit.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {
33
FormControl, InputLabel, MenuItem, Select, TextField, Grid, Stack, Switch, Typography, Tooltip, IconButton, type SelectChangeEvent
44
} from "@mui/material";
55
import HelpIcon from "@mui/icons-material/Help";
6-
import { ApiHelper, Locale, UniqueIdHelper } from "@churchapps/apphelper";
6+
import { ApiHelper, ErrorMessages, Locale, UniqueIdHelper } from "@churchapps/apphelper";
77
import { type PaymentGatewaysInterface } from "../../helpers";
88
import { FeeOptionsSettingsEdit } from "./FeeOptionsSettingsEdit";
99

1010
interface Props {
1111
churchId: string;
1212
saveTrigger: Date | null;
13+
onError?: (errors: string[]) => void;
1314
}
1415

1516
export const GivingSettingsEdit: React.FC<Props> = (props) => {
@@ -18,6 +19,7 @@ export const GivingSettingsEdit: React.FC<Props> = (props) => {
1819
const [publicKey, setPublicKey] = React.useState("");
1920
const [privateKey, setPrivateKey] = React.useState("");
2021
const [payFees, setPayFees] = React.useState<boolean>(false);
22+
const [errors, setErrors] = React.useState<string[]>([]);
2123

2224
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement> | SelectChangeEvent) => {
2325
e.preventDefault();
@@ -73,16 +75,30 @@ export const GivingSettingsEdit: React.FC<Props> = (props) => {
7375
}
7476
};
7577

76-
const save = () => {
77-
if (provider === "") {
78-
if (!UniqueIdHelper.isMissing(gateway?.id)) ApiHelper.delete("/gateways/" + gateway.id, "GivingApi");
79-
} else {
80-
const gw: PaymentGatewaysInterface = gateway === null ? { churchId: props.churchId } : { ...gateway };
81-
gw.provider = provider;
82-
gw.publicKey = publicKey;
83-
gw.payFees = payFees;
84-
if (privateKey !== "") gw.privateKey = privateKey;
85-
ApiHelper.post("/gateways", [gw], "GivingApi");
78+
const save = async () => {
79+
try {
80+
if (provider === "") {
81+
if (!UniqueIdHelper.isMissing(gateway?.id)) await ApiHelper.delete("/gateways/" + gateway.id, "GivingApi");
82+
} else {
83+
const gw: PaymentGatewaysInterface = gateway === null ? { churchId: props.churchId } : { ...gateway };
84+
gw.provider = provider;
85+
gw.publicKey = publicKey;
86+
gw.payFees = payFees;
87+
if (privateKey !== "") gw.privateKey = privateKey;
88+
await ApiHelper.post("/gateways", [gw], "GivingApi");
89+
}
90+
} catch (error: any) {
91+
let message = "An error occurred while saving gateway settings";
92+
if (error?.message) {
93+
try {
94+
const parsed = JSON.parse(error.message);
95+
message = parsed.message || error.message;
96+
} catch {
97+
message = error.message;
98+
}
99+
}
100+
setErrors([message]);
101+
if (props.onError) props.onError([message]);
86102
}
87103
};
88104

@@ -113,6 +129,7 @@ export const GivingSettingsEdit: React.FC<Props> = (props) => {
113129

114130
return (
115131
<>
132+
<ErrorMessages errors={errors} />
116133
{/* <div className="subHead">{Locale.label("settings.givingSettingsEdit.giving")}</div> */}
117134
<Grid container spacing={3} marginBottom={2}>
118135
<Grid size={{ xs: 12, md: 4 }}>

yarn.lock

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -514,51 +514,6 @@
514514
resolved "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.25.8.tgz"
515515
integrity sha512-cn3Yr7+OaaZq1c+2pe+8yxC8E144SReCQjN6/2ynubzYjvyqZjTXfQJpAcQpsdJq3My7XADANiYGHoFC69pLQw==
516516

517-
"@esbuild/netbsd-arm64@0.25.8":
518-
version "0.25.8"
519-
resolved "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.25.8.tgz"
520-
integrity sha512-d1KfruIeohqAi6SA+gENMuObDbEjn22olAR7egqnkCD9DGBG0wsEARotkLgXDu6c4ncgWTZJtN5vcgxzWRMzcw==
521-
522-
"@esbuild/netbsd-x64@0.25.8":
523-
version "0.25.8"
524-
resolved "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.25.8.tgz"
525-
integrity sha512-nVDCkrvx2ua+XQNyfrujIG38+YGyuy2Ru9kKVNyh5jAys6n+l44tTtToqHjino2My8VAY6Lw9H7RI73XFi66Cg==
526-
527-
"@esbuild/openbsd-arm64@0.25.8":
528-
version "0.25.8"
529-
resolved "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.25.8.tgz"
530-
integrity sha512-j8HgrDuSJFAujkivSMSfPQSAa5Fxbvk4rgNAS5i3K+r8s1X0p1uOO2Hl2xNsGFppOeHOLAVgYwDVlmxhq5h+SQ==
531-
532-
"@esbuild/openbsd-x64@0.25.8":
533-
version "0.25.8"
534-
resolved "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.25.8.tgz"
535-
integrity sha512-1h8MUAwa0VhNCDp6Af0HToI2TJFAn1uqT9Al6DJVzdIBAd21m/G0Yfc77KDM3uF3T/YaOgQq3qTJHPbTOInaIQ==
536-
537-
"@esbuild/openharmony-arm64@0.25.8":
538-
version "0.25.8"
539-
resolved "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.25.8.tgz"
540-
integrity sha512-r2nVa5SIK9tSWd0kJd9HCffnDHKchTGikb//9c7HX+r+wHYCpQrSgxhlY6KWV1nFo1l4KFbsMlHk+L6fekLsUg==
541-
542-
"@esbuild/sunos-x64@0.25.8":
543-
version "0.25.8"
544-
resolved "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.25.8.tgz"
545-
integrity sha512-zUlaP2S12YhQ2UzUfcCuMDHQFJyKABkAjvO5YSndMiIkMimPmxA+BYSBikWgsRpvyxuRnow4nS5NPnf9fpv41w==
546-
547-
"@esbuild/win32-arm64@0.25.8":
548-
version "0.25.8"
549-
resolved "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.25.8.tgz"
550-
integrity sha512-YEGFFWESlPva8hGL+zvj2z/SaK+pH0SwOM0Nc/d+rVnW7GSTFlLBGzZkuSU9kFIGIo8q9X3ucpZhu8PDN5A2sQ==
551-
552-
"@esbuild/win32-ia32@0.25.8":
553-
version "0.25.8"
554-
resolved "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.25.8.tgz"
555-
integrity sha512-hiGgGC6KZ5LZz58OL/+qVVoZiuZlUYlYHNAmczOm7bs2oE1XriPFi5ZHHrS8ACpV5EjySrnoCKmcbQMN+ojnHg==
556-
557-
"@esbuild/win32-x64@0.25.8":
558-
version "0.25.8"
559-
resolved "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.25.8.tgz"
560-
integrity sha512-cn3Yr7+OaaZq1c+2pe+8yxC8E144SReCQjN6/2ynubzYjvyqZjTXfQJpAcQpsdJq3My7XADANiYGHoFC69pLQw==
561-
562517
"@eslint-community/eslint-utils@^4.2.0", "@eslint-community/eslint-utils@^4.7.0":
563518
version "4.7.0"
564519
resolved "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.7.0.tgz"
@@ -1238,21 +1193,6 @@
12381193
resolved "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.46.2.tgz"
12391194
integrity sha512-CvUo2ixeIQGtF6WvuB87XWqPQkoFAFqW+HUo/WzHwuHDvIwZCtjdWXoYCcr06iKGydiqTclC4jU/TNObC/xKZg==
12401195

1241-
"@rollup/rollup-win32-arm64-msvc@4.46.2":
1242-
version "4.46.2"
1243-
resolved "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.46.2.tgz"
1244-
integrity sha512-wiJWMIpeaak/jsbaq2HMh/rzZxHVW1rU6coyeNNpMwk5isiPjSTx0a4YLSlYDwBH/WBvLz+EtsNqQScZTLJy3g==
1245-
1246-
"@rollup/rollup-win32-ia32-msvc@4.46.2":
1247-
version "4.46.2"
1248-
resolved "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.46.2.tgz"
1249-
integrity sha512-gBgaUDESVzMgWZhcyjfs9QFK16D8K6QZpwAaVNJxYDLHWayOta4ZMjGm/vsAEy3hvlS2GosVFlBlP9/Wb85DqQ==
1250-
1251-
"@rollup/rollup-win32-x64-msvc@4.46.2":
1252-
version "4.46.2"
1253-
resolved "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.46.2.tgz"
1254-
integrity sha512-CvUo2ixeIQGtF6WvuB87XWqPQkoFAFqW+HUo/WzHwuHDvIwZCtjdWXoYCcr06iKGydiqTclC4jU/TNObC/xKZg==
1255-
12561196
"@stripe/react-stripe-js@^3.8.0", "@stripe/react-stripe-js@^3.9.0":
12571197
version "3.9.0"
12581198
resolved "https://registry.npmjs.org/@stripe/react-stripe-js/-/react-stripe-js-3.9.0.tgz"
@@ -1270,21 +1210,6 @@
12701210
resolved "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.13.3.tgz"
12711211
integrity sha512-A+JSKGkRbPLVV2Kwx8TaDAV0yXIXm/gc8m98hSkVDGlPBBmydgzNdWy3X7HTUBM7IDk7YlWE7w2+RUGjdgpTmg==
12721212

1273-
"@swc/core-win32-arm64-msvc@1.13.3":
1274-
version "1.13.3"
1275-
resolved "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.13.3.tgz"
1276-
integrity sha512-elTQpnaX5vESSbhCEgcwXjpMsnUbqqHfEpB7ewpkAsLzKEXZaK67ihSRYAuAx6ewRQTo7DS5iTT6X5aQD3MzMw==
1277-
1278-
"@swc/core-win32-ia32-msvc@1.13.3":
1279-
version "1.13.3"
1280-
resolved "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.13.3.tgz"
1281-
integrity sha512-nvehQVEOdI1BleJpuUgPLrclJ0TzbEMc+MarXDmmiRFwEUGqj+pnfkTSb7RZyS1puU74IXdK/YhTirHurtbI9w==
1282-
1283-
"@swc/core-win32-x64-msvc@1.13.3":
1284-
version "1.13.3"
1285-
resolved "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.13.3.tgz"
1286-
integrity sha512-A+JSKGkRbPLVV2Kwx8TaDAV0yXIXm/gc8m98hSkVDGlPBBmydgzNdWy3X7HTUBM7IDk7YlWE7w2+RUGjdgpTmg==
1287-
12881213
"@swc/core@^1.12.11":
12891214
version "1.13.3"
12901215
resolved "https://registry.npmjs.org/@swc/core/-/core-1.13.3.tgz"
@@ -2271,16 +2196,6 @@ fs.realpath@^1.0.0:
22712196
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
22722197
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==
22732198

2274-
fsevents@~2.3.2, fsevents@2.3.2:
2275-
version "2.3.2"
2276-
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
2277-
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
2278-
2279-
fsevents@~2.3.3:
2280-
version "2.3.3"
2281-
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
2282-
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
2283-
22842199
function-bind@^1.1.2:
22852200
version "1.1.2"
22862201
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"

0 commit comments

Comments
 (0)