Skip to content

Commit

Permalink
update search query
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Oct 23, 2024
1 parent b8e3d8a commit f9eb350
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/common/api/search.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,28 @@ export const getSearchData = (url?: string, urlParams?: Record<string, string>)
};

export const getSearchResults = async (params: Record<string, string | number>) => {
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<string, string> | 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 {
Expand Down
2 changes: 2 additions & 0 deletions src/common/constants/search.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/common/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ 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';
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 {
Expand All @@ -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);
Expand Down Expand Up @@ -130,6 +131,7 @@ export const useSearch = () => {
value: updatedQuery,
}) as string)
: (updatedQuery as string);
const isBrowseSearch = selectedNavigationSegment === SearchSegment.Browse;

const result = fetchSearchResults
? await fetchSearchResults({
Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions src/components/ComplexLookupField/ModalComplexLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const ModalComplexLookup: FC<ModalComplexLookupProps> = memo(
fetchSearchResults={getSearchResults}
buildSearchQuery={buildSearchQuery}
searchResultsLimit={api.searchQuery.limit}
precedingRecordsCount={api.searchQuery.precedingRecordsCount}
searchResultsContainer={api.results.containers}
onAssignRecord={onAssign}
/>
Expand Down
1 change: 1 addition & 0 deletions src/configs/complexLookup/complexLookup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const COMPLEX_LOOKUPS_CONFIG: ComplexLookupsConfig = {
sourceKey: 'authoritySourceFiles',
searchQuery: {
limit: 100,
precedingRecordsCount: 5,
},
results: {
containers: {
Expand Down
3 changes: 2 additions & 1 deletion src/types/complexLookup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ComplexLookupApiEntryConfig = {
searchQuery: {
filter?: string;
limit?: number;
precedingRecordsCount?: number;
};
results: {
containers: {
Expand Down Expand Up @@ -93,4 +94,4 @@ type BuildSearchQueryParams = {
selector?: SearchableIndexQuerySelectorType;
searchBy: SearchableIndexType;
value: string;
}
};
1 change: 1 addition & 0 deletions src/types/search.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type SearchParams = {
getSearchSourceData?: (url?: string) => Promise<void>;
getSearchFacetsData?: (facet?: string, isOpen?: boolean) => Promise<void>;
searchResultsLimit?: number;
precedingRecordsCount?: number;
fetchSearchResults?: (params: any) => Promise<SearchResults>;
buildSearchQuery?: (params: BuildSearchQueryParams) => string | undefined;
searchResultsContainer?: {
Expand Down

0 comments on commit f9eb350

Please sign in to comment.