2022-06-11 19:10:08 +03:00
|
|
|
import { FC, ReactNode, useState } from 'react';
|
2020-09-04 19:43:26 +03:00
|
|
|
import { fromPairs, pipe, reverse, sortBy, splitEvery, toLower, toPairs, type, zipObj } from 'ramda';
|
2021-11-01 14:48:11 +03:00
|
|
|
import { rangeOf } from '../../utils/utils';
|
2021-11-06 14:26:20 +03:00
|
|
|
import { Order } from '../../utils/helpers/ordering';
|
2022-05-06 22:20:14 +03:00
|
|
|
import { SimplePaginator } from '../../common/SimplePaginator';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { roundTen } from '../../utils/helpers/numbers';
|
2021-12-25 12:31:48 +03:00
|
|
|
import { OrderingDropdown } from '../../utils/OrderingDropdown';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { PaginationDropdown } from '../../utils/PaginationDropdown';
|
2020-09-04 19:43:26 +03:00
|
|
|
import { Stats, StatsRow } from '../types';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { HorizontalBarChart, HorizontalBarChartProps } from './HorizontalBarChart';
|
|
|
|
import { ChartCard } from './ChartCard';
|
2019-01-07 15:35:14 +03:00
|
|
|
|
2021-09-18 20:05:28 +03:00
|
|
|
interface SortableBarChartCardProps extends Omit<HorizontalBarChartProps, 'max'> {
|
2021-09-18 13:07:05 +03:00
|
|
|
title: Function | string;
|
2020-09-04 19:43:26 +03:00
|
|
|
sortingItems: Record<string, string>;
|
|
|
|
withPagination?: boolean;
|
2022-06-11 19:10:08 +03:00
|
|
|
extraHeaderContent?: (activeCities?: string[]) => ReactNode;
|
2020-09-04 19:43:26 +03:00
|
|
|
}
|
2018-10-29 00:54:08 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
const toLowerIfString = (value: any) => (type(value) === 'String' ? toLower(value) : value);
|
|
|
|
const pickKeyFromPair = ([key]: StatsRow) => key;
|
|
|
|
const pickValueFromPair = ([, value]: StatsRow) => value;
|
2021-09-18 20:05:28 +03:00
|
|
|
|
|
|
|
export const SortableBarChartCard: FC<SortableBarChartCardProps> = ({
|
2020-05-31 12:36:56 +03:00
|
|
|
stats,
|
|
|
|
highlightedStats,
|
|
|
|
title,
|
|
|
|
sortingItems,
|
|
|
|
extraHeaderContent,
|
|
|
|
withPagination = true,
|
|
|
|
...rest
|
2021-09-18 20:05:28 +03:00
|
|
|
}) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const [order, setOrder] = useState<Order<string>>({});
|
|
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
|
|
const [itemsPerPage, setItemsPerPage] = useState(50);
|
2019-03-10 10:28:14 +03:00
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
const getSortedPairsForStats = (statsToSort: Stats, sorting: Record<string, string>) => {
|
|
|
|
const pairs = toPairs(statsToSort);
|
2021-11-06 14:26:20 +03:00
|
|
|
const sortedPairs = !order.field ? pairs : sortBy(
|
2020-09-04 19:43:26 +03:00
|
|
|
pipe<StatsRow, string | number, string | number>(
|
2022-03-26 14:17:42 +03:00
|
|
|
order.field === Object.keys(sorting)[0] ? pickKeyFromPair : pickValueFromPair,
|
2020-08-22 09:10:31 +03:00
|
|
|
toLowerIfString,
|
2019-03-10 10:28:14 +03:00
|
|
|
),
|
2020-08-22 09:10:31 +03:00
|
|
|
pairs,
|
2019-03-10 10:28:14 +03:00
|
|
|
);
|
|
|
|
|
2021-11-06 14:26:20 +03:00
|
|
|
return !order.dir || order.dir === 'ASC' ? sortedPairs : reverse(sortedPairs);
|
2020-05-31 12:36:56 +03:00
|
|
|
};
|
2020-09-04 19:43:26 +03:00
|
|
|
const determineCurrentPagePairs = (pages: StatsRow[][]): StatsRow[] => {
|
|
|
|
const page = pages[currentPage - 1];
|
|
|
|
|
|
|
|
if (currentPage < pages.length) {
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstPageLength = pages[0].length;
|
|
|
|
|
|
|
|
// Using the "hidden" key, the chart will just replace the label by an empty string
|
2022-03-26 14:17:42 +03:00
|
|
|
return [...page, ...rangeOf(firstPageLength - page.length, (i): StatsRow => [`hidden_${i}`, 0])];
|
2020-09-04 19:43:26 +03:00
|
|
|
};
|
|
|
|
const renderPagination = (pagesCount: number) =>
|
|
|
|
<SimplePaginator currentPage={currentPage} pagesCount={pagesCount} setCurrentPage={setCurrentPage} />;
|
2022-03-26 14:17:42 +03:00
|
|
|
const determineStats = (statsToSort: Stats, sorting: Record<string, string>, theHighlightedStats?: Stats) => {
|
|
|
|
const sortedPairs = getSortedPairsForStats(statsToSort, sorting);
|
2020-04-10 15:38:31 +03:00
|
|
|
const sortedKeys = sortedPairs.map(pickKeyFromPair);
|
|
|
|
// The highlighted stats have to be ordered based on the regular stats, not on its own values
|
2022-03-26 14:17:42 +03:00
|
|
|
const sortedHighlightedPairs = theHighlightedStats && toPairs(
|
|
|
|
{ ...zipObj(sortedKeys, sortedKeys.map(() => 0)), ...theHighlightedStats },
|
2020-04-10 15:38:31 +03:00
|
|
|
);
|
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
if (sortedPairs.length <= itemsPerPage) {
|
2020-04-10 15:38:31 +03:00
|
|
|
return {
|
|
|
|
currentPageStats: fromPairs(sortedPairs),
|
|
|
|
currentPageHighlightedStats: sortedHighlightedPairs && fromPairs(sortedHighlightedPairs),
|
|
|
|
};
|
2019-03-10 10:28:14 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 12:36:56 +03:00
|
|
|
const pages = splitEvery(itemsPerPage, sortedPairs);
|
|
|
|
const highlightedPages = sortedHighlightedPairs && splitEvery(itemsPerPage, sortedHighlightedPairs);
|
2019-03-10 10:28:14 +03:00
|
|
|
|
|
|
|
return {
|
2020-05-31 12:36:56 +03:00
|
|
|
currentPageStats: fromPairs(determineCurrentPagePairs(pages)),
|
|
|
|
currentPageHighlightedStats: highlightedPages && fromPairs(determineCurrentPagePairs(highlightedPages)),
|
|
|
|
pagination: renderPagination(pages.length),
|
|
|
|
max: roundTen(Math.max(...sortedPairs.map(pickValueFromPair))),
|
2018-10-29 00:54:08 +03:00
|
|
|
};
|
2020-05-31 12:36:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const { currentPageStats, currentPageHighlightedStats, pagination, max } = determineStats(
|
|
|
|
stats,
|
2020-08-22 09:10:31 +03:00
|
|
|
sortingItems,
|
2022-03-26 14:17:42 +03:00
|
|
|
highlightedStats && Object.keys(highlightedStats).length > 0 ? highlightedStats : undefined,
|
2020-05-31 12:36:56 +03:00
|
|
|
);
|
2020-09-04 19:43:26 +03:00
|
|
|
const activeCities = Object.keys(currentPageStats);
|
2020-05-31 12:36:56 +03:00
|
|
|
const computeTitle = () => (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2020-05-31 12:36:56 +03:00
|
|
|
{title}
|
2022-03-05 15:26:28 +03:00
|
|
|
<div className="float-end">
|
2021-12-25 12:31:48 +03:00
|
|
|
<OrderingDropdown
|
2020-05-31 12:36:56 +03:00
|
|
|
isButton={false}
|
|
|
|
right
|
|
|
|
items={sortingItems}
|
2021-11-06 14:26:20 +03:00
|
|
|
order={order}
|
|
|
|
onChange={(field, dir) => {
|
|
|
|
setOrder({ field, dir });
|
2020-09-04 19:43:26 +03:00
|
|
|
setCurrentPage(1);
|
|
|
|
}}
|
2020-05-31 12:36:56 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2020-09-04 19:43:26 +03:00
|
|
|
{withPagination && Object.keys(stats).length > 50 && (
|
2022-03-05 15:26:28 +03:00
|
|
|
<div className="float-end">
|
2020-05-31 12:36:56 +03:00
|
|
|
<PaginationDropdown
|
2022-03-05 15:26:28 +03:00
|
|
|
toggleClassName="btn-sm p-0 me-3"
|
2022-03-26 14:17:42 +03:00
|
|
|
ranges={[50, 100, 200, 500]}
|
2020-05-31 12:36:56 +03:00
|
|
|
value={itemsPerPage}
|
2022-03-26 14:17:42 +03:00
|
|
|
setValue={(value) => {
|
|
|
|
setItemsPerPage(value);
|
2020-09-04 19:43:26 +03:00
|
|
|
setCurrentPage(1);
|
|
|
|
}}
|
2018-10-29 00:54:08 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2020-05-31 12:36:56 +03:00
|
|
|
)}
|
|
|
|
{extraHeaderContent && (
|
2022-03-05 15:26:28 +03:00
|
|
|
<div className="float-end">
|
2020-05-31 12:36:56 +03:00
|
|
|
{extraHeaderContent(pagination ? activeCities : undefined)}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-11-14 00:44:26 +03:00
|
|
|
</>
|
2020-05-31 12:36:56 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2021-09-18 20:05:28 +03:00
|
|
|
<ChartCard
|
2020-05-31 12:36:56 +03:00
|
|
|
title={computeTitle}
|
|
|
|
footer={pagination}
|
2021-09-18 20:05:28 +03:00
|
|
|
>
|
|
|
|
<HorizontalBarChart stats={currentPageStats} highlightedStats={currentPageHighlightedStats} max={max} {...rest} />
|
|
|
|
</ChartCard>
|
2020-05-31 12:36:56 +03:00
|
|
|
);
|
|
|
|
};
|