fix(candidates): fix candidate selection loop

This commit is contained in:
Gabriel Ramos 2026-06-10 14:29:56 -04:00
parent 6c83fb6af7
commit 60fb20cc4b

View file

@ -235,20 +235,20 @@ export default function CandidatesPage() {
if (selectIdAfterFetch) { if (selectIdAfterFetch) {
const matched = data.find((c: Candidate) => c.id === selectIdAfterFetch); const matched = data.find((c: Candidate) => c.id === selectIdAfterFetch);
if (matched) setSelectedCandidate(matched); if (matched) setSelectedCandidate(matched);
} else if (data.length > 0 && !selectedCandidate) { } else {
// Default selection if none is currently selected // Keep current selection fresh using functional update
setSelectedCandidate(data[0]); setSelectedCandidate((prevSelected) => {
} else if (selectedCandidate) { if (!prevSelected) return data[0] || null;
// Keep current selection fresh const refreshed = data.find((c: Candidate) => c.id === prevSelected.id);
const refreshed = data.find((c: Candidate) => c.id === selectedCandidate.id); return refreshed || data[0] || null;
setSelectedCandidate(refreshed || data[0] || null); });
} }
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
setLoading(false); setLoading(false);
}); });
}, [selectedCandidate]); }, []);
useEffect(() => { useEffect(() => {
fetchCandidates(); fetchCandidates();