Skip to content

Commit

Permalink
PR cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Hussein committed Jul 2, 2024
1 parent 1aa637c commit 6079d12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 37 deletions.
35 changes: 11 additions & 24 deletions src/components/Navbar/SearchDrawer/SearchDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable react-func/max-lines-per-function */
/* eslint-disable max-lines */
/* eslint-disable react/no-multi-comp */
import React, { RefObject, useCallback, useEffect, useState } from 'react';
import React, { RefObject, useEffect, useState } from 'react';

import dynamic from 'next/dynamic';
import useTranslation from 'next-translate/useTranslation';
import { shallowEqual, useDispatch, useSelector } from 'react-redux';

import SearchDrawerHeader from './Header';
Expand All @@ -17,7 +18,6 @@ import { selectSelectedTranslations } from '@/redux/slices/QuranReader/translati
import { selectIsSearchDrawerVoiceFlowStarted } from '@/redux/slices/voiceSearch';
import SearchQuerySource from '@/types/SearchQuerySource';
import { areArraysEqual } from '@/utils/array';
import { getLocaleCookie } from '@/utils/cookies';
import { logButtonClick } from '@/utils/eventLogger';
import { addToSearchHistory, searchGetResults } from '@/utils/search';
import { SearchResponse } from 'types/ApiResponses';
Expand All @@ -39,6 +39,7 @@ const PAGE_SIZE = 10;
const DEBOUNCING_PERIOD_MS = 1000;

const SearchDrawer: React.FC = () => {
const { lang } = useTranslation();
const selectedTranslations = useSelector(selectSelectedTranslations, areArraysEqual);
const [focusInput, searchInputRef]: [() => void, RefObject<HTMLInputElement>] = useFocus();
const [searchQuery, setSearchQuery] = useState<string>('');
Expand All @@ -57,37 +58,23 @@ const SearchDrawer: React.FC = () => {
}
}, [isOpen, focusInput]);

/**
* Call BE to fetch the results using the passed filters.
*
* @param {string} query
* @param {number} page
*/
const getResults = useCallback(
(query: string, page: number) => {
useEffect(() => {
// only when the search query has a value we call the API.
if (debouncedSearchQuery) {
addToSearchHistory(dispatch, debouncedSearchQuery, SearchQuerySource.SearchDrawer);
searchGetResults(
SearchQuerySource.SearchDrawer,
query,
page,
debouncedSearchQuery,
FIRST_PAGE_NUMBER,
PAGE_SIZE,
setIsSearching,
setHasError,
setSearchResult,
getLocaleCookie(),
lang,
selectedTranslations?.length && selectedTranslations.join(','),
);
},
[selectedTranslations],
);

useEffect(() => {
// only when the search query has a value we call the API.
if (debouncedSearchQuery) {
addToSearchHistory(dispatch, debouncedSearchQuery, SearchQuerySource.SearchDrawer);

getResults(debouncedSearchQuery, FIRST_PAGE_NUMBER);
}
}, [debouncedSearchQuery, selectedTranslations, dispatch, getResults]);
}, [debouncedSearchQuery, selectedTranslations, dispatch, lang]);

const resetQueryAndResults = () => {
logButtonClick('search_drawer_clear_input');
Expand Down
14 changes: 1 addition & 13 deletions src/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import Cookies from 'js-cookie';

const NEXT_LOCALE = 'NEXT_LOCALE';
const LOCALE_COOKIE_PERSISTENCE_PERIOD_MS = 86400000000000; // maximum milliseconds-since-the-epoch value https://stackoverflow.com/a/56980560/1931451

// eslint-disable-next-line import/prefer-default-export
export const setLocaleCookie = (newLocale: string) => {
const date = new Date();
date.setTime(LOCALE_COOKIE_PERSISTENCE_PERIOD_MS);
// eslint-disable-next-line i18next/no-literal-string
document.cookie = `${NEXT_LOCALE}=${newLocale};expires=${date.toUTCString()};path=/`;
};

export const getLocaleCookie = () => {
const value = Cookies.get(NEXT_LOCALE);
if (!value) {
return null;
}

return value;
document.cookie = `NEXT_LOCALE=${newLocale};expires=${date.toUTCString()};path=/`;
};

0 comments on commit 6079d12

Please sign in to comment.