From 62d13939d470efacc9eb4160dab99e138ea1d5e4 Mon Sep 17 00:00:00 2001 From: dotslashf Date: Mon, 6 Nov 2023 20:27:30 +0800 Subject: [PATCH 1/2] feat: getVerseKeyById function and update quiz question format --- src/data/index.ts | 10 ++++++++++ src/quiz/guessSurah.ts | 11 +++++++++-- src/quiz/guessVerse.ts | 8 +++++++- src/quiz/helpers.ts | 4 ++-- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/data/index.ts b/src/data/index.ts index 601e091..9d8deab 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -61,6 +61,16 @@ export const getVerseTextById = (id: number) => { return find.text_uthmani; }; +export const getVerseKeyById = (id: number) => { + const find = getQuran().find((item) => item.id === id); + + if (!find) { + throw new Error("Failed to find verse"); + } + + return find.verse_key; +} + const chapterNames = [ "Al-Fatihah", "Al-Baqarah", diff --git a/src/quiz/guessSurah.ts b/src/quiz/guessSurah.ts index c6398a2..8780ff3 100644 --- a/src/quiz/guessSurah.ts +++ b/src/quiz/guessSurah.ts @@ -3,6 +3,7 @@ import { getManyVerseIdBySurahId, getSurahBySurahId, getSurahByVerseId, + getVerseKeyById, getVerseTextById, } from "~/data"; import { randomizeOptions } from "~/quiz/helpers"; @@ -48,7 +49,10 @@ const guessSurahBySurah = (props: GuessVerse) => { options = options.sort(() => 0.5 - Math.random()).slice(0, 3); return randomizeOptions({ - question: getVerseTextById(questionVerseId), + question: { + text: getVerseTextById(questionVerseId), + verseKey: getVerseKeyById(questionVerseId), + }, options: [ ...options.map((option) => { return { @@ -79,7 +83,10 @@ const guessSurahBySurah = (props: GuessVerse) => { }; type CreateQuizOutput = { - question: string; + question: { + text: string; + verseKey: string; + }; options: { text: string; value: number; diff --git a/src/quiz/guessVerse.ts b/src/quiz/guessVerse.ts index 4b5a166..00d01cc 100644 --- a/src/quiz/guessVerse.ts +++ b/src/quiz/guessVerse.ts @@ -4,6 +4,7 @@ import { getManyVerseIdBySurahId, getManyVerseIdByJuzId, getVerseTextById, + getVerseKeyById, } from "~/data"; import { getIndexOfQuestionAnswerOptions, @@ -126,17 +127,22 @@ const createGuessVerseQuiz = (props: CreateGuessVerseQuiz) => { const { questionVerseId, answerVerseId, optionsVerseId } = props; const result = { - question: getVerseTextById(questionVerseId), + question: { + text: getVerseTextById(questionVerseId), + verseKey: getVerseKeyById(questionVerseId), + }, options: [ ...optionsVerseId.map((option) => { return { text: getVerseTextById(option), value: 0, + verseKey: getVerseKeyById(option), }; }), { text: getVerseTextById(answerVerseId), value: 1, + verseKey: getVerseKeyById(answerVerseId), }, ], }; diff --git a/src/quiz/helpers.ts b/src/quiz/helpers.ts index a1d7a7c..47bed13 100644 --- a/src/quiz/helpers.ts +++ b/src/quiz/helpers.ts @@ -42,10 +42,10 @@ export const getIndexOfQuestionAnswerOptions = ( }; export const randomizeOptions = (result: { - question: string; + question: { text: string; verseKey: string }; options: { text: string; value: number }[]; }): { - question: string; + question: { text: string; verseKey: string }; options: { text: string; value: number }[]; } => { const randomizedOptions = [...result.options]; From 1c5f64a7147e3dad998f2cdcdeac34c494d4d6f7 Mon Sep 17 00:00:00 2001 From: dotslashf Date: Mon, 6 Nov 2023 20:31:09 +0800 Subject: [PATCH 2/2] docs: add verseKey to question object in API response --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3038d33..76ae3cc 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ When the `options.value` is set to 1, it means the answer is correct, while when { "data": [ { - "question": " قُلْ هُوَ ٱللَّهُ أَحَدٌ", + "question": { + "text": " قُلْ هُوَ ٱللَّهُ أَحَدٌ", + "verseKey": "112:1" + }, "options": [ { "text": "Al-Masad",