Skip to content

Commit

Permalink
display search results count
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Oct 21, 2024
1 parent 1828243 commit 29cdc45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/common/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export const useSearch = () => {
setFacets(selectedFacets);

setIsLoading(true);
clearPagination();
await getSearchSourceData?.();
await fetchData({
query: selectedQuery,
Expand Down
30 changes: 12 additions & 18 deletions src/components/ComplexLookupField/ModalComplexLookup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FC, memo, useCallback, useEffect, useMemo } from 'react';
import { useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil';
import { FC, memo, useCallback, useEffect } from 'react';
import { useResetRecoilState, useSetRecoilState } from 'recoil';
import classNames from 'classnames';
import { FormattedMessage, FormattedNumber } from 'react-intl';
import { getSearchResults } from '@common/api/search.api';
Expand Down Expand Up @@ -51,7 +51,6 @@ export const ModalComplexLookup: FC<ModalComplexLookupProps> = memo(
const setIsMarcPreviewOpen = useSetRecoilState(state.ui.isMarcPreviewOpen);
const setSearchQuery = useSetRecoilState(state.search.query);
const clearSearchQuery = useResetRecoilState(state.search.query);
const searchResultsMetadata = useRecoilValue(state.search.pageMetadata);
const setMarcMetadata = useSetRecoilState(state.data.marcPreviewMetadata);
const clearMarcMetadata = useResetRecoilState(state.data.marcPreviewMetadata);
const { getFacetsData, getSourceData } = useComplexLookupApi(api, filters);
Expand All @@ -77,29 +76,24 @@ export const ModalComplexLookup: FC<ModalComplexLookupProps> = memo(
onClose();
};

const searchControlsSubLabel = useMemo(
() =>
searchResultsMetadata?.totalElements ? (
<FormattedMessage
id={'ld.recordsFound'}
values={{
recordsCount: (
<FormattedNumber value={searchResultsMetadata?.totalElements} data-testid="records-found-count" />
),
}}
/>
) : undefined,
[searchResultsMetadata?.totalElements],
const renderSearchControlsSubLabel = (totalElements: number) => (
<FormattedMessage
id={'ld.recordsFound'}
values={{
recordsCount: <FormattedNumber value={totalElements} data-testid="records-found-count" />,
}}
/>
);

const renderSearchControlPane = useCallback(
() => (
<SearchControlPane
label={<FormattedMessage id={labels.modal.searchResults} />}
subLabel={searchControlsSubLabel}
renderSubLabel={renderSearchControlsSubLabel}
segmentsConfig={segments.primary}
/>
),
[labels.modal.searchResults, searchControlsSubLabel],
[labels.modal.searchResults],
);

const loadMarcData = useCallback(
Expand Down
24 changes: 21 additions & 3 deletions src/components/SearchControlPane/SearchControlPane.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import { FC } from 'react';
import { useRecoilValue } from 'recoil';
import classNames from 'classnames';
import { IS_EMBEDDED_MODE } from '@common/constants/build.constants';
import { useSearchContext } from '@common/hooks/useSearchContext';
import { SearchSegment } from '@common/constants/search.constants';
import state from '@state';
import './SearchControlPane.scss';

type SearchControlPaneProps = {
children?: ReactElement;
label: string | ReactElement;
subLabel?: string | ReactElement;
segmentsConfig: PrimarySegmentsConfig;
renderSubLabel?: (count: number) => ReactElement;
renderCloseButton?: () => ReactElement;
};

export const SearchControlPane: FC<SearchControlPaneProps> = ({ children, label, subLabel, renderCloseButton }) => {
export const SearchControlPane: FC<SearchControlPaneProps> = ({
children,
label,
renderSubLabel,
renderCloseButton,
segmentsConfig,
}) => {
const searchResultsMetadata = useRecoilValue(state.search.pageMetadata);
const { navigationSegment } = useSearchContext();
const selectedSegment = navigationSegment?.value;
const isVisibleSubLabel = segmentsConfig[selectedSegment as SearchSegment]?.isVisibleSubLabel;

return (
<div className={classNames(['search-control-pane', IS_EMBEDDED_MODE && 'search-control-pane-embedded'])}>
{renderCloseButton?.()}
<div className="search-control-pane-title">
<div className="search-control-pane-mainLabel">{label}</div>
{subLabel && <div className="search-control-pane-subLabel">{subLabel}</div>}
{isVisibleSubLabel && !!renderSubLabel && (
<div className="search-control-pane-subLabel">{renderSubLabel?.(searchResultsMetadata?.totalElements)}</div>
)}
</div>
{children}
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/configs/complexLookup/complexLookup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ export const COMPLEX_LOOKUPS_CONFIG: ComplexLookupsConfig = {
type: SearchSegment.Search,
labelId: 'ld.search',
isVisiblePaginationCount: true,
isVisibleSubLabel: true,
},
[SearchSegment.Browse]: {
type: SearchSegment.Browse,
labelId: 'ld.browse',
isVisiblePaginationCount: false,
isVisibleSubLabel: false,
},
},
defaultValues: {
Expand Down
1 change: 1 addition & 0 deletions src/types/complexLookup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type SearchSegmentConfig = {
type: SearchSegment;
labelId: string;
isVisiblePaginationCount?: boolean;
isVisibleSubLabel?: boolean;
};

type PrimarySegmentsConfig = { [key in SearchSegment]: SearchSegmentConfig };
Expand Down

0 comments on commit 29cdc45

Please sign in to comment.