|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import Axios from "axios"; |
| 3 | +import Loading from "./assets/loading.gif"; |
| 4 | +import Quiz from "./quiz"; |
| 5 | + |
| 6 | +const SetupQuiz = () => { |
| 7 | + const [amount, setAmount] = useState(5); |
| 8 | + const [difficulty, setDifficulty] = useState("easy"); |
| 9 | + const [category, setCategory] = useState(21); |
| 10 | + const [isStart, setIsStart] = useState(false); |
| 11 | + const [isLoading, setIsLoading] = useState(false); |
| 12 | + const [isError, setIsError] = useState(false); |
| 13 | + const [questions, setQuestions] = useState([]); |
| 14 | + const BASE_URL = `https://opentdb.com/api.php?amount=${amount}&difficulty=${difficulty}&category=${category}`; |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + if (amount > 50) { |
| 18 | + setAmount(50); |
| 19 | + } else if (amount < 1) { |
| 20 | + setAmount(1); |
| 21 | + } |
| 22 | + }, [amount]); |
| 23 | + |
| 24 | + const startQuiz = () => { |
| 25 | + setIsLoading(true); |
| 26 | + Axios.get(BASE_URL) |
| 27 | + .then((resp) => resp.data.results) |
| 28 | + .then((data) => { |
| 29 | + if (data.length < 1) { |
| 30 | + setIsStart(false); |
| 31 | + setIsLoading(false); |
| 32 | + setIsError(true); |
| 33 | + } else { |
| 34 | + setQuestions(data); |
| 35 | + setIsLoading(false); |
| 36 | + setIsError(false); |
| 37 | + setIsStart(true); |
| 38 | + } |
| 39 | + }) |
| 40 | + .catch((err) => { |
| 41 | + setIsError(true); |
| 42 | + setIsStart(false); |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + if (isLoading) { |
| 47 | + return ( |
| 48 | + <> |
| 49 | + <img src={Loading} alt="Loading spinner is spinning." /> |
| 50 | + </> |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + if (!isStart) { |
| 55 | + return ( |
| 56 | + <div className="container"> |
| 57 | + <div className="setup-card"> |
| 58 | + <h1 style={{ fontWeight: "bold" }}>Setup Quiz</h1> |
| 59 | + <form onSubmit={(e) => e.preventDefault()}> |
| 60 | + <section className="form-section"> |
| 61 | + <label htmlFor="amount">Number Of Questions</label> |
| 62 | + <br /> |
| 63 | + <input |
| 64 | + type="number" |
| 65 | + name="amount" |
| 66 | + id="amount" |
| 67 | + value={amount} |
| 68 | + onChange={(e) => setAmount(e.target.value)} |
| 69 | + max="50" |
| 70 | + min="1" |
| 71 | + /> |
| 72 | + </section> |
| 73 | + <section className="form-section"> |
| 74 | + <label htmlFor="category">Select Category</label> |
| 75 | + <br /> |
| 76 | + <select |
| 77 | + name="category" |
| 78 | + id="category" |
| 79 | + value={category} |
| 80 | + onChange={(e) => setCategory(e.target.value)} |
| 81 | + > |
| 82 | + <option value="21">Sport</option> |
| 83 | + <option value="23">History</option> |
| 84 | + <option value="24">Politics</option> |
| 85 | + </select> |
| 86 | + </section> |
| 87 | + <section className="form-section"> |
| 88 | + <label htmlFor="difficulty">Select Difficulty</label> |
| 89 | + <br /> |
| 90 | + <select |
| 91 | + name="difficulty" |
| 92 | + id="difficulty" |
| 93 | + value={difficulty} |
| 94 | + onChange={(e) => setDifficulty(e.target.value)} |
| 95 | + > |
| 96 | + <option value="easy">Easy</option> |
| 97 | + <option value="normal">Normal</option> |
| 98 | + <option value="hard">Hard</option> |
| 99 | + </select> |
| 100 | + </section> |
| 101 | + <section className="form-section"> |
| 102 | + <button className="start-btn" onClick={() => startQuiz()}> |
| 103 | + Start Quiz |
| 104 | + </button> |
| 105 | + {isError && <p className="error-txt">An Error Occured.</p>} |
| 106 | + </section> |
| 107 | + </form> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return <Quiz questions={questions} setIsStart={setIsStart} />; |
| 114 | +}; |
| 115 | + |
| 116 | +export default SetupQuiz; |
0 commit comments