Skip to content

Commit

Permalink
linked the frontend with the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilvny authored Feb 17, 2023
1 parent a2437ad commit 3419ce7
Show file tree
Hide file tree
Showing 13 changed files with 663 additions and 598 deletions.
128 changes: 64 additions & 64 deletions src/components/Question.jsx
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
140 changes: 70 additions & 70 deletions src/components/Quiz.jsx
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
Loading

0 comments on commit 3419ce7

Please sign in to comment.