Skip to content

Commit

Permalink
Merge pull request #329 from visualize-admin/feat/nav-counts-search-d…
Browse files Browse the repository at this point in the history
…ependent

feat: Display counts per theme / organisations depending on search
  • Loading branch information
ptbrowne authored Feb 7, 2022
2 parents c6fec51 + f1bf34d commit a1ffd15
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions app/configurator/components/dataset-browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import SvgIcOrganisations from "../../icons/components/IcOrganisations";
import { useLocale } from "../../locales/use-locale";
import { BrowseParams } from "../../pages/browse";
import isAttrEqual from "../../utils/is-attr-equal";
import truthy from "../../utils/truthy";
import Tag from "./Tag";
import { useFormatDate } from "./ui-helpers";
import useDatasetCount from "./use-dataset-count";
Expand Down Expand Up @@ -634,17 +635,37 @@ export const NavSectionTitle = ({
);
};

export const SearchFilters = () => {
export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
const locale = useLocale();
const { filters } = useBrowseContext();
const { filters, search } = useBrowseContext();
const [{ data: allThemes }] = useThemesQuery({
variables: { locale },
});
const [{ data: allOrgs }] = useOrganizationsQuery({
variables: { locale },
});

const counts = useDatasetCount(filters);
const allCounts = useDatasetCount(filters);
const resultsCounts = useMemo(() => {
if (!data?.dataCubes) {
return {};
} else {
const res = {} as Record<string, number>;
for (const cube of data.dataCubes) {
const countables = [
...cube.dataCube.themes,
cube.dataCube.creator,
].filter(truthy);
for (const item of countables) {
res[item.iri] = res[item.iri] || 0;
res[item.iri] += 1;
}
}
return res;
}
}, [data]);

const counts = search && search != "" ? resultsCounts : allCounts;

const themeFilter = filters.find(isAttrEqual("__typename", "DataCubeTheme"));
const orgFilter = filters.find(
Expand Down
2 changes: 1 addition & 1 deletion app/configurator/components/select-dataset-step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const SelectDatasetStepContent = () => {
<DataSetMetadata sx={{ mt: "3rem" }} dataSetIri={dataset} />
</>
) : (
<SearchFilters />
<SearchFilters data={data} />
)}
</PanelLeftWrapper>
<PanelMiddleWrapper
Expand Down

0 comments on commit a1ffd15

Please sign in to comment.