Skip to content

Commit

Permalink
managed the state through redux to render the question page and creat…
Browse files Browse the repository at this point in the history
…ed selectors for fetching the data from the state
  • Loading branch information
vasantisuthar committed May 22, 2022
1 parent afcec42 commit 2b18fbe
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 54 deletions.
5 changes: 3 additions & 2 deletions src/plays/quizeo/src/Quizeo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useEffect } from 'react';
import { setScore, setQuestionNo } from "./redux/questions/questions-action";
import Directory from "./components/directory/directory.component";
import Header from "./components/header/header.component";
import { Provider } from 'react-redux';
import { Provider} from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import './quizeo.css'

Expand All @@ -18,6 +18,7 @@ function Quizeo(props) {

// Your Code Start below.


useEffect(() =>{
store.dispatch(setScore());
store.dispatch(setQuestionNo());
Expand All @@ -32,7 +33,7 @@ function Quizeo(props) {
<PersistGate persistor={persistor}>
<Header/>
<Directory/>
</PersistGate>
</PersistGate>
</Provider>
{/* Your Code Ends Here */}

Expand Down
37 changes: 23 additions & 14 deletions src/plays/quizeo/src/components/directory/directory.component.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import React from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectDirectorySection } from "../../redux/directory/directory-selectors";
import MoviePage from "../../pages/moviePage/moviePage.component";
import { useSelector} from 'react-redux';
import MenuItem from "../menu-item/menuItem.component";
import { selectQuestionIndex, selectClickedAction} from "../../redux/movie/movieSelector";

import './directory.styles.css';
const Directory = ({sections}) => {
return (
<div className="directoryMenu">
{sections.map(({id, ...otherSectionComponent}) => (
<MenuItem key={id} {...otherSectionComponent}/>
))}
</div>
const Directory = () => {

let questionIndex = useSelector(selectQuestionIndex);
let isClick = useSelector(selectClickedAction);
const sections = useSelector(selectDirectorySection);

return (
<div>
{
isClick ? <MoviePage categoryId={questionIndex}/> :
<div className="directoryMenu">
{sections.map(({id, ...otherSectionComponent}) => (
<MenuItem key={id} {...otherSectionComponent}/>
))}
</div>
}
</div>
);
}

const mapStateToProps = createStructuredSelector({
sections: selectDirectorySection
})
export default connect(mapStateToProps)(Directory);

export default Directory;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.directoryMenu{
display: flex;
margin: 3rem;
display: flex;
flex-direction: row;
Expand Down
9 changes: 4 additions & 5 deletions src/plays/quizeo/src/components/header/header.component.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import './header.styles.css';
import { isClicked } from '../../redux/questions/questions-action';

const Header = () => {

let dispatch = useDispatch();
return (
<div>
<Link to="/plays/quizeo">
<h1 className="header">Quizeo</h1>
</Link>
<h1 className="header" onClick={() => dispatch(isClicked())}>Quizeo</h1>
</div>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/plays/quizeo/src/components/header/header.styles.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.header{
cursor: pointer;
margin-top: 1.5rem;
margin-bottom: 3.5rem;
font-size: 3rem;
Expand Down
24 changes: 7 additions & 17 deletions src/plays/quizeo/src/components/menu-item/menuItem.component.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import React, { useState } from "react";
import MoviePage from "../../pages/moviePage/moviePage.component";
import React from "react";
import { useDispatch} from "react-redux"
import { displayQuestionComponent, isClicked } from "../../redux/questions/questions-action";

import './menu-item.styles.css'

const MenuItem = ({title, imageUrl, linkUrl}) => {
const [isClicked, setIsClicked] = useState(false);

const handleClick = () =>{
setIsClicked(true);
}

const handleLocation = () => {
if(window.location.pathname === '/plays/quizeo/movie'){
return <MoviePage categoryId={linkUrl}/>
}
}

const dispatch = useDispatch();

return (
<div className="menu-item">
<div className="image-container" style={{backgroundImage:`url(${imageUrl})`}}/>
<div className="image-content"
onClick={handleClick}>
{isClicked ? handleLocation(): null}
onClick={() => {dispatch(displayQuestionComponent(linkUrl)); dispatch(isClicked())}}>
<h1>{title}</h1>
</div>
</div>
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { useSelector, useDispatch } from "react-redux";
import { ScoreModel } from "../../pages/score-page/score.component";
import { addScore,showModel } from "../../redux/questions/questions-action";
import { increaseQuestionNo } from "../../redux/questions/questions-action";
import { selectQuestionNo, selectHiddenState } from "../../redux/movie/movieSelector";

import './questions.styles.css';

const Questions = (collection) => {
const counter = useSelector((state) => state.movie.questionNo);
const hidden = useSelector((state) => state.movie.hidden);
const counter = useSelector(selectQuestionNo);
const hidden = useSelector(selectHiddenState);
const dispatch = useDispatch();
let obj;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

.question-model{
margin-top:-4%;
width: 75%;
width: 50%;
}

.category-title{
Expand Down
3 changes: 1 addition & 2 deletions src/plays/quizeo/src/pages/moviePage/moviePage.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import React from "react";
import { useSelector } from 'react-redux'
import { selectMovieDataSection } from "../../redux/movie/movieSelector";
import Questions from "../../components/questions/questions.component";
import Header from "../../components/header/header.component";

import '../../quizeo.css'

const MoviePage = ({categoryId}) => {
console.log(categoryId);
const collection = useSelector(selectMovieDataSection(categoryId))
return (
<div>
<Header/>
<Questions collection={collection} />
</div>
);
Expand Down
9 changes: 4 additions & 5 deletions src/plays/quizeo/src/pages/score-page/score.component.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from "react"
import { useSelector, useDispatch} from "react-redux"
import { showModel,setScore, setQuestionNo } from "../../redux/questions/questions-action";
import { useNavigate } from "react-router-dom";
import { showModel,setScore, setQuestionNo, isClicked } from "../../redux/questions/questions-action";
import { selectScore } from "../../redux/movie/movieSelector";
import './score.styles.css';

export const ScoreModel = () => {
var score = useSelector((state) => state.movie.score)
const navigate = useNavigate();
var score = useSelector(selectScore);
const dispatch = useDispatch();
return(
<div className="score-container">
Expand All @@ -16,8 +15,8 @@ export const ScoreModel = () => {
<button className="play-button"
onClick={() => {
dispatch(showModel());
navigate('/plays/quizeo')
dispatch(setScore());
dispatch(isClicked());
dispatch(setQuestionNo());
}}>Play again</button>
</div>
Expand Down
15 changes: 14 additions & 1 deletion src/plays/quizeo/src/redux/movie/movie-reducer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import movie_data from "../../data/movie.data";
import { QuestionTypes } from "../questions/question-types";
import { calculateScore } from "../questions/question.utils";

const INITIAL_STATE = {
collection:movie_data,
hidden: true,
questionNo : 0,
score:0
score:0,
questionIndex : '',
setClicked: false
}

const movieReducer = (state = INITIAL_STATE,action) =>{
Expand Down Expand Up @@ -35,6 +38,16 @@ const movieReducer = (state = INITIAL_STATE,action) =>{
...state,
questionNo: 0
}
case QuestionTypes.DISPLAY_QUESTION_COMPONENT:
return{
...state,
questionIndex: action.payload
}
case QuestionTypes.IS_CLICKED:
return{
...state,
setClicked: !state.setClicked
}
default:
return {...state}
}
Expand Down
25 changes: 25 additions & 0 deletions src/plays/quizeo/src/redux/movie/movieSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ import memoize from 'lodash.memoize';

const selectMovie = (state) => state.movie;

export const selectQuestionIndex = createSelector(
[selectMovie],
movie => movie.questionIndex
)

export const selectQuestionNo = createSelector(
[selectMovie],
movie => movie.questionNo
)

export const selectHiddenState = createSelector(
[selectMovie],
movie => movie.hidden
)

export const selectScore = createSelector(
[selectMovie],
movie => movie.score
)

export const selectClickedAction = createSelector(
[selectMovie],
movie => movie.setClicked
)

export const selectMovieData = createSelector(
[selectMovie],
movie => movie.collection
Expand Down
4 changes: 3 additions & 1 deletion src/plays/quizeo/src/redux/questions/question-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ export const QuestionTypes = {
ADD_SCORE : 'ADD_SCORE',
SHOW_MODEL : 'SHOW_MODEL',
SET_SCORE : 'SET_SCORE',
SET_QUESTION_NO: 'SET_QUESTION_NO'
SET_QUESTION_NO: 'SET_QUESTION_NO',
DISPLAY_QUESTION_COMPONENT : 'DISPLAY_QUESTION_COMPONENT',
IS_CLICKED: 'IS_CLICKED'
}
1 change: 0 additions & 1 deletion src/plays/quizeo/src/redux/questions/question.utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

export const calculateScore = (score, answer) =>{
// eslint-disable-next-line eqeqeq
if(answer.answer){
if(answer.answer === answer.correct){
score = score + 1
Expand Down
9 changes: 9 additions & 0 deletions src/plays/quizeo/src/redux/questions/questions-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ export const setScore = () => ({

export const setQuestionNo = () => ({
type:QuestionTypes.SET_QUESTION_NO
})

export const displayQuestionComponent = (linkUrl) =>({
type: QuestionTypes.DISPLAY_QUESTION_COMPONENT,
payload: linkUrl
})

export const isClicked = () => ({
type:QuestionTypes.IS_CLICKED
})
7 changes: 5 additions & 2 deletions src/plays/quizeo/src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createStore } from "redux";
import { createStore,applyMiddleware } from "redux";
import { persistStore } from "redux-persist";

import logger from "redux-logger";
import rootReducer from './root-reducer';

const store = createStore(rootReducer);
const middleware = [logger];

const store = createStore(rootReducer, applyMiddleware(...middleware));
const persistor = persistStore(store);

export {store, persistor};

0 comments on commit 2b18fbe

Please sign in to comment.