Skip to content

Commit 3c1fba9

Browse files
committed
Setup Form Structure
1 parent 5c0723c commit 3c1fba9

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

src/SetupForm.js

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,73 @@
1-
import React from 'react'
2-
import { useGlobalContext } from './context'
1+
import React from "react";
2+
import { useGlobalContext } from "./context";
33

44
const SetupForm = () => {
5-
return <h2>setup form</h2>
6-
}
5+
const { quiz, handleChange, handleSubmit, error } = useGlobalContext();
76

8-
export default SetupForm
7+
return (
8+
<main>
9+
<section className="quiz quiz-small">
10+
<form className="setup-form">
11+
<h2>setup quiz</h2>
12+
13+
{/* amount */}
14+
<div className="form-control">
15+
<label htmlFor="amount">number of questions</label>
16+
<input
17+
type="number"
18+
name="amount"
19+
id="amount"
20+
className="form-input"
21+
value={quiz.amount}
22+
onChange={handleChange}
23+
min={1}
24+
max={50}
25+
/>
26+
</div>
27+
28+
{/* category */}
29+
<div className="form-control">
30+
<label htmlFor="category">category</label>
31+
<select
32+
name="category"
33+
id="category"
34+
className="form-input"
35+
value={quiz.category}
36+
onChange={handleChange}
37+
>
38+
<option value="sports">sports</option>
39+
<option value="history">history</option>
40+
<option value="politics">politics</option>
41+
</select>
42+
</div>
43+
44+
{/* difficulty */}
45+
<div className="form-control">
46+
<label htmlFor="difficulty">select difficulty</label>
47+
<select
48+
name="difficulty"
49+
id="difficulty"
50+
className="form-input"
51+
value={quiz.difficulty}
52+
onChange={handleChange}
53+
>
54+
<option value="easy">easy</option>
55+
<option value="medium">history</option>
56+
<option value="hard">politics</option>
57+
</select>
58+
</div>
59+
{error && (
60+
<div className="error">
61+
can't generate questions, please try different options
62+
</div>
63+
)}
64+
<button className="submit-btn" onClick={handleSubmit}>
65+
play quiz
66+
</button>
67+
</form>
68+
</section>
69+
</main>
70+
);
71+
};
72+
73+
export default SetupForm;

src/context.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const AppProvider = ({ children }) => {
2323
const [index, setIndex] = useState(0);
2424
const [correct, setCorrect] = useState(0);
2525
const [error, setError] = useState(false);
26+
const [quiz, setQuiz] = useState({
27+
amount: 10,
28+
category: "sports",
29+
difficulty: "easy",
30+
});
2631
const [isModalOpen, setIsModalOpen] = useState(false);
2732

2833
const fetchQuestions = async (url) => {
@@ -76,9 +81,13 @@ const AppProvider = ({ children }) => {
7681
setIsModalOpen(false);
7782
};
7883

79-
useEffect(() => {
80-
fetchQuestions(tempUrl);
81-
}, []);
84+
const handleChange = (event) => {
85+
console.log(event);
86+
};
87+
88+
const handleSubmit = (event) => {
89+
event.preventDefault();
90+
};
8291

8392
return (
8493
<AppContext.Provider
@@ -93,6 +102,9 @@ const AppProvider = ({ children }) => {
93102
nextQuestion,
94103
checkAnswer,
95104
closeModal,
105+
quiz,
106+
handleChange,
107+
handleSubmit,
96108
}}
97109
>
98110
{children}

0 commit comments

Comments
 (0)