|
| 1 | +import { useState } from 'react'; |
| 2 | +import Button from '../components/UI/Button'; |
| 3 | +import classes from './ConfigureQuizScreen.module.css' |
| 4 | + |
| 5 | +const CATEGORIES = ['Linux', 'Bash', 'Docker', 'SQL', 'CMS', 'Code', 'DevOps']; |
| 6 | +const DIFFICULTY = ['Easy', 'Medium', 'Hard']; |
| 7 | +const NUMBERS = [1, 5, 10, 15, 20]; |
| 8 | + |
| 9 | +const ConfigureQuizScreen = (props) => { |
| 10 | + const [category, setCategory] = useState(null); |
| 11 | + const [difficulty, setDifficulty] = useState(null); |
| 12 | + const [numberOfQuestions, setNumberOfQuestions] = useState(null); |
| 13 | + |
| 14 | + const setCategoryHandler = (category) => { |
| 15 | + let selectedCategory = category.toLowerCase(); |
| 16 | + setCategory(selectedCategory); |
| 17 | + }; |
| 18 | + |
| 19 | + const setDifficultyHandler = (dif) => { |
| 20 | + setDifficulty(dif); |
| 21 | + }; |
| 22 | + |
| 23 | + const setNumberOfQuestionsHandler = (num) => { |
| 24 | + setNumberOfQuestions(num); |
| 25 | + }; |
| 26 | + |
| 27 | + if (numberOfQuestions) { |
| 28 | + let config = { category, difficulty, numberOfQuestions }; |
| 29 | + props.onStartQuiz(config); |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className={classes.container}> |
| 34 | + {!category && ( |
| 35 | + <div> |
| 36 | + <h1>Choose topic:</h1> |
| 37 | + {CATEGORIES.map((category) => ( |
| 38 | + <Button |
| 39 | + onClick={setCategoryHandler.bind(this, category)} |
| 40 | + key={category} |
| 41 | + text={category} |
| 42 | + /> |
| 43 | + ))} |
| 44 | + </div> |
| 45 | + )} |
| 46 | + {category && !difficulty && ( |
| 47 | + <div> |
| 48 | + <h1>Choose difficulty:</h1> |
| 49 | + {DIFFICULTY.map((dif) => ( |
| 50 | + <Button |
| 51 | + onClick={setDifficultyHandler.bind(this, dif)} |
| 52 | + key={dif} |
| 53 | + text={dif} |
| 54 | + /> |
| 55 | + ))} |
| 56 | + </div> |
| 57 | + )} |
| 58 | + {category && difficulty && ( |
| 59 | + <div> |
| 60 | + <h1>Choose number of questions:</h1> |
| 61 | + {NUMBERS.map((num) => ( |
| 62 | + <Button |
| 63 | + onClick={setNumberOfQuestionsHandler.bind(this, num)} |
| 64 | + key={num} |
| 65 | + text={num} |
| 66 | + /> |
| 67 | + ))} |
| 68 | + </div> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + ); |
| 72 | +}; |
| 73 | + |
| 74 | +export default ConfigureQuizScreen; |
0 commit comments