Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Oct 24, 2024
1 parent 57aa920 commit 5ef9acf
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions src/common/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,71 @@ export const useSearch = () => {
setIsLoading(false);
};

const handlePageClick = async ({
pageNumber,
query,
pageMetadata,
isBrowseSearch,
searchBy,
navigationSegment,
baseQuerySelectorType,
pageMetadataSelectorType,
}: {
pageNumber: number;
query: string;
pageMetadata: PageMetadata;
isBrowseSearch: boolean;
searchBy: string;
navigationSegment?: NavigationSegment;
baseQuerySelectorType: 'Prev' | 'Next';
pageMetadataSelectorType: 'prev' | 'next';
}) => {
const isInitialPage = pageNumber === 0;
const selectedQuery = (isInitialPage ? query : pageMetadata?.[pageMetadataSelectorType]) || query;
const baseQuerySelector =
isBrowseSearch && !isInitialPage
? SearchableIndexQuerySelector[baseQuerySelectorType]
: SearchableIndexQuerySelector.Query;

await fetchData({
query: selectedQuery,
searchBy,
offset: pageNumber,
selectedSegment: navigationSegment?.value,
baseQuerySelector,
});
};

const onPrevPageClick = hasCustomPagination
? async () => {
const pageNumber = onPrevPageClickBase();
const isInitialPage = pageNumber === 0;
const selectedQuery = (isInitialPage ? query : pageMetadata?.prev) || query;
const baseQuerySelector =
isBrowseSearch && !isInitialPage ? SearchableIndexQuerySelector.Prev : SearchableIndexQuerySelector.Query;

await fetchData({
query: selectedQuery,
await handlePageClick({
pageNumber,
query,
pageMetadata,
isBrowseSearch,
searchBy,
offset: pageNumber,
selectedSegment: navigationSegment?.value,
baseQuerySelector,
navigationSegment,
baseQuerySelectorType: 'Prev',
pageMetadataSelectorType: 'prev',
});
}
: onPrevPageClickBase;

const onNextPageClick = hasCustomPagination
? async () => {
const pageNumber = onNextPageClickBase();
const isInitialPage = pageNumber === 0;
const selectedQuery = (isInitialPage ? query : pageMetadata?.next) || query;
const baseQuerySelector =
isBrowseSearch && !isInitialPage ? SearchableIndexQuerySelector.Next : SearchableIndexQuerySelector.Query;

await fetchData({
query: selectedQuery,
await handlePageClick({
pageNumber,
query,
pageMetadata,
isBrowseSearch,
searchBy,
offset: pageNumber,
selectedSegment: navigationSegment?.value,
baseQuerySelector,
navigationSegment,
baseQuerySelectorType: 'Next',
pageMetadataSelectorType: 'next',
});
}
: onNextPageClickBase;
Expand Down

0 comments on commit 5ef9acf

Please sign in to comment.