Skip to content

Commit

Permalink
QF-613-word-by-word-in-settings-dynamic-list (#2185)
Browse files Browse the repository at this point in the history
* change static list to dynamic list

* address PR comments

* Use lodash/uniqBy

* remove util function and test cases

* fix loading and error states

* remove unused imports

---------

Co-authored-by: Osama Sayed <[email protected]>
  • Loading branch information
AhmedCodeGuy and osamasayed authored Jul 7, 2024
1 parent a43a78d commit eed82f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
ReciterResponse,
TafsirContentResponse,
PagesLookUpResponse,
WordByWordTranslationsResponse,
} from 'types/ApiResponses';
import AudioData from 'types/AudioData';
import { MushafLines, QuranFont } from 'types/QuranReader';
Expand Down Expand Up @@ -100,11 +101,11 @@ export const getAvailableTranslations = async (language: string): Promise<Transl
*
* @param {string} language we use this to get translated names of authors in specific the current language.
*
* @returns {Promise<TranslationsResponse>}
* @returns {Promise<WordByWordTranslationsResponse>}
*/
export const getAvailableWordByWordTranslations = async (
language: string,
): Promise<TranslationsResponse> => fetcher(makeWordByWordTranslationsUrl(language));
): Promise<WordByWordTranslationsResponse> => fetcher(makeWordByWordTranslationsUrl(language));

/**
* Get the current available languages with the name translated in the current language.
Expand Down
45 changes: 28 additions & 17 deletions src/components/Navbar/SettingsDrawer/WordByWordSection.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable i18next/no-literal-string */
/* eslint-disable max-lines */
import React from 'react';

import { Action } from '@reduxjs/toolkit';
import uniqBy from 'lodash/uniqBy';
import { useRouter } from 'next/router';
import Trans from 'next-translate/Trans';
import useTranslation from 'next-translate/useTranslation';
Expand All @@ -11,17 +11,18 @@ import { shallowEqual, useSelector } from 'react-redux';
import Section from './Section';
import styles from './WordByWordSection.module.scss';

import DataFetcher from '@/components/DataFetcher';
import Counter from '@/dls/Counter/Counter';
import Checkbox from '@/dls/Forms/Checkbox/Checkbox';
import Select, { SelectSize } from '@/dls/Forms/Select';
import Link, { LinkVariant } from '@/dls/Link/Link';
import Separator from '@/dls/Separator/Separator';
import usePersistPreferenceGroup from '@/hooks/auth/usePersistPreferenceGroup';
import {
setSelectedWordByWordLocale,
selectReadingPreferences,
setWordByWordDisplay,
setSelectedWordByWordLocale,
setWordByWordContentType,
setWordByWordDisplay,
setWordClickFunctionality,
} from '@/redux/slices/QuranReader/readingPreferences';
import {
Expand All @@ -31,19 +32,15 @@ import {
increaseWordByWordFontScale,
selectWordByWordFontScale,
} from '@/redux/slices/QuranReader/styles';
import { WordByWordTranslationsResponse } from '@/types/ApiResponses';
import QueryParam from '@/types/QueryParam';
import { WordByWordDisplay, WordByWordType, WordClickFunctionality } from '@/types/QuranReader';
import { makeWordByWordTranslationsUrl } from '@/utils/apiPaths';
import { removeItemFromArray } from '@/utils/array';
import { logValueChange } from '@/utils/eventLogger';
import { getLocaleName } from '@/utils/locale';
import PreferenceGroup from 'types/auth/PreferenceGroup';

export const WBW_LOCALES = ['en', 'ur', 'id', 'bn', 'tr', 'fa', 'hi', 'ta', 'inh'];
export const WORD_BY_WORD_LOCALES_OPTIONS = WBW_LOCALES.map((locale) => ({
label: getLocaleName(locale),
value: locale,
}));

const WordByWordSection = () => {
const { t, lang } = useTranslation('common');
const {
Expand Down Expand Up @@ -218,14 +215,28 @@ const WordByWordSection = () => {
<Separator className={styles.separator} />
<Section.Row>
<Section.Label>{t('trans-lang')}</Section.Label>
<Select
size={SelectSize.Small}
id="wordByWord"
name="wordByWord"
options={WORD_BY_WORD_LOCALES_OPTIONS}
value={wordByWordLocale}
disabled={shouldDisableLanguageSelect}
onChange={onWordByWordLocaleChange}
<DataFetcher
queryKey={makeWordByWordTranslationsUrl(lang)}
render={(data: WordByWordTranslationsResponse) => {
const uniqueData = uniqBy(data.wordByWordTranslations, 'isoCode');

const options = uniqueData.map(({ isoCode }) => ({
label: getLocaleName(isoCode),
value: isoCode,
}));

return (
<Select
size={SelectSize.Small}
id="wordByWord"
name="wordByWord"
options={options}
value={wordByWordLocale}
disabled={shouldDisableLanguageSelect}
onChange={onWordByWordLocaleChange}
/>
);
}}
/>
</Section.Row>
<Section.Footer>
Expand Down
9 changes: 8 additions & 1 deletion types/AvailableWordByWordTranslation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import AvailableTranslation from './AvailableTranslation';
import TranslatedName from './TranslatedName';

interface AvailableWordByWordTranslation extends AvailableTranslation {
isoCode?: string;
id: number;
name: string;
authorName: string;
slug: string;
languageName: string;
isoCode: string;
translatedName: TranslatedName;
}
export default AvailableWordByWordTranslation;

0 comments on commit eed82f7

Please sign in to comment.