|
|
@@ -1,27 +1,31 @@ |
|
|
|
import { IonButton, IonCheckbox, IonItem, IonLabel, IonList, IonRadio, IonRadioGroup } from '@ionic/react'; |
|
|
|
import { useEffect, useRef, useState } from 'react'; |
|
|
|
import { Link } from 'react-router-dom'; |
|
|
|
import Input from '../../components/formInput/Input'; |
|
|
|
import styles from './Options.module.scss'; |
|
|
|
|
|
|
|
interface OwnProps { |
|
|
|
options: string[] | undefined; |
|
|
|
type: string; |
|
|
|
answer: string[]; |
|
|
|
lastQuestion: boolean; |
|
|
|
questionNumber: number; |
|
|
|
updateQuestionNumber: () => void; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const Options: React.FC<OwnProps> = (props) => { |
|
|
|
|
|
|
|
const [selected, setSelected] = useState<string | undefined>(undefined); |
|
|
|
const [selectedOptions, setSelectedOptions] = useState<string[]>([]); |
|
|
|
const [textInput, setTextInput] = useState<String>(""); |
|
|
|
const [answers, setAnswers] = useState<string[]>([]); |
|
|
|
const [textInput, setTextInput] = useState<string>(""); |
|
|
|
const [answers, setAnswers] = useState<number>(0); |
|
|
|
const inputRef = useRef<HTMLInputElement>(null); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
console.log("render"); |
|
|
|
setSelected(undefined); |
|
|
|
setSelectedOptions([]); |
|
|
|
setTextInput(""); |
|
|
|
}, [props.options]); |
|
|
|
|
|
|
|
const selectChecked = (option: string) => { |
|
|
@@ -34,14 +38,37 @@ const Options: React.FC<OwnProps> = (props) => { |
|
|
|
setSelectedOptions(newOption); |
|
|
|
} |
|
|
|
|
|
|
|
console.log(selectedOptions); |
|
|
|
console.log(selected); |
|
|
|
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!) |
|
|
|
console.log(textInput); |
|
|
|
} |
|
|
|
|
|
|
|
if ((selected || textInput)) { |
|
|
|
console.log("decision"); |
|
|
|
} |
|
|
|
|
|
|
|
if (props.lastQuestion) { |
|
|
|
console.log("last"); |
|
|
|
localStorage.setItem("answer", answers.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const options = props.options!.map((option, key) => { |
|
|
|
return ( |
|
|
|
<IonItem lines='none' key={key} className={(option === selected) ? styles.highlighted : ""}> |
|
|
@@ -75,7 +102,7 @@ const Options: React.FC<OwnProps> = (props) => { |
|
|
|
{MultiSelectOptions} |
|
|
|
</div> |
|
|
|
} |
|
|
|
{props.type === "textInpit" && |
|
|
|
{props.type === "textInput" && |
|
|
|
<div className={styles.options}> |
|
|
|
<input |
|
|
|
type="text" |
|
|
@@ -85,13 +112,20 @@ const Options: React.FC<OwnProps> = (props) => { |
|
|
|
onChange={handleInput} /> |
|
|
|
</div> |
|
|
|
} |
|
|
|
|
|
|
|
</IonList> |
|
|
|
|
|
|
|
<div className={styles.button + " " + (selected ? styles.active : "")} |
|
|
|
onClick={() => props.updateQuestionNumber()}> |
|
|
|
<IonButton shape="round" expand='block'>Next</IonButton> |
|
|
|
</div> |
|
|
|
{!props.lastQuestion && |
|
|
|
<div className={styles.button + " " + ((selected || selectedOptions.length > 0 || textInput) ? styles.active : "")} |
|
|
|
onClick={(selected || selectedOptions.length > 0 || textInput) ? () => { props.updateQuestionNumber(); validateAnswer(); } : undefined}> |
|
|
|
<IonButton shape="round" expand='block'>Next</IonButton> |
|
|
|
</div> |
|
|
|
} |
|
|
|
|
|
|
|
{props.lastQuestion && |
|
|
|
< Link to="/preliminaryRoundResults" className={styles.button + " " + (selected ? styles.active : "")}> |
|
|
|
<IonButton shape="round" expand='block'>Next Step</IonButton> |
|
|
|
</Link> |
|
|
|
} |
|
|
|
</div > |
|
|
|
); |
|
|
|
} |
|
|
|