ready to play

This commit is contained in:
2023-06-03 00:22:29 +02:00
parent fa20932e18
commit 4114ccb678
22 changed files with 29784 additions and 1 deletions
+34
View File
@@ -0,0 +1,34 @@
import { shuffleArray } from './Utils';
import { QuestionType } from './Types';
export const fetchQuizQuestions = async (
amount: number,
category: number,
difficulty: string
) => {
try {
let anyCategory: string = category === 1 ? '' : '&category=' + category;
const endpoint = `https://opentdb.com/api.php?amount=${amount}${anyCategory}&difficulty=${difficulty}&type=multiple`;
const data = await (await fetch(endpoint)).json();
return data.results.map((question: QuestionType) => ({
...question,
answers: shuffleArray([
...question.incorrect_answers,
question.correct_answer
])
}));
} catch (error) {
console.log(error);
}
};
export const fetchCategories = async () => {
try {
const endpoint = `https://opentdb.com/api_category.php`;
const response = await fetch(endpoint);
const categories = await response.json();
return categories.trivia_categories;
} catch (error) {
console.log(error);
}
};