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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const QuestionsButton: React.FC<Props> = ({ verseKey, onActionTriggered }) => {
const { t, lang } = useTranslation('quran-reader');
const router = useRouter();
const [isContentModalOpen, setIsContentModalOpen] = useState(false);
const hasQuestions = pageQuestionsCount && pageQuestionsCount[verseKey] > 0;
const hasQuestions = pageQuestionsCount && pageQuestionsCount[verseKey]?.total > 0;
const onButtonClicked = (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
e.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createContext, useContext } from 'react';

export const PageQuestionsContext = createContext<Record<string, number> | undefined>(undefined);
import { QuestionData } from '@/utils/auth/api';

export const PageQuestionsContext = createContext<Record<string, QuestionData> | undefined>(
undefined,
);

export const usePageQuestions = () => {
const context = useContext(PageQuestionsContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ const TranslationPageVerse: React.FC<TranslationPageVerse> = ({
};
}, [isLastVerseInView, verse, verseKeysQueue]);

const hasQuestions = questionsCount && questionsCount[verse.verseKey] > 0;
const hasNotes = notesCount && notesCount[verse.verseKey] > 0;
const hasQuestions = questionsCount?.[verse.verseKey]?.total > 0;
const hasNotes = notesCount?.[verse.verseKey] > 0;

return (
<div
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/auth/useCountRangeQuestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useTranslation from 'next-translate/useTranslation';
import useSWRImmutable from 'swr/immutable';

import Language from '@/types/Language';
import { countQuestionsWithinRange } from '@/utils/auth/api';
import { countQuestionsWithinRange, QuestionData } from '@/utils/auth/api';
import { makeCountQuestionsWithinRangeUrl } from '@/utils/auth/apiPaths';

type Range = {
Expand All @@ -11,18 +11,18 @@ type Range = {
};

type CountRangeQuestionsResponse = {
data: Record<string, number>;
data: Record<string, QuestionData>;
isLoading: boolean;
error: Error | null;
};

const useCountRangeQuestions = (questionsRange: Range): CountRangeQuestionsResponse => {
const { lang } = useTranslation();
const { data, isValidating, error } = useSWRImmutable<Record<string, number>>(
const { data, isValidating, error } = useSWRImmutable<Record<string, QuestionData>>(
questionsRange
? makeCountQuestionsWithinRangeUrl(questionsRange.from, questionsRange.to, lang as Language)
: null,
async (): Promise<Record<string, number>> => {
async (): Promise<Record<string, QuestionData>> => {
return countQuestionsWithinRange(questionsRange.from, questionsRange.to, lang as Language);
},
);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/auth/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,16 @@ export const addCollection = async (collectionName: string) => {
return postRequest(makeAddCollectionUrl(), { name: collectionName });
};

export type QuestionData = {
types: Record<string, number>;
total: number;
};

export const countQuestionsWithinRange = async (
from: string,
to: string,
language: Language,
): Promise<Record<string, number>> => {
): Promise<Record<string, QuestionData>> => {
return privateFetcher(makeCountQuestionsWithinRangeUrl(from, to, language));
};

Expand Down
Loading