import { IonButton, IonCheckbox, IonItem, IonLabel, IonList, IonRadio, IonRadioGroup } from '@ionic/react'; import { useEffect, useRef, useState } from 'react'; import { Link } from 'react-router-dom'; import styles from './Options.module.scss'; import { OptionType } from '../../models/QuizDetails' import RadioButton from '../../components/RadioButton/RadioButton'; interface OwnProps { options: string[] | undefined; type: OptionType; answer: string[]; lastQuestion: boolean; questionNumber: number; updateQuestionNumber: () => void; } const Options: React.FC = (props) => { const [selected, setSelected] = useState(undefined); const [selectedOptions, setSelectedOptions] = useState([]); const [textInput, setTextInput] = useState(""); const [answers, setAnswers] = useState(0); const inputRef = useRef(null); useEffect(() => { setSelected(""); setSelectedOptions([]); setTextInput(""); }, [props.questionNumber]); const selectChecked = (option: string) => { let newOption: string[] = []; if (selectedOptions.includes(option)) { newOption = selectedOptions.filter(oldOption => oldOption !== option); } else { newOption = selectedOptions.concat([option]); } setSelectedOptions(newOption); } const validateAnswer = () => { if (props.type === "singleSelect") { if (props.answer.includes(selected!)) { setAnswers(answers + 1); } } else if (props.type === "multiSelect") { if (props.answer.length === selectedOptions.length && props.answer.sort().join(',') === selectedOptions.sort().join(',')) { setAnswers(answers + 1); } } else if (props.type === "textInput") { if (props.answer.includes(textInput)) { setAnswers(answers + 1); } } } const handleInput = () => { setTextInput(inputRef.current?.value!); } const setAnswer = () => { localStorage.setItem("answer", answers.toString()); } const options = props.options!.map((option, key) => { return (
setSelected(option)}>
); }); const MultiSelectOptions = props.options!.map((option, key) => { return (
selectChecked(option)}>
); }); return (
{props.type === "singleSelect" &&
{options}
} {props.type === "multiSelect" &&
{MultiSelectOptions}
} {props.type === "textInput" &&
}
{!props.lastQuestion &&
0 || textInput) ? styles.active : "")} onClick={(selected || selectedOptions.length > 0 || textInput) ? () => { props.updateQuestionNumber(); validateAnswer(); } : undefined}> Next
} {props.lastQuestion && < Link to="/preliminaryRoundResults" onClick={setAnswer} className={styles.button + " " + ((selected || selectedOptions.length > 0 || textInput) ? styles.active : "")}> Next Step }
); } export default Options;