Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package-lock.json

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

68 changes: 46 additions & 22 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useState } from "react";
import { useState} from "react";
import ResultCard from "./components/ResultCard";
import QuestionCard from "./components/QuestionCard";
import { shuffleArray } from "./lib/utils";
import rawTriviaQuestion from "./lib/data";


// Since react rerenders on state change, I might not need to use useEffect and useState to wrap the card conditional block in a function.

const triviaQuestion = rawTriviaQuestion.results[0];

function App() {
Expand All @@ -16,36 +19,57 @@ function App() {

let card;

if (selectedAnswer) {
card = (
<ResultCard
correct={selectedAnswer === questionData.correct_answer}
answer={questionData.correct_answer}
/>
);
} else {
let options = [
questionData.correct_answer,
...questionData.incorrect_answers,
];
card = (
<QuestionCard
question={questionData.question}
options={shuffleArray(options)}
selectAnswer={selectAnswer}
/>
);

if (selectedAnswer) {
card = (
<ResultCard
correct={selectedAnswer === questionData.correct_answer}
answer={questionData.correct_answer}
/>
);
} else {
let tempArr = [...questionData.incorrect_answers];
console.log("Temp Arr", tempArr);
console.log(tempArr[0]);
let options = [questionData.correct_answer, ...tempArr];
card = (
<QuestionCard
question={questionData.question}
options={shuffleArray(options)}
selectAnswer={selectAnswer}
/>
);
}
function getNextQuestion() {
fetch("https://opentdb.com/api.php?amount=1&category=9&type=multiple")
.then((response) => {
if (response.status === 200) {
return response.json();
} else {
console.log(response.status);
}
})
.then((data) => {
setQuestionData(data.results[0]);
console.log("data", questionData);
setSelectedAnswer(false);
})
.catch((error) => {
console.log("Error:", error);
});
}

return (
<div className="w-100 my-5 d-flex justify-content-center align-items-center">
<div style={{ maxWidth: "45%" }}>
<h1 className="text-center">Trivia App</h1>
<button className="btn btn-success">Next Question</button>
<button className="btn btn-success" onClick={() => getNextQuestion()}>
Next Question
</button>
{card}
</div>
</div>
);
}

export default App;
export default App;