Skip to content

Commit

Permalink
Initialized project and got api working
Browse files Browse the repository at this point in the history
  • Loading branch information
j8ahmed committed Apr 3, 2021
1 parent 1b0790c commit 37089a0
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 110 deletions.
75 changes: 72 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.37",
"@types/react": "^17.0.3",
"@types/react-dom": "^17.0.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.2.3",
"web-vitals": "^1.1.1"
},
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/API/trivia_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@



export const testAPI = () => {
console.log("Hello")
}


export {}
38 changes: 0 additions & 38 deletions src/App.css

This file was deleted.

25 changes: 0 additions & 25 deletions src/App.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import QuestionCard from './components/QuestionCard'

function App() {
return (
<div className="App">

<h1>React Quiz</h1>
<QuestionCard />
<footer>
Built by Jamal J8
</footer>
</div>
);
}

export default App;
17 changes: 17 additions & 0 deletions src/components/Controls.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'

type Props = {
isAnswered: boolean,
nextExists: boolean,
next: () => void
}

const Controls = ({ isAnswered, nextExists, next }:Props) => {
return (
<div>
{isAnswered && nextExists && <button onClick={next}>Next Question</button>}
</div>
)
}

export default Controls
88 changes: 88 additions & 0 deletions src/components/QuestionCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React, { useEffect } from 'react'
import Controls from './Controls'
import QuestionSet from './QuestionSet'

const URL = "https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple"

enum QuizState { LOADING, COMPLETE, NOT_STARTED, QUESTION_ANSWERED }
type Question = {
category: string,
correct_answer: string,
difficulty: string,
incorrect_answers: string[],
question: string,
type: string,
}

const QuestionCard = () => {
const [quizState, setQuizState] = React.useState({
status: QuizState.NOT_STARTED
})
const [questionDeck, setQuestionDeck] = React.useState([] as Question[])
const [question, setQuestion] = React.useState({
id: 0,
question: "",
options: [""],
correctAnswer: "",
})

const getQuiz = async () => {
setQuizState( {status: QuizState.LOADING} )
// Initialize Quiz with data from API.
try{
const data = await (await fetch(URL)).json()
console.log(data)
setQuestionDeck(data.results)

const Q1: Question = data.results[0]
setQuestion({
id: 0,
question: Q1.question,
options: [...Q1.incorrect_answers, Q1.correct_answer],
correctAnswer: Q1.correct_answer
})
}
catch(e){
console.log(e)
throw e
}
}

useEffect(() => {
getQuiz()
}, [])

const selectAnswer = (ans: string): void => {
setQuizState({status: QuizState.QUESTION_ANSWERED})
}

const nextQuestion = () => {
//reset the quizState
setQuizState({status: QuizState.LOADING})
setQuestion( q => {
const id = q.id + 1
const data = questionDeck[id]
return {
id: id,
question: data.question,
options: [...data.incorrect_answers, data.correct_answer],
correctAnswer: data.correct_answer,
}
})
}

return (
<div className="question-card">
<QuestionSet
questionState={quizState.status === QuizState.QUESTION_ANSWERED}
selectAnswer={selectAnswer}
{...question}/>
<Controls
isAnswered={quizState.status === QuizState.QUESTION_ANSWERED}
nextExists={question.id + 2 <= questionDeck.length}
next={nextQuestion}/>
</div>
)
}

export default QuestionCard
Loading

0 comments on commit 37089a0

Please sign in to comment.