import {useState, useEffect, useRef, KeyboardEvent} from 'react'; import {useTranslation} from 'next-i18next'; import {Container} from 'reactstrap'; import {faArrowRight} from '@fortawesome/free-solid-svg-icons'; import {MAX_NUM_CANDIDATES} from '@services/constants'; import Alert from '@components/Alert'; import Button from '@components/Button'; import {useElection, useElectionDispatch} from '@services/ElectionContext'; import CandidateField from './CandidateField'; const CandidatesField = ({onSubmit}) => { const {t} = useTranslation(); const submitReference = useRef(null); const election = useElection(); const dispatch = useElectionDispatch(); const candidates = election.candidates; const [error, setError] = useState(null); const disabled = candidates.filter((c) => c.name !== '').length < 2; // What to do when we change the candidates useEffect(() => { // Initialize the list with at least two candidates if (candidates.length < 2) { dispatch({type: 'candidate-push', value: 'default'}); } if (candidates.length > MAX_NUM_CANDIDATES) { setError('error.too-many-candidates'); } }, [candidates]); useEffect(() => { if (!disabled && submitReference.current) { submitReference.current.focus(); } }, [disabled, submitReference]); const handleKeyPress = (e: KeyboardEvent) => { console.log(e.key) if (e.key == "Enter" && !disabled) { onSubmit(); } } return (

{t('admin.add-candidates')}

{candidates.map((candidate, index) => { return ( ); })}
); }; export default CandidatesField;