-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linked the frontend with the backend
- Loading branch information
Showing
13 changed files
with
663 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { useFetchQuestion } from '../hooks/FetchQuestion' | ||
import { updateAnswer } from '../hooks/helpers' | ||
|
||
function Question({ onSelected }) { | ||
const [selected ,setSelected] = useState(undefined) | ||
const state = useSelector(state => state) | ||
const { queus , trace } = state.questions | ||
const score = state.result.score | ||
// eslint-disable-next-line | ||
const [{isLoading, apiData, serverError}] = useFetchQuestion() // const [getdata,setGetData] = useFetchQuestion() , but we destructered and we don't need setGetData | ||
const dispatch = useDispatch() | ||
|
||
useSelector(state=> console.log(state)) | ||
|
||
useEffect(()=>{ | ||
// console.log(` the state ${state}`) | ||
dispatch(updateAnswer({trace,selected})) | ||
console.log({trace,selected}) | ||
},[selected]) | ||
|
||
const onSelect = (value) => { | ||
setSelected(value) // this is wrong and caused infinite loop | ||
// console.log(selected) | ||
// pushAnswer(selected) | ||
onSelected(value) | ||
dispatch(updateAnswer({trace,selected})) | ||
} | ||
const question = queus[trace] // we want to set the question from the state and the trace is the indicator to access that array to we know which question to render on screen based on click on next and prv buttons | ||
|
||
|
||
if (isLoading) return <h3 className='text-light'>Loading...</h3> | ||
if (serverError) return <h3 className='text-light'>{serverError || 'Error occured'}</h3> | ||
return ( | ||
<div className="questions"> | ||
<h2 key={question?.id} className="text-light"> | ||
{question?.question} | ||
</h2> | ||
|
||
<ul> | ||
{question?.options.map((q, i) => ( | ||
<li key={i} | ||
// onClick={e=>{onSelect(i)}} | ||
> | ||
<input | ||
type="radio" | ||
name="options" | ||
id={`q${i}-option`} | ||
value={false} | ||
onChange={()=>onSelect(i)} | ||
/> | ||
<label htmlFor={`q${i}-option`} className="text-primary"> | ||
{q} | ||
</label> | ||
{/* <div className={`check ${score[trace] == i? 'checked' : "" }`}></div> */} | ||
<div className={`check`}></div> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { useFetchQuestion } from '../hooks/FetchQuestion' | ||
import { updateAnswer } from '../hooks/helpers' | ||
|
||
function Question({ onSelected }) { | ||
const [selected ,setSelected] = useState(undefined) | ||
const state = useSelector(state => state) | ||
const { queus , trace } = state.questions | ||
const score = state.result.score | ||
// eslint-disable-next-line | ||
const [{isLoading, apiData, serverError}] = useFetchQuestion() // const [getdata,setGetData] = useFetchQuestion() , but we destructered and we don't need setGetData | ||
const dispatch = useDispatch() | ||
|
||
useSelector(state=> console.log(state)) | ||
|
||
useEffect(()=>{ | ||
// console.log(` the state ${state}`) | ||
dispatch(updateAnswer({trace,selected})) | ||
console.log({trace,selected}) | ||
},[selected]) | ||
|
||
const onSelect = (value) => { | ||
setSelected(value) // this is wrong and caused infinite loop | ||
// console.log(selected) | ||
// pushAnswer(selected) | ||
onSelected(value) | ||
dispatch(updateAnswer({trace,selected})) | ||
} | ||
const question = queus[trace] // we want to set the question from the state and the trace is the indicator to access that array to we know which question to render on screen based on click on next and prv buttons | ||
|
||
|
||
if (isLoading) return <h3 className='text-light'>Loading...</h3> | ||
if (serverError) return <h3 className='text-light'>{serverError || 'Error occured'}</h3> | ||
return ( | ||
<div className="questions"> | ||
<h2 key={question?.id} className="text-light"> | ||
{question?.question} | ||
</h2> | ||
|
||
<ul> | ||
{question?.options.map((q, i) => ( | ||
<li key={i} | ||
// onClick={e=>{onSelect(i)}} | ||
> | ||
<input | ||
type="radio" | ||
name="options" | ||
id={`q${i}-option`} | ||
value={false} | ||
onChange={()=>onSelect(i)} | ||
/> | ||
<label htmlFor={`q${i}-option`} className="text-primary"> | ||
{q} | ||
</label> | ||
{/* <div className={`check ${score[trace] == i? 'checked' : "" }`}></div> */} | ||
<div className={`check`}></div> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
export default Question |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,71 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import Question from './Question' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { paginateNextQuestion, paginatePrevQuestion, pushAnswer } from '../hooks/helpers' | ||
import { Navigate } from 'react-router-dom' | ||
|
||
const Quiz = () => { | ||
|
||
const [answer, setAnswer] = useState(undefined) | ||
const state = useSelector(state => state) | ||
const score = useSelector(state => state.result.score) | ||
const dispatch = useDispatch() | ||
|
||
const { queus , trace } = state.questions | ||
|
||
// useEffect(()=>{ | ||
// console.log('the score is:',score) | ||
// // console.log(questions,result) | ||
// // console.log(apiData) | ||
|
||
// }) | ||
|
||
const onPrev = () => { | ||
console.log('prev button clicked') | ||
if(trace > 0) { | ||
dispatch(paginatePrevQuestion()) | ||
} | ||
|
||
} | ||
const onNext = () => { | ||
console.log('next button clicked') | ||
if ( queus.length > trace && answer !== undefined) { | ||
dispatch(paginateNextQuestion()) | ||
if(trace >= score.length){ | ||
dispatch(pushAnswer(answer)) | ||
} | ||
} | ||
|
||
// setAnswer(undefined) | ||
} | ||
|
||
const onSelected = (ans) => { | ||
// console.log(ans) | ||
setAnswer(ans) | ||
} | ||
|
||
// user reached the end of the questions | ||
if (score.length > 0){ | ||
if(score && score.length >= queus.length ){ | ||
return <Navigate to={'/result'} replace={true}></Navigate> | ||
} | ||
} | ||
|
||
return ( | ||
<div className='container'> | ||
<h2 className='title text-light'>Quiz Application</h2> | ||
|
||
{/* The Questions here */} | ||
<Question onSelected={onSelected}/> | ||
|
||
<div className='grid'> | ||
{trace > 0 ? <button className='btn prev' onClick={onPrev}>Prev</button> | ||
: <div></div>} | ||
<button className='btn next' onClick={onNext}>Next</button> | ||
</div> | ||
|
||
</div> | ||
) | ||
} | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import Question from './Question' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { paginateNextQuestion, paginatePrevQuestion, pushAnswer } from '../hooks/helpers' | ||
import { Navigate } from 'react-router-dom' | ||
|
||
const Quiz = () => { | ||
|
||
const [answer, setAnswer] = useState(undefined) | ||
const state = useSelector(state => state) | ||
const score = useSelector(state => state.result.score) | ||
const dispatch = useDispatch() | ||
|
||
const { queus , trace } = state.questions | ||
|
||
// useEffect(()=>{ | ||
// console.log('the score is:',score) | ||
// // console.log(questions,result) | ||
// // console.log(apiData) | ||
|
||
// }) | ||
|
||
const onPrev = () => { | ||
console.log('prev button clicked') | ||
if(trace > 0) { | ||
dispatch(paginatePrevQuestion()) | ||
} | ||
|
||
} | ||
const onNext = () => { | ||
console.log('next button clicked') | ||
if ( queus.length > trace && answer !== undefined) { | ||
dispatch(paginateNextQuestion()) | ||
if(trace >= score.length){ | ||
dispatch(pushAnswer(answer)) | ||
} | ||
} | ||
|
||
// setAnswer(undefined) | ||
} | ||
|
||
const onSelected = (ans) => { | ||
// console.log(ans) | ||
setAnswer(ans) | ||
} | ||
|
||
// user reached the end of the questions | ||
if (score.length > 0){ | ||
if(score && score.length >= queus.length ){ | ||
return <Navigate to={'/result'} replace={true}></Navigate> | ||
} | ||
} | ||
|
||
return ( | ||
<div className='container'> | ||
<h2 className='title text-light'>Quiz Application</h2> | ||
|
||
{/* The Questions here */} | ||
<Question onSelected={onSelected}/> | ||
|
||
<div className='grid'> | ||
{trace > 0 ? <button className='btn prev' onClick={onPrev}>Prev</button> | ||
: <div></div>} | ||
<button className='btn next' onClick={onNext}>Next</button> | ||
</div> | ||
|
||
</div> | ||
) | ||
} | ||
|
||
export default Quiz |
Oops, something went wrong.