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 #18

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
97 changes: 56 additions & 41 deletions src/pages/Home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
import React, { useContext, useState } from 'react'
import Form from '../../components/Form/Form'
import QuizArea from '../QuizArea/QuizArea'
import quizContext from '../../context/quizContext'
# Home Component: UI Enhancement and Optimization

## Changes Made:

### Chakra UI Integration
- Integrated Chakra UI components such as `Container`, `Divider`, and adjusted text styling to enhance the UI.

### Loader Optimization
- Ensured proper positioning of the loading spinner and adjusted styling for better visibility during loading.

### Code Readability
- Improved code structure, added comments for better readability, and optimized component organization.

### Responsive Design
- Applied Chakra UI's responsive styles and adjusted container padding for improved alignment and UI consistency.

---

```javascript
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, Container, Divider } 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 handleSubmit = (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;
setUrl(`https://opentdb.com/api.php?amount=${number}&category=${category}&difficulty=${difficulty}&type=${type}`);
fetchQuestions(url);
setLoading(true);
};

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

return (
<>
<div className="d-flex justify-content-center align-items-center">
<HashLoader
color={'#3585c1'}
loading={loading}
size={100}
aria-label="Loading Spinner"
data-testid="loader"
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>
<hr />
<Form handleSubmit={handleSubmit} onChange={onChange} />
</div>
:
<QuizArea />
}
</>
)
}

export default Home
<Container maxW="lg" py="6">
{loading && (
<div style={{ position: 'relative', minHeight: '200px' }}>
<HashLoader color="#3585c1" loading={loading} size={100} />
</div>
)}
{!url || questions.length === 0 ? (
<div>
<Text fontSize="2xl" mb="4">
Start your Quiz Now
</Text>
<Divider mb="4" />
<Form handleSubmit={handleSubmit} onChange={onChange} />
</div>
) : (
<QuizArea />
)}
</Container>
);
};

export default Home;