diff --git a/src/api.ts b/src/api.ts index 06f3f93786..be4b919a1d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,7 +11,6 @@ import { makeAudioTimestampsUrl, makeChapterAudioDataUrl, makeAvailableRecitersUrl, - makeSearchResultsUrl, makeTranslationsInfoUrl, makeTranslationsUrl, makeVersesUrl, @@ -31,10 +30,9 @@ import { } from '@/utils/apiPaths'; import generateSignature from '@/utils/auth/signature'; import { isStaticBuild } from '@/utils/build'; -import { SearchRequest, AdvancedCopyRequest, PagesLookUpRequest } from 'types/ApiRequests'; +import { AdvancedCopyRequest, PagesLookUpRequest } from 'types/ApiRequests'; import { TranslationsResponse, - SearchResponse, AdvancedCopyRawResultResponse, LanguagesResponse, RecitersResponse, @@ -244,15 +242,6 @@ export const getAdvancedCopyRawResult = async ( params: AdvancedCopyRequest, ): Promise => fetcher(makeAdvancedCopyUrl(params)); -/** - * Get the search results of a query. - * - * @param {SearchRequest} params - * @returns {Promise} - */ -export const getSearchResults = async (params: SearchRequest): Promise => - fetcher(makeSearchResultsUrl(params)); - /** * Get the search results of a query. * diff --git a/src/components/CommandBar/CommandBarBody/index.tsx b/src/components/CommandBar/CommandBarBody/index.tsx index e1c37cb843..f06cf31bdf 100644 --- a/src/components/CommandBar/CommandBarBody/index.tsx +++ b/src/components/CommandBar/CommandBarBody/index.tsx @@ -25,7 +25,7 @@ import { areArraysEqual } from '@/utils/array'; import { logButtonClick, logTextSearchQuery } from '@/utils/eventLogger'; import { getQuickSearchQuery } from '@/utils/search'; import { SearchResponse } from 'types/ApiResponses'; -import { SearchNavigationType } from 'types/SearchNavigationResult'; +import { SearchNavigationType } from 'types/Search/SearchNavigationResult'; const NAVIGATE_TO = [ { diff --git a/src/components/CommandBar/CommandsList/CommandPrefix/index.tsx b/src/components/CommandBar/CommandsList/CommandPrefix/index.tsx index 9375fee93e..6af1d9eda7 100644 --- a/src/components/CommandBar/CommandsList/CommandPrefix/index.tsx +++ b/src/components/CommandBar/CommandsList/CommandPrefix/index.tsx @@ -8,7 +8,7 @@ import styles from './CommandPrefix.module.scss'; import DataContext from '@/contexts/DataContext'; import NavigateIcon from '@/icons/east.svg'; import { getSearchNavigationResult } from '@/utils/search'; -import { SearchNavigationType } from 'types/SearchNavigationResult'; +import { SearchNavigationType } from 'types/Search/SearchNavigationResult'; interface Props { name: string; diff --git a/src/components/CommandBar/CommandsList/index.tsx b/src/components/CommandBar/CommandsList/index.tsx index 832b19f84c..40933c68c1 100644 --- a/src/components/CommandBar/CommandsList/index.tsx +++ b/src/components/CommandBar/CommandsList/index.tsx @@ -22,7 +22,7 @@ import { } from '@/redux/slices/CommandBar/state'; import { logButtonClick } from '@/utils/eventLogger'; import { resolveUrlBySearchNavigationType } from '@/utils/navigation'; -import { SearchNavigationResult } from 'types/SearchNavigationResult'; +import { SearchNavigationResult } from 'types/Search/SearchNavigationResult'; export interface Command extends SearchNavigationResult { group: string; diff --git a/src/components/Search/NavigationItem/index.tsx b/src/components/Search/NavigationItem/index.tsx index f81dd37f0b..53cfffa337 100644 --- a/src/components/Search/NavigationItem/index.tsx +++ b/src/components/Search/NavigationItem/index.tsx @@ -13,7 +13,7 @@ import { logButtonClick } from '@/utils/eventLogger'; import { toLocalizedNumber, toLocalizedVerseKey } from '@/utils/locale'; import { resolveUrlBySearchNavigationType } from '@/utils/navigation'; import { getSearchNavigationResult } from '@/utils/search'; -import { SearchNavigationResult, SearchNavigationType } from 'types/SearchNavigationResult'; +import { SearchNavigationResult, SearchNavigationType } from 'types/Search/SearchNavigationResult'; interface Props { navigation: SearchNavigationResult; @@ -24,7 +24,7 @@ interface Props { const NavigationItem: React.FC = ({ navigation, isSearchDrawer, - service = SearchService.QDC, + service = SearchService.KALIMAT, }) => { const { t, lang } = useTranslation('common'); const chaptersData = useContext(DataContext); diff --git a/src/components/Search/SearchResults/KalimatNavigationSearchResultItem.tsx b/src/components/Search/SearchResults/KalimatNavigationSearchResultItem.tsx index a55c91f4c9..ffaf9e3325 100644 --- a/src/components/Search/SearchResults/KalimatNavigationSearchResultItem.tsx +++ b/src/components/Search/SearchResults/KalimatNavigationSearchResultItem.tsx @@ -8,8 +8,8 @@ import styles from './SearchResultItem.module.scss'; import DataContext from '@/contexts/DataContext'; import Link from '@/dls/Link/Link'; +import { SearchNavigationType } from '@/types/Search/SearchNavigationResult'; import SearchService from '@/types/Search/SearchService'; -import { SearchNavigationType } from '@/types/SearchNavigationResult'; import SearchQuerySource from '@/types/SearchQuerySource'; import { getChapterData } from '@/utils/chapter'; import { logButtonClick } from '@/utils/eventLogger'; diff --git a/src/components/Search/SearchResults/SearchResultItem.module.scss b/src/components/Search/SearchResults/SearchResultItem.module.scss index f81fefdef3..1d4071a515 100644 --- a/src/components/Search/SearchResults/SearchResultItem.module.scss +++ b/src/components/Search/SearchResults/SearchResultItem.module.scss @@ -1,19 +1,3 @@ -.translationName { - font-size: var(--font-size-xsmall); - opacity: var(--opacity-75); -} - -.translationContainer { - margin-block-start: var(--spacing-small); - margin-block-end: var(--spacing-small); - margin-inline-start: 0; - margin-inline-end: 0; - em { - font-weight: var(--font-weight-semibold); - text-decoration: underline; - } -} - .container { text-decoration: none; border-block-end: 1px solid var(--color-borders-hairline); @@ -30,11 +14,15 @@ .quranTextResult { font-size: var(--font-size-xlarge); line-height: var(--line-height-large); - direction: rtl; padding-block-start: var(--spacing-micro); padding-block-end: var(--spacing-micro); padding-inline-start: var(--spacing-micro); padding-inline-end: var(--spacing-micro); + + em { + font-weight: var(--font-weight-semibold); + text-decoration: underline; + } } .verseKey { diff --git a/src/components/Search/SearchResults/SearchResultItem.tsx b/src/components/Search/SearchResults/SearchResultItem.tsx index 50360d057e..db51c3190e 100644 --- a/src/components/Search/SearchResults/SearchResultItem.tsx +++ b/src/components/Search/SearchResults/SearchResultItem.tsx @@ -1,42 +1,42 @@ -/* eslint-disable react/no-danger */ - -import React, { useMemo } from 'react'; +import React from 'react'; import useTranslation from 'next-translate/useTranslation'; +import KalimatNavigationSearchResultItem from './KalimatNavigationSearchResultItem'; import styles from './SearchResultItem.module.scss'; -import Link from '@/dls/Link/Link'; -import QuranWord from '@/dls/QuranWord/QuranWord'; -import useGetChaptersData from '@/hooks/useGetChaptersData'; +import Button from '@/dls/Button/Button'; +import { SearchNavigationType } from '@/types/Search/SearchNavigationResult'; import SearchService from '@/types/Search/SearchService'; +import SearchVerseItem from '@/types/Search/SearchVerseItem'; import SearchQuerySource from '@/types/SearchQuerySource'; -import { getChapterData } from '@/utils/chapter'; import { logButtonClick } from '@/utils/eventLogger'; -import { toLocalizedVerseKey } from '@/utils/locale'; -import { getChapterWithStartingVerseUrl } from '@/utils/navigation'; -import { getChapterNumberFromKey } from '@/utils/verse'; -import Verse from 'types/Verse'; +import { toLocalizedNumber, toLocalizedVerseKey } from '@/utils/locale'; +import { resolveUrlBySearchNavigationType } from '@/utils/navigation'; interface Props { - result: Verse; + result: SearchVerseItem; source: SearchQuerySource; service?: SearchService; } -const SearchResultItem: React.FC = ({ result, source, service = SearchService.QDC }) => { +const SearchResultItem: React.FC = ({ result, source, service = SearchService.KALIMAT }) => { const { lang } = useTranslation('quran-reader'); - const localizedVerseKey = useMemo( - () => toLocalizedVerseKey(result.verseKey, lang), - [lang, result.verseKey], - ); + const url = resolveUrlBySearchNavigationType(result.resultType, result.key, true); - const chaptersData = useGetChaptersData(lang); + const getKalimatResultSuffix = () => { + if (result.resultType === SearchNavigationType.SURAH) { + return `(${toLocalizedNumber(Number(result.key), lang)})`; + } - if (!chaptersData) return null; + if (result.resultType === SearchNavigationType.AYAH) { + return `(${toLocalizedVerseKey(result.key as string, lang)})`; + } + + return undefined; + }; - const chapterNumber = getChapterNumberFromKey(result.verseKey); - const chapterData = getChapterData(chaptersData, chapterNumber.toString()); + const suffix = getKalimatResultSuffix(); const onResultItemClicked = () => { logButtonClick(`search_result_item`, { @@ -48,35 +48,20 @@ const SearchResultItem: React.FC = ({ result, source, service = SearchSer return (
- - {chapterData.transliteratedName} {localizedVerseKey} - -
-
- {result.words.map((word, index) => { - return ( - - ); - })} -
+
+ {result.resultType === SearchNavigationType.AYAH ? ( + + ) : ( + + )}
- {result.translations?.map((translation) => ( -
-
- {/* eslint-disable-next-line i18next/no-literal-string */} -

- {translation.resourceName}

-
- ))}
); diff --git a/src/components/Search/SearchResults/index.tsx b/src/components/Search/SearchResults/index.tsx index 88650d699d..3016fd8c6f 100644 --- a/src/components/Search/SearchResults/index.tsx +++ b/src/components/Search/SearchResults/index.tsx @@ -57,7 +57,7 @@ const SearchResults: React.FC = ({ <> {searchResult.result.verses.map((result) => ( = ({ searchResult, isCommandBar }) => { return ( <> {data.verses.map((verse) => ( - = ({ + result, + source, + service = SearchService.Tarteel, +}) => { + const { lang } = useTranslation('quran-reader'); + const localizedVerseKey = useMemo( + () => toLocalizedVerseKey(result.verseKey, lang), + [lang, result.verseKey], + ); + + const chaptersData = useGetChaptersData(lang); + + if (!chaptersData) return null; + + const chapterNumber = getChapterNumberFromKey(result.verseKey); + const chapterData = getChapterData(chaptersData, chapterNumber.toString()); + + const onResultItemClicked = () => { + logButtonClick(`search_result_item`, { + service, + source, + }); + }; + + return ( +
+
+ + {chapterData.transliteratedName} {localizedVerseKey} + +
+
+ {result.words.map((word, index) => { + return ( + + ); + })} +
+
+ {result.translations?.map((translation) => ( +
+
+ {/* eslint-disable-next-line i18next/no-literal-string */} +

- {translation.resourceName}

+
+ ))} +
+
+ ); +}; +export default TarteelSearchResultItem; diff --git a/src/pages/search.tsx b/src/pages/search.tsx index eecc8fbf79..40a22eda36 100644 --- a/src/pages/search.tsx +++ b/src/pages/search.tsx @@ -9,33 +9,23 @@ import { useDispatch } from 'react-redux'; import styles from './search.module.scss'; -import { getAvailableLanguages, getAvailableTranslations } from '@/api'; +import { getAvailableLanguages } from '@/api'; import NextSeoWrapper from '@/components/NextSeoWrapper'; -import TranslationsFilter from '@/components/Search/Filters/TranslationsFilter'; import SearchBodyContainer from '@/components/Search/SearchBodyContainer'; -import Button, { ButtonSize, ButtonVariant } from '@/dls/Button/Button'; -import ContentModal, { ContentModalSize } from '@/dls/ContentModal/ContentModal'; import Input, { InputVariant } from '@/dls/Forms/Input'; import useAddQueryParamsToUrl from '@/hooks/useAddQueryParamsToUrl'; import useDebounce from '@/hooks/useDebounce'; import useFocus from '@/hooks/useFocusElement'; -import FilterIcon from '@/icons/filter.svg'; import SearchIcon from '@/icons/search.svg'; import { setInitialSearchQuery, setIsOpen } from '@/redux/slices/CommandBar/state'; import SearchQuerySource from '@/types/SearchQuerySource'; import { getAllChaptersData } from '@/utils/chapter'; -import { logButtonClick, logEvent, logValueChange } from '@/utils/eventLogger'; -import filterTranslations from '@/utils/filter-translations'; -import { getLanguageAlternates, toLocalizedNumber } from '@/utils/locale'; +import { logButtonClick, logEvent } from '@/utils/eventLogger'; +import { getLanguageAlternates } from '@/utils/locale'; import { getCanonicalUrl } from '@/utils/navigation'; -import { - addToSearchHistory, - getDefaultTranslationIdsByLang, - searchGetResults, -} from '@/utils/search'; +import { addToSearchHistory, searchGetResults } from '@/utils/search'; import { SearchResponse } from 'types/ApiResponses'; import AvailableLanguage from 'types/AvailableLanguage'; -import AvailableTranslation from 'types/AvailableTranslation'; import ChaptersData from 'types/ChaptersData'; const PAGE_SIZE = 10; @@ -43,22 +33,16 @@ const DEBOUNCING_PERIOD_MS = 1000; type SearchProps = { languages: AvailableLanguage[]; - translations: AvailableTranslation[]; chaptersData: ChaptersData; }; -const Search: NextPage = ({ translations }): JSX.Element => { +const Search: NextPage = (): JSX.Element => { const { t, lang } = useTranslation('common'); const router = useRouter(); const [searchQuery, setSearchQuery] = useState(''); const [focusInput, searchInputRef]: [() => void, RefObject] = useFocus(); const [currentPage, setCurrentPage] = useState(1); const [selectedLanguages, setSelectedLanguages] = useState(''); - const [selectedTranslations, setSelectedTranslations] = useState(() => { - return getDefaultTranslationIdsByLang(translations, lang) as string; - }); - const [translationSearchQuery, setTranslationSearchQuery] = useState(''); - const [isContentModalOpen, setIsContentModalOpen] = useState(false); const [isSearching, setIsSearching] = useState(false); const [hasError, setHasError] = useState(false); const [searchResult, setSearchResult] = useState(null); @@ -71,9 +55,8 @@ const Search: NextPage = ({ translations }): JSX.Element => { page: currentPage, languages: selectedLanguages, q: debouncedSearchQuery, - translations: selectedTranslations, }), - [currentPage, debouncedSearchQuery, selectedLanguages, selectedTranslations], + [currentPage, debouncedSearchQuery, selectedLanguages], ); useAddQueryParamsToUrl('/search', queryParams); @@ -83,15 +66,10 @@ const Search: NextPage = ({ translations }): JSX.Element => { // in the query object. @see https://nextjs.org/docs/routing/dynamic-routes#caveats useEffect(() => { // we don't want to focus the main search input when the translation filter modal is open. - if (router.isReady && !isContentModalOpen) { + if (router.isReady) { focusInput(); } - }, [focusInput, router, isContentModalOpen]); - - // handle when language changes - useEffect(() => { - setSelectedTranslations(getDefaultTranslationIdsByLang(translations, lang) as string); - }, [lang, translations]); + }, [focusInput, router]); useEffect(() => { if (router.query.q || router.query.query) { @@ -108,16 +86,7 @@ const Search: NextPage = ({ translations }): JSX.Element => { if (router.query.languages) { setSelectedLanguages(router.query.languages as string); } - if (router.query.translations) { - setSelectedTranslations(router.query.translations as string); - } - }, [ - router.query.q, - router.query.query, - router.query.page, - router.query.languages, - router.query.translations, - ]); + }, [router.query.q, router.query.query, router.query.page, router.query.languages]); /** * Handle when the search query is changed. @@ -140,25 +109,20 @@ const Search: NextPage = ({ translations }): JSX.Element => { * * @param {string} query * @param {number} page - * @param {string} translation * @param {string} language */ - const getResults = useCallback( - (query: string, page: number, translation: string, language: string) => { - searchGetResults( - SearchQuerySource.SearchPage, - query, - page, - PAGE_SIZE, - setIsSearching, - setHasError, - setSearchResult, - language, - translation, - ); - }, - [], - ); + const getResults = useCallback((query: string, page: number, language: string) => { + searchGetResults( + SearchQuerySource.SearchPage, + query, + page, + PAGE_SIZE, + setIsSearching, + setHasError, + setSearchResult, + language, + ); + }, []); // a ref to know whether this is the initial search request made when the user loads the page or not const isInitialSearch = useRef(true); @@ -178,7 +142,6 @@ const Search: NextPage = ({ translations }): JSX.Element => { debouncedSearchQuery, // if it is the initial search request, use the page number in the url, otherwise, reset it isInitialSearch.current ? currentPage : 1, - selectedTranslations, selectedLanguages, ); @@ -190,97 +153,20 @@ const Search: NextPage = ({ translations }): JSX.Element => { // we don't want to run this effect when currentPage is changed // because we are already handeling this in onPageChange // eslint-disable-next-line react-hooks/exhaustive-deps - }, [debouncedSearchQuery, getResults, selectedLanguages, selectedTranslations]); + }, [debouncedSearchQuery, getResults, selectedLanguages]); const onPageChange = (page: number) => { logEvent('search_page_number_change', { page }); setCurrentPage(page); - getResults(debouncedSearchQuery, page, selectedTranslations, selectedLanguages); + getResults(debouncedSearchQuery, page, selectedLanguages); }; - const onTranslationChange = useCallback((translationIds: string[]) => { - // convert the array into a string - setSelectedTranslations((prevTranslationIds) => { - // filter out the empty strings - const newTranslationIds = translationIds.filter((id) => id !== '').join(','); - logValueChange('search_page_selected_translations', prevTranslationIds, newTranslationIds); - return newTranslationIds; - }); - // reset the current page since most probable the results will change. - setCurrentPage(1); - }, []); - const onSearchKeywordClicked = useCallback((keyword: string) => { setSearchQuery(keyword); }, []); const navigationUrl = '/search'; - const formattedSelectedTranslations = useMemo(() => { - if (!selectedTranslations) return t('search:default-translations'); - - let selectedValueString; - - const selectedTranslationsArray = selectedTranslations.split(','); - - const firstSelectedTranslation = translations.find( - (translation) => translation.id.toString() === selectedTranslationsArray[0], - ); - - if (!firstSelectedTranslation) return t('search:all-translations'); - - if (selectedTranslationsArray.length === 1) { - selectedValueString = firstSelectedTranslation.name; - } - if (selectedTranslationsArray.length === 2) { - selectedValueString = t('settings.value-and-other', { - value: firstSelectedTranslation?.name, - othersCount: toLocalizedNumber(selectedTranslationsArray.length - 1, lang), - }); - } - if (selectedTranslationsArray.length > 2) { - selectedValueString = t('settings.value-and-others', { - value: firstSelectedTranslation?.name, - othersCount: toLocalizedNumber(selectedTranslationsArray.length - 1, lang), - }); - } - - return selectedValueString; - }, [lang, selectedTranslations, t, translations]); - - const filteredTranslations = translationSearchQuery - ? filterTranslations(translations, translationSearchQuery) - : translations; - - const onResetButtonClicked = () => { - logButtonClick('search_page_reset_button'); - const defaultLangTranslationIds = getDefaultTranslationIdsByLang( - translations, - lang, - false, - ) as string[]; - onTranslationChange(defaultLangTranslationIds); - }; - - const onTranslationSearchQueryChange = (newTranslationSearchQuery: string) => { - logValueChange( - 'search_page_translation_search_query', - translationSearchQuery, - newTranslationSearchQuery, - ); - setTranslationSearchQuery(newTranslationSearchQuery); - }; - - const onTranslationSearchClearClicked = () => { - logButtonClick('search_page_translation_search_clear'); - setTranslationSearchQuery(''); - }; - - const onTranslationsFiltersClicked = () => { - logButtonClick('search_page_translation_filter'); - setIsContentModalOpen(true); - }; - return ( <> = ({ translations }): JSX.Element => { fixedWidth={false} variant={InputVariant.Main} /> - -
- } - onChange={onTranslationSearchQueryChange} - onClearClicked={onTranslationSearchClearClicked} - clearable - value={translationSearchQuery} - placeholder={t('settings.search-translations')} - fixedWidth={false} - variant={InputVariant.Main} - /> -
- -
- } - isOpen={isContentModalOpen} - onClose={() => setIsContentModalOpen(false)} - > - - -
- -
- {/* eslint-disable-next-line i18next/no-literal-string */} - {t('search:searching-translations')}: - {formattedSelectedTranslations} -
-
@@ -387,28 +221,19 @@ const Search: NextPage = ({ translations }): JSX.Element => { export const getStaticProps: GetStaticProps = async ({ locale }) => { try { - const [availableLanguagesResponse, availableTranslationsResponse] = await Promise.all([ - getAvailableLanguages(locale), - getAvailableTranslations(locale), - ]); + const availableLanguagesResponse = await getAvailableLanguages(locale); - let translations = []; let languages = []; if (availableLanguagesResponse.status !== 500) { const { languages: responseLanguages } = availableLanguagesResponse; languages = responseLanguages; } - if (availableTranslationsResponse.status !== 500) { - const { translations: responseTranslations } = availableTranslationsResponse; - translations = responseTranslations; - } const chaptersData = await getAllChaptersData(locale); return { props: { chaptersData, languages, - translations, }, }; } catch (e) { diff --git a/src/redux/slices/CommandBar/state.ts b/src/redux/slices/CommandBar/state.ts index bc8dc2821e..e38751d576 100644 --- a/src/redux/slices/CommandBar/state.ts +++ b/src/redux/slices/CommandBar/state.ts @@ -2,7 +2,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { RootState } from '@/redux/RootState'; import SliceName from '@/redux/types/SliceName'; -import { SearchNavigationResult } from 'types/SearchNavigationResult'; +import { SearchNavigationResult } from 'types/Search/SearchNavigationResult'; export type CommandBar = { isOpen: boolean; diff --git a/src/utils/apiPaths.ts b/src/utils/apiPaths.ts index 39f9ffa5f0..b1703e1ecb 100644 --- a/src/utils/apiPaths.ts +++ b/src/utils/apiPaths.ts @@ -10,7 +10,7 @@ import { getTranslationsInitialState, } from '@/redux/defaultSettings/util'; import { SearchRequestParams, SearchMode } from '@/types/Search/SearchRequestParams'; -import { AdvancedCopyRequest, PagesLookUpRequest, SearchRequest } from 'types/ApiRequests'; +import { AdvancedCopyRequest, PagesLookUpRequest } from 'types/ApiRequests'; import { MushafLines, QuranFont } from 'types/QuranReader'; export const DEFAULT_VERSES_PARAMS = { @@ -146,14 +146,6 @@ export const makeTranslationsInfoUrl = (locale: string, translations: number[]): export const makeAdvancedCopyUrl = (params: AdvancedCopyRequest): string => makeUrl('/verses/advanced_copy', params as Record); -/** - * Compose the url for search API. - * - * @param {SearchRequest} params the request params. - * @returns {string} - */ -export const makeSearchResultsUrl = (params: SearchRequest): string => makeUrl('/search', params); - export const makeNewSearchApiUrl = (params: Record) => { const baseUrl = process.env.NEXT_PUBLIC_SEARCH_BASE_URL; diff --git a/src/utils/navigation.ts b/src/utils/navigation.ts index a2b5d3056d..32807effe0 100644 --- a/src/utils/navigation.ts +++ b/src/utils/navigation.ts @@ -8,7 +8,7 @@ import { getVerseAndChapterNumbersFromKey, getVerseNumberRangeFromKey } from './ import QueryParam from '@/types/QueryParam'; import { QuranReaderFlow } from '@/types/QuranReader'; -import { SearchNavigationType } from 'types/SearchNavigationResult'; +import { SearchNavigationType } from 'types/Search/SearchNavigationResult'; /** * Get the href link to a verse. diff --git a/src/utils/search.ts b/src/utils/search.ts index dd047285fc..ceaed36148 100644 --- a/src/utils/search.ts +++ b/src/utils/search.ts @@ -8,7 +8,7 @@ import { AnyAction } from 'redux'; import { logEmptySearchResults, logSearchResults, logTextSearchQuery } from './eventLogger'; -import { getNewSearchResults, getSearchResults } from '@/api'; +import { getNewSearchResults } from '@/api'; import { addSearchHistoryRecord } from '@/redux/slices/Search/search'; import { SearchResponse } from '@/types/ApiResponses'; import AvailableTranslation from '@/types/AvailableTranslation'; @@ -19,7 +19,7 @@ import SearchQuerySource from '@/types/SearchQuerySource'; import { getChapterData } from '@/utils/chapter'; import { toLocalizedNumber } from '@/utils/locale'; import { getVerseAndChapterNumbersFromKey, getVerseNumberRangeFromKey } from '@/utils/verse'; -import { SearchNavigationResult, SearchNavigationType } from 'types/SearchNavigationResult'; +import { SearchNavigationResult, SearchNavigationType } from 'types/Search/SearchNavigationResult'; export const LOCALE_TO_TRANSLATION_LANGUAGE = { en: 'english', @@ -177,8 +177,7 @@ export const getSearchNavigationResult = ( }; /** - * Call BE to fetch the search results using the passed filters - * and if there are no results call Kalimat API. + * Call Kalimat API to fetch the search results using the passed filters. * * @param {SearchQuerySource} source * @param {string} query @@ -188,7 +187,6 @@ export const getSearchNavigationResult = ( * @param {(arg: boolean) => void} setHasError * @param {(data: SearchResponse) => void} setSearchResult * @param {string} languages - * @param {string} translations */ export const searchGetResults = ( source: SearchQuerySource, @@ -199,65 +197,37 @@ export const searchGetResults = ( setHasError: (arg: boolean) => void, setSearchResult: (data: SearchResponse) => void, languages?: string, - translations?: string, ) => { setIsSearching(true); logTextSearchQuery(query, source); - getSearchResults({ + getNewSearchResults({ + mode: SearchMode.Advanced, query, - ...(languages && { filterLanguages: languages }), // languages will be included only when there is a selected language size: pageSize, + filterLanguages: languages, page, - ...(translations && { filterTranslations: translations }), // translations will be included only when there is a selected translation + exactMatchesOnly: 0, + getText: 1, + highlight: 1, }) - .then(async (response) => { - if (response.status === 500) { - setHasError(true); - } else { - setSearchResult({ ...response, service: SearchService.QDC }); - const noQdcResults = - response.pagination.totalRecords === 0 && !response.result.navigation.length; - // if there is no navigations nor verses in the response - if (noQdcResults) { - logEmptySearchResults({ - query, - source, - service: SearchService.QDC, - }); + .then(async (kalimatResponse) => { + setSearchResult({ + ...kalimatResponse, + service: SearchService.KALIMAT, + }); - const kalimatResponse = await getNewSearchResults({ - mode: SearchMode.Advanced, - query, - size: pageSize, - filterLanguages: languages, - page, - exactMatchesOnly: 0, - // translations will be included only when there is a selected translation - ...(translations && { - filterTranslations: translations, - translationFields: 'resource_name', - }), - }); - - setSearchResult({ - ...kalimatResponse, - service: SearchService.KALIMAT, - }); - - if (kalimatResponse.pagination.totalRecords === 0) { - logEmptySearchResults({ - query, - source, - service: SearchService.KALIMAT, - }); - } else { - logSearchResults({ - query, - source, - service: SearchService.KALIMAT, - }); - } - } + if (kalimatResponse.pagination.totalRecords === 0) { + logEmptySearchResults({ + query, + source, + service: SearchService.KALIMAT, + }); + } else { + logSearchResults({ + query, + source, + service: SearchService.KALIMAT, + }); } }) .catch(() => { diff --git a/types/ApiRequests.ts b/types/ApiRequests.ts index cb44fb7f94..e7da06d773 100644 --- a/types/ApiRequests.ts +++ b/types/ApiRequests.ts @@ -1,11 +1,3 @@ -export type SearchRequest = { - query: string; - filterLanguages?: string; - filterTranslations?: string; - size?: number; - page?: number; -}; - export type AdvancedCopyRequest = { from: string; to: string; diff --git a/types/ApiResponses.ts b/types/ApiResponses.ts index cb55c05c9c..4f1f46e505 100644 --- a/types/ApiResponses.ts +++ b/types/ApiResponses.ts @@ -9,8 +9,9 @@ import LookupRange from './LookupRange'; import LookupRecord from './LookupRecord'; import MetaData from './MetaData'; import Reciter from './Reciter'; +import { SearchNavigationResult } from './Search/SearchNavigationResult'; import SearchService from './Search/SearchService'; -import { SearchNavigationResult } from './SearchNavigationResult'; +import SearchVerseItem from './Search/SearchVerseItem'; import TafsirInfo from './TafsirInfo'; import Verse from './Verse'; @@ -19,7 +20,7 @@ export interface BaseResponse { error?: string; } -interface Pagination { +export interface Pagination { perPage: number; currentPage: number; nextPage: number | null; @@ -84,7 +85,7 @@ export interface SearchResponse extends BaseResponse { pagination: Pagination; result?: { navigation: SearchNavigationResult[]; - verses: Verse[]; + verses: SearchVerseItem[]; }; } diff --git a/types/SearchNavigationResult.ts b/types/Search/SearchNavigationResult.ts similarity index 100% rename from types/SearchNavigationResult.ts rename to types/Search/SearchNavigationResult.ts diff --git a/types/Search/SearchRequestParams.ts b/types/Search/SearchRequestParams.ts index 167515b826..1d2daa92b9 100644 --- a/types/Search/SearchRequestParams.ts +++ b/types/Search/SearchRequestParams.ts @@ -20,7 +20,6 @@ export type SearchRequestParams = { size?: number; page?: number; getText?: SearchBoolean; - filterTranslations?: string; filterLanguages?: string; fields?: string; translationFields?: string; diff --git a/types/Search/SearchResponse.ts b/types/Search/SearchResponse.ts index 7bb72ad810..8ce399109b 100644 --- a/types/Search/SearchResponse.ts +++ b/types/Search/SearchResponse.ts @@ -1,6 +1,6 @@ -import { BaseResponse } from '../ApiResponses'; -import { SearchNavigationResult } from '../SearchNavigationResult'; +import { BaseResponse, Pagination } from '../ApiResponses'; +import { SearchNavigationResult } from './SearchNavigationResult'; import SearchVerseItem from './SearchVerseItem'; interface SearchResponse extends BaseResponse { @@ -8,13 +8,7 @@ interface SearchResponse extends BaseResponse { navigation: SearchNavigationResult[]; verses: SearchVerseItem[]; }; - pagination: { - perPage: number; - currentPage: number; - nextPage: number | null; - totalRecords: number; - totalPages: number; - }; + pagination: Pagination; } export default SearchResponse; diff --git a/types/Search/SearchVerseItem.ts b/types/Search/SearchVerseItem.ts index 1d85a42a3b..98cded5ea3 100644 --- a/types/Search/SearchVerseItem.ts +++ b/types/Search/SearchVerseItem.ts @@ -1,11 +1,4 @@ -import Verse from '../Verse'; -import Word from '../Word'; +import { SearchNavigationResult } from './SearchNavigationResult'; -type SearchVerseItem = Verse & { - words: Word[]; -} & { - kalimatData: { - matches?: string; - }; -}; +type SearchVerseItem = SearchNavigationResult; export default SearchVerseItem;