Skip to content

Commit f2d2e23

Browse files
Fix the shaky timeout issue (#419)
1 parent 4b04140 commit f2d2e23

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/app/components/pages/IsaacCompetition/EntryForm/CompetitionEntryForm.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from "react";
1+
import React, { useEffect, useState, useRef } from "react";
22
import { Form, Row, Col, Container, FormGroup, Label, Input } from "reactstrap";
33
import { isaacApi, useAppSelector } from "../../../../state";
44
import { selectors } from "../../../../state/selectors";
@@ -26,6 +26,7 @@ export const CompetitionEntryForm = ({ handleTermsClick }: CompetitionEntryFormP
2626
const targetUser = useAppSelector(selectors.user.orNull);
2727
const reserveUsersOnCompetition = useReserveUsersOnCompetition();
2828
const [memberSelectionError, setMemberSelectionError] = useState<string>("");
29+
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
2930
const [userToUpdate, setUserToUpdate] = useState(targetUser ? { ...targetUser, password: null } : { password: null });
3031

3132
const handleUserUpdate = (user: any) => {
@@ -67,14 +68,28 @@ export const CompetitionEntryForm = ({ handleTermsClick }: CompetitionEntryFormP
6768
setSelectedMembers([]);
6869
}, [selectedGroupId]);
6970

71+
useEffect(() => {
72+
return () => {
73+
if (timeoutRef.current) {
74+
clearTimeout(timeoutRef.current);
75+
}
76+
};
77+
}, []);
78+
7079
const handleMemberSelection = (selectedOptions: any) => {
7180
const selectedValues = selectedOptions ? selectedOptions.map((option: any) => option.value) : [];
7281

82+
if (timeoutRef.current) {
83+
clearTimeout(timeoutRef.current);
84+
timeoutRef.current = null;
85+
}
86+
7387
if (selectedValues.length > 4) {
7488
setMemberSelectionError("Limit of 4 students reached. To select a new student, remove one first.");
7589

76-
setTimeout(() => {
90+
timeoutRef.current = setTimeout(() => {
7791
setMemberSelectionError("");
92+
timeoutRef.current = null;
7893
}, 10000);
7994

8095
return;

0 commit comments

Comments
 (0)