-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Group Ayah/Translation of Q&A (#2284)
* Group Ayah/Translation of Q&A * Keep code DRY
- Loading branch information
1 parent
70c4a41
commit 40609e4
Showing
6 changed files
with
155 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
gap: var(--spacing-small); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* eslint-disable unicorn/no-array-reduce */ | ||
import { useMemo } from 'react'; | ||
|
||
import styles from './GroupedVerseAndTranslation.module.scss'; | ||
import PlainVerseText from './PlainVerseText'; | ||
|
||
import Error from '@/components/Error'; | ||
import TranslationText from '@/components/QuranReader/TranslationView/TranslationText'; | ||
import Spinner from '@/dls/Spinner/Spinner'; | ||
import useVerseAndTranslation from '@/hooks/useVerseAndTranslation'; | ||
import { getVerseWords } from '@/utils/verse'; | ||
|
||
interface Props { | ||
chapter: number; | ||
from: number; | ||
to: number; | ||
} | ||
|
||
const GroupedVerseAndTranslation: React.FC<Props> = (props) => { | ||
const { data, error, mutate, translations, translationFontScale } = useVerseAndTranslation(props); | ||
|
||
const allWords = useMemo(() => { | ||
if (!data?.verses) return []; | ||
return data.verses.reduce((acc, verse) => { | ||
return [...acc, ...getVerseWords(verse)]; | ||
}, []); | ||
}, [data?.verses]); | ||
|
||
const groupedTranslations = useMemo(() => { | ||
if (!data?.verses) return {}; | ||
return translations.reduce<Record<number, any>>((acc, translationId: number) => { | ||
const texts = data.verses.reduce((textsAcc, verse) => { | ||
const translation = verse.translations?.find( | ||
(t) => String(t.resourceId) === String(translationId), | ||
); | ||
return translation ? [...textsAcc, translation] : textsAcc; | ||
}, []); | ||
|
||
return { ...acc, [translationId]: texts }; | ||
}, {}); | ||
}, [data?.verses, translations]); | ||
|
||
if (error) return <Error error={error} onRetryClicked={mutate} />; | ||
if (!data) return <Spinner />; | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<div className={styles.arabicSection}> | ||
<div className={styles.arabicVerseContainer}> | ||
<PlainVerseText words={allWords} /> | ||
</div> | ||
</div> | ||
|
||
<div className={styles.translationsSection}> | ||
{translations.map((translationId: number) => { | ||
const translationGroup = groupedTranslations[translationId]; | ||
if (!translationGroup?.length) return null; | ||
|
||
const firstTranslation = translationGroup[0]; | ||
return ( | ||
<div key={`translation-${translationId}`} className={styles.translationGroup}> | ||
<div className={styles.translationContainer}> | ||
<TranslationText | ||
languageId={firstTranslation.languageId} | ||
resourceName={firstTranslation.resourceName} | ||
translationFontScale={translationFontScale} | ||
text={translationGroup | ||
.map((t, index) => | ||
data.verses.length > 1 | ||
? `${t.text} (${data.verses[index].verseNumber})` | ||
: t.text, | ||
) | ||
.join(' ')} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default GroupedVerseAndTranslation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import useTranslation from 'next-translate/useTranslation'; | ||
import { shallowEqual, useSelector } from 'react-redux'; | ||
import useSWR from 'swr/immutable'; | ||
|
||
import useQcfFont from '@/hooks/useQcfFont'; | ||
import { selectQuranReaderStyles } from '@/redux/slices/QuranReader/styles'; | ||
import { selectSelectedTranslations } from '@/redux/slices/QuranReader/translations'; | ||
import { getDefaultWordFields, getMushafId } from '@/utils/api'; | ||
import { makeVersesUrl } from '@/utils/apiPaths'; | ||
import { areArraysEqual } from '@/utils/array'; | ||
import { fetcher } from 'src/api'; | ||
import { VersesResponse } from 'types/ApiResponses'; | ||
|
||
interface Props { | ||
chapter: number; | ||
from: number; | ||
to: number; | ||
} | ||
|
||
const useVerseAndTranslation = ({ chapter, from, to }: Props) => { | ||
const { lang } = useTranslation(); | ||
const translations = useSelector(selectSelectedTranslations, areArraysEqual); | ||
const { quranFont, mushafLines, translationFontScale } = useSelector( | ||
selectQuranReaderStyles, | ||
shallowEqual, | ||
); | ||
|
||
const mushafId = getMushafId(quranFont, mushafLines).mushaf; | ||
const apiParams = { | ||
...getDefaultWordFields(quranFont), | ||
translationFields: 'resource_name,language_id', | ||
translations: translations.join(','), | ||
mushaf: mushafId, | ||
from: `${chapter}:${from}`, | ||
to: `${chapter}:${to}`, | ||
}; | ||
|
||
const shouldFetchData = !!from; | ||
|
||
const { data, error, mutate } = useSWR<VersesResponse>( | ||
shouldFetchData ? makeVersesUrl(chapter, lang, apiParams) : null, | ||
fetcher, | ||
); | ||
|
||
useQcfFont(quranFont, data?.verses ? data.verses : []); | ||
|
||
return { | ||
data, | ||
error, | ||
mutate, | ||
translations, | ||
translationFontScale, | ||
quranFont, | ||
}; | ||
}; | ||
|
||
export default useVerseAndTranslation; |