Skip to content

Commit 3ccd76f

Browse files
fix(forms): remove unused state and add unmount safety to webhook submit hook
Signed-off-by: Manishnemade12 <mnemade140@gmail.com>
1 parent 271d732 commit 3ccd76f

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/components/CommonForm/events.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import layer5_img from "../../assets/images/layer5/layer5-only/svg/layer5-white-
1010
const EventForm = ({ form, title, submit_title, submit_body }) => {
1111

1212
const [stepNumber, setStepNumber] = useState(0);
13-
const [memberFormOne, setMemberFormOne] = useState({});
14-
const [submit, setSubmit] = useState(false);
1513
const { submitForm } = useWebhookSubmit();
1614

1715
// Form values
@@ -42,7 +40,6 @@ const EventForm = ({ form, title, submit_title, submit_body }) => {
4240
if (success) {
4341
setStatus(null);
4442
setStepNumber(1);
45-
setSubmit(true);
4643
window.scrollTo(0,window.scrollY - 800);
4744
setFirstName(values.firstname);
4845
setEmail(values.email);

src/utils/hooks/useWebhookSubmit.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
1-
import { useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22
import axios from 'axios';
33

44
export const useWebhookSubmit = () => {
55
const [isSubmitting, setIsSubmitting] = useState(false);
66
const [error, setError] = useState(null);
7+
const isMountedRef = useRef(true);
8+
const requestControllerRef = useRef(null);
9+
10+
useEffect(() => {
11+
return () => {
12+
isMountedRef.current = false;
13+
if (requestControllerRef.current) {
14+
requestControllerRef.current.abort();
15+
}
16+
};
17+
}, []);
718

819
const submitForm = async (payload) => {
9-
setIsSubmitting(true);
10-
setError(null);
20+
const controller = new AbortController();
21+
requestControllerRef.current = controller;
22+
23+
if (isMountedRef.current) {
24+
setIsSubmitting(true);
25+
setError(null);
26+
}
27+
1128
try {
12-
const response = await axios.post("https://hook.us1.make.com/nficb3d7swqkclkl467st4hp4cg65u8o", payload);
29+
const response = await axios.post(
30+
"https://hook.us1.make.com/nficb3d7swqkclkl467st4hp4cg65u8o",
31+
payload,
32+
{ signal: controller.signal },
33+
);
1334
return { success: true, data: response.data };
1435
} catch (err) {
36+
const isAborted = controller.signal.aborted || err?.name === "CanceledError";
37+
if (isAborted) {
38+
return { success: false, error: null, errorMessage: "Request was cancelled." };
39+
}
40+
1541
const errorMessage = (err && err.message) || 'An error occurred during submission.';
1642
const errorObject = err instanceof Error ? err : new Error(errorMessage);
17-
setError(errorObject);
43+
if (isMountedRef.current) {
44+
setError(errorObject);
45+
}
1846
return { success: false, error: errorObject, errorMessage };
1947
} finally {
20-
setIsSubmitting(false);
48+
if (requestControllerRef.current === controller) {
49+
requestControllerRef.current = null;
50+
}
51+
if (isMountedRef.current) {
52+
setIsSubmitting(false);
53+
}
2154
}
2255
};
2356

0 commit comments

Comments
 (0)