|
1 | | -import React, { useEffect, useState } from "react"; |
| 1 | +import React, { useEffect, useState, useRef } from "react"; |
2 | 2 | import { Form, Row, Col, Container, FormGroup, Label, Input } from "reactstrap"; |
3 | 3 | import { isaacApi, useAppSelector } from "../../../../state"; |
4 | 4 | import { selectors } from "../../../../state/selectors"; |
@@ -26,6 +26,7 @@ export const CompetitionEntryForm = ({ handleTermsClick }: CompetitionEntryFormP |
26 | 26 | const targetUser = useAppSelector(selectors.user.orNull); |
27 | 27 | const reserveUsersOnCompetition = useReserveUsersOnCompetition(); |
28 | 28 | const [memberSelectionError, setMemberSelectionError] = useState<string>(""); |
| 29 | + const timeoutRef = useRef<NodeJS.Timeout | null>(null); |
29 | 30 | const [userToUpdate, setUserToUpdate] = useState(targetUser ? { ...targetUser, password: null } : { password: null }); |
30 | 31 |
|
31 | 32 | const handleUserUpdate = (user: any) => { |
@@ -67,14 +68,28 @@ export const CompetitionEntryForm = ({ handleTermsClick }: CompetitionEntryFormP |
67 | 68 | setSelectedMembers([]); |
68 | 69 | }, [selectedGroupId]); |
69 | 70 |
|
| 71 | + useEffect(() => { |
| 72 | + return () => { |
| 73 | + if (timeoutRef.current) { |
| 74 | + clearTimeout(timeoutRef.current); |
| 75 | + } |
| 76 | + }; |
| 77 | + }, []); |
| 78 | + |
70 | 79 | const handleMemberSelection = (selectedOptions: any) => { |
71 | 80 | const selectedValues = selectedOptions ? selectedOptions.map((option: any) => option.value) : []; |
72 | 81 |
|
| 82 | + if (timeoutRef.current) { |
| 83 | + clearTimeout(timeoutRef.current); |
| 84 | + timeoutRef.current = null; |
| 85 | + } |
| 86 | + |
73 | 87 | if (selectedValues.length > 4) { |
74 | 88 | setMemberSelectionError("Limit of 4 students reached. To select a new student, remove one first."); |
75 | 89 |
|
76 | | - setTimeout(() => { |
| 90 | + timeoutRef.current = setTimeout(() => { |
77 | 91 | setMemberSelectionError(""); |
| 92 | + timeoutRef.current = null; |
78 | 93 | }, 10000); |
79 | 94 |
|
80 | 95 | return; |
|
0 commit comments