diff --git a/src/common/api/search.api.ts b/src/common/api/search.api.ts index 33cebefe..42218f67 100644 --- a/src/common/api/search.api.ts +++ b/src/common/api/search.api.ts @@ -44,14 +44,28 @@ export const getSearchData = (url?: string, urlParams?: Record) }; export const getSearchResults = async (params: Record) => { - const { endpointUrl, query, offset = '0', limit = SEARCH_RESULTS_LIMIT.toString(), resultsContainer } = params; + const { + endpointUrl, + query, + offset, + limit = SEARCH_RESULTS_LIMIT.toString(), + resultsContainer, + precedingRecordsCount, + } = params; const urlParams: Record | undefined = { query: query as string, - offset: offset?.toString(), limit: limit?.toString(), }; + if (offset) { + urlParams.offset = offset?.toString(); + } + + if (precedingRecordsCount) { + urlParams.precedingRecordsCount = precedingRecordsCount.toString(); + } + const result = await baseApi.getJson({ url: endpointUrl as string, urlParams }); return { diff --git a/src/common/constants/search.constants.ts b/src/common/constants/search.constants.ts index 4abea21d..f84ed370 100644 --- a/src/common/constants/search.constants.ts +++ b/src/common/constants/search.constants.ts @@ -94,6 +94,8 @@ export type AdvancedSearchSchema = AdvancedSearchSchemaRow[]; export const SEARCH_RESULTS_LIMIT = 10; +export const BROWSE_PRECEDING_RECORDS_COUNT = 5; + export const SELECT_IDENTIFIERS = Object.values(SearchIdentifiers); export const SELECT_OPERATORS = Object.values(AdvancedSearchOperators); diff --git a/src/common/hooks/useSearch.ts b/src/common/hooks/useSearch.ts index 3afb833a..8aa7c383 100644 --- a/src/common/hooks/useSearch.ts +++ b/src/common/hooks/useSearch.ts @@ -2,8 +2,9 @@ import { useCallback, useEffect } from 'react'; import { useSearchParams } from 'react-router-dom'; import { useSetRecoilState, useRecoilState, useResetRecoilState } from 'recoil'; import { getByIdentifier } from '@common/api/search.api'; +import { SearchableIndexQuerySelector } from '@common/constants/complexLookup.constants'; import { DEFAULT_PAGES_METADATA } from '@common/constants/api.constants'; -import { SearchIdentifiers } from '@common/constants/search.constants'; +import { SearchIdentifiers, SearchSegment } from '@common/constants/search.constants'; import { StatusType } from '@common/constants/status.constants'; import { generateSearchParamsState, normalizeQuery } from '@common/helpers/search.helper'; import { normalizeLccn } from '@common/helpers/validations.helper'; @@ -11,7 +12,6 @@ import { UserNotificationFactory } from '@common/services/userNotification'; import { usePagination } from '@common/hooks/usePagination'; import state from '@state'; import { useSearchContext } from './useSearchContext'; -import { SearchableIndexQuerySelector } from '@common/constants/complexLookup.constants'; export const useSearch = () => { const { @@ -31,6 +31,7 @@ export const useSearch = () => { searchableIndicesMap, getSearchSourceData, buildSearchQuery, + precedingRecordsCount, } = useSearchContext(); const setIsLoading = useSetRecoilState(state.loadingState.isLoading); const [searchBy, setSearchBy] = useRecoilState(state.search.index); @@ -130,6 +131,7 @@ export const useSearch = () => { value: updatedQuery, }) as string) : (updatedQuery as string); + const isBrowseSearch = selectedNavigationSegment === SearchSegment.Browse; const result = fetchSearchResults ? await fetchSearchResults({ @@ -140,6 +142,7 @@ export const useSearch = () => { query: generatedQuery, offset: offset?.toString(), limit: searchResultsLimit?.toString(), + precedingRecordsCount: isBrowseSearch ? precedingRecordsCount : undefined, resultsContainer: searchResultsContainer?.[selectedNavigationSegment as SearchSegmentValue], }) : await getByIdentifier({ diff --git a/src/components/ComplexLookupField/ModalComplexLookup.tsx b/src/components/ComplexLookupField/ModalComplexLookup.tsx index 67576496..e997c7d4 100644 --- a/src/components/ComplexLookupField/ModalComplexLookup.tsx +++ b/src/components/ComplexLookupField/ModalComplexLookup.tsx @@ -170,6 +170,7 @@ export const ModalComplexLookup: FC = memo( fetchSearchResults={getSearchResults} buildSearchQuery={buildSearchQuery} searchResultsLimit={api.searchQuery.limit} + precedingRecordsCount={api.searchQuery.precedingRecordsCount} searchResultsContainer={api.results.containers} onAssignRecord={onAssign} /> diff --git a/src/configs/complexLookup/complexLookup.config.ts b/src/configs/complexLookup/complexLookup.config.ts index 683ed788..fd91615b 100644 --- a/src/configs/complexLookup/complexLookup.config.ts +++ b/src/configs/complexLookup/complexLookup.config.ts @@ -20,6 +20,7 @@ export const COMPLEX_LOOKUPS_CONFIG: ComplexLookupsConfig = { sourceKey: 'authoritySourceFiles', searchQuery: { limit: 100, + precedingRecordsCount: 5, }, results: { containers: { diff --git a/src/types/complexLookup.d.ts b/src/types/complexLookup.d.ts index da74af5b..e08cd7e4 100644 --- a/src/types/complexLookup.d.ts +++ b/src/types/complexLookup.d.ts @@ -44,6 +44,7 @@ type ComplexLookupApiEntryConfig = { searchQuery: { filter?: string; limit?: number; + precedingRecordsCount?: number; }; results: { containers: { @@ -93,4 +94,4 @@ type BuildSearchQueryParams = { selector?: SearchableIndexQuerySelectorType; searchBy: SearchableIndexType; value: string; -} +}; diff --git a/src/types/search.d.ts b/src/types/search.d.ts index 5333a165..41fa10b5 100644 --- a/src/types/search.d.ts +++ b/src/types/search.d.ts @@ -41,6 +41,7 @@ type SearchParams = { getSearchSourceData?: (url?: string) => Promise; getSearchFacetsData?: (facet?: string, isOpen?: boolean) => Promise; searchResultsLimit?: number; + precedingRecordsCount?: number; fetchSearchResults?: (params: any) => Promise; buildSearchQuery?: (params: BuildSearchQueryParams) => string | undefined; searchResultsContainer?: {