Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Home.jsx #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions src/pages/Home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import React, { useContext, useState } from 'react'
import Form from '../../components/Form/Form'
import QuizArea from '../QuizArea/QuizArea'
import quizContext from '../../context/quizContext'
import React, { useContext, useState } from 'react';
import Form from '../../components/Form/Form';
import QuizArea from '../QuizArea/QuizArea';
import quizContext from '../../context/quizContext';
import { HashLoader } from 'react-spinners';
import { Text } from '@chakra-ui/react'
import { Text } from '@chakra-ui/react';

const Home = () => {
const context = useContext(quizContext)
const { setUrl, url, fetchQuestions, setLoading, loading, questions } = context
const [formData, setFormData] = useState({ number: '', category: '', difficulty: '', type: '' })
const context = useContext(quizContext);
const { setUrl, url, fetchQuestions, setLoading, loading, questions } = context;
const [formData, setFormData] = useState({ number: '', category: '', difficulty: '', type: '' });
const [error, setError] = useState(null); // New: State for error handling

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();
const { number, category, difficulty, type } = formData
setUrl(`https://opentdb.com/api.php?amount=${number}&category=${category}&difficulty=${difficulty}&type=${type}`, fetchQuestions(url))
setLoading(true)
}
const { number, category, difficulty, type } = formData;
try {
await setUrl(`https://opentdb.com/api.php?amount=${number}&category=${category}&difficulty=${difficulty}&type=${type}`);
await fetchQuestions(url);
setLoading(true);
} catch (err) {
setError('Failed to fetch questions. Please try again.');
setLoading(false);
}
};

const onChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value })
}
setFormData({ ...formData, [e.target.name]: e.target.value });
};

return (
<>
Expand All @@ -33,19 +40,25 @@ const Home = () => {
style={{ backgroundColor: '#4d4d4dcc', width: '100%', height: '100vh', position: 'absolute', top: '13%' }}
/>
</div>
{
(url === '' || questions.length === 0)
?
<div className="container my-3">
<Text mb={'4'} fontSize='4xl'>Start your Quiz Now</Text>
<div className="container my-3">
{error ? (
<Text color="red.500" fontSize="lg">
{error}
</Text>
) : url === '' || questions.length === 0 ? (
<>
<Text mb={'4'} fontSize='4xl'>
Start your Quiz Now
</Text>
<hr />
<Form handleSubmit={handleSubmit} onChange={onChange} />
</div>
:
</>
) : (
<QuizArea />
}
)}
</div>
</>
)
}
);
};

export default Home
export default Home;