Skip to content

Commit

Permalink
added link to show all documents; added extra filters for landing page
Browse files Browse the repository at this point in the history
  • Loading branch information
zotya committed Mar 9, 2023
1 parent a94f081 commit cdb843a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 51 deletions.
131 changes: 83 additions & 48 deletions searchlib/components/LandingPage/TilesLandingPage.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { Tab, Menu, List } from 'semantic-ui-react';
import { Tab, Menu, List, Divider } from 'semantic-ui-react';
import useDeepCompareEffect from 'use-deep-compare-effect';
import { useAtom } from 'jotai';

import { showFacetsAsideAtom } from '@eeacms/search/state';
import { getFacetCounts } from './request';
import { getFacetCounts, getTotal } from './request';
import buildStateFacets from '@eeacms/search/lib/search/state/facets';
import { customOrder } from '@eeacms/search/lib/utils';
import { useLandingPageData, useLandingPageRequest } from './state';
Expand Down Expand Up @@ -45,6 +45,7 @@ const LandingPage = (props) => {
maxPerSection = 12,
sortField,
sortDirection,
filterForAllResults,
} = appConfig.initialView.tilesLandingPageParams;

const sectionFacetFields = sections.map((s) => s.facetField);
Expand All @@ -62,8 +63,7 @@ const LandingPage = (props) => {
const getTiles = (maxPerSection_default) => {
const maxPerSection =
activeSectionConfig.maxPerSection || maxPerSection_default;
let result = landingPageData?.[activeSection]?.[0]?.data || [];

let result = landingPageData?.facets?.[activeSection]?.[0]?.data || [];
if (activeSectionConfig.blacklist !== undefined) {
result = result.filter(
(res) => !activeSectionConfig.blacklist.includes(res.value),
Expand All @@ -87,9 +87,12 @@ const LandingPage = (props) => {

const { icon } = activeSectionConfig;

const total = landingPageData?.total;

useDeepCompareEffect(() => {
async function fetchFacets() {
let facets;
let total;
if (!isRequested) {
setIsRequested(true);
} else {
Expand All @@ -113,10 +116,11 @@ const LandingPage = (props) => {
sectionFacetFields,
);
facets = buildStateFacets(facetCounts, appConfig);
total = await getTotal(appConfig);
}

if (!landingPageData && facets) {
setLandingPageData(facets);
if (!landingPageData && facets && total) {
setLandingPageData({ facets, total });
}
}
if (!landingPageData) {
Expand All @@ -132,6 +136,13 @@ const LandingPage = (props) => {
sections,
]);

const fixedOnClickHandler = (conf) => {
//TODO: This is a temporary solution, until we find another way to detect if there was any interaction on the landing page
if (filterForAllResults) {
setFilter(filterForAllResults.field, filterForAllResults.value);
}
};

const panes = sections.map((section, index) => {
const tabIndex = index + 1;

Expand All @@ -153,51 +164,75 @@ const LandingPage = (props) => {
},
render: () => {
return (
<Tab.Pane>
<div className={`landing-page-cards ${activeSection}`}>
<List>
{sortedTiles(tiles, activeSectionConfig, appConfig).map(
(topic, index) => {
const onClickHandler = () => {
setFilter(
activeSection,
topic.value,
activeSectionConfig.filterType || 'any',
);
<>
<Tab.Pane>
<div className={`landing-page-cards ${activeSection}`}>
<List>
{sortedTiles(tiles, activeSectionConfig, appConfig).map(
(topic, index) => {
const onClickHandler = () => {
setFilter(
activeSection,
topic.value,
activeSectionConfig.filterType || 'any',
);

// apply configured default values
appConfig.facets
.filter((f) => f.field !== activeSection && f.default)
.forEach((facet) => {
facet.default.values.forEach((value) =>
setFilter(
facet.field,
value,
facet.default.type || 'any',
),
);
});
setSort(sortField, sortDirection);
setShowFacets(true);
};

// apply configured default values
appConfig.facets
.filter((f) => f.field !== activeSection && f.default)
.forEach((facet) => {
facet.default.values.forEach((value) =>
setFilter(
facet.field,
value,
facet.default.type || 'any',
),
);
});
setSort(sortField, sortDirection);
setShowFacets(true);
};

return (
<List.Item onClick={onClickHandler} key={index}>
<List.Content>
{icon ? <Icon {...icon} type={topic.value} /> : ''}
<Term term={topic.value} field={activeSection} />
<span className="count">
({topic.count}{' '}
{topic.count === 1 ? 'item' : 'items'})
</span>
</List.Content>
</List.Item>
);
},
)}
return (
<List.Item onClick={onClickHandler} key={index}>
<List.Content>
{icon ? <Icon {...icon} type={topic.value} /> : ''}
<Term term={topic.value} field={activeSection} />
<span className="count">
({topic.count}{' '}
{topic.count === 1 ? 'item' : 'items'})
</span>
</List.Content>
</List.Item>
);
},
)}
</List>
</div>
</Tab.Pane>
<Divider />
<div className="landing-page-cards">
<List>
<List.Item>
<List.Content>
<div
key="all_data"
tabIndex="-1"
role="button"
onKeyDown={fixedOnClickHandler}
onClick={fixedOnClickHandler}
>
<div className="extra content">
<span className="count">
Show all {total} documents
</span>
</div>
</div>
</List.Content>
</List.Item>
</List>
</div>
</Tab.Pane>
</>
);
},
};
Expand Down
18 changes: 15 additions & 3 deletions searchlib/components/LandingPage/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ function changeSizeToZero(body) {
}

export async function getFacetCounts(state, config, facetNames) {
let { default_filters } = config?.initialView?.tilesLandingPageParams;
default_filters = default_filters !== undefined ? default_filters : [];
const sf = state.filters.concat(default_filters);
const disjunctiveFacetNames = facetNames.filter((name) =>
state.filters.find((f) => f.field === name),
sf.find((f) => f.field === name),
);
const normalFacetNames = facetNames.filter(
(name) => disjunctiveFacetNames.indexOf(name) === -1,
);

// batch non-exact aggregations together
let body = buildRequest({ filters: [] }, config, true); // for normal we don't want filters
let body = buildRequest({ filters: default_filters }, config, true); // for normal, apply the default filters from config
body = changeSizeToZero(body);
body = removeAllFacetsExcept(body, normalFacetNames);
const normalRequest = runRequest(body, config);
Expand All @@ -72,3 +74,13 @@ export async function getFacetCounts(state, config, facetNames) {
);
return combineAggregationsFromResponses(responses);
}

export async function getTotal(config) {
let { default_filters } = config?.initialView?.tilesLandingPageParams;
default_filters = default_filters !== undefined ? default_filters : [];
let body = buildRequest({ filters: default_filters }, config, true);
body = changeSizeToZero(body);
const req = runRequest(body, config);
const resp = await req;
return resp?.body?.hits?.total?.value;
}

0 comments on commit cdb843a

Please sign in to comment.