import React, { useState } from 'react'; import { fromPairs, pipe, reverse, sortBy, splitEvery, toLower, toPairs, type, zipObj } from 'ramda'; import SortingDropdown from '../../utils/SortingDropdown'; import PaginationDropdown from '../../utils/PaginationDropdown'; import { OrderDir, rangeOf } from '../../utils/utils'; import { roundTen } from '../../utils/helpers/numbers'; import SimplePaginator from '../../common/SimplePaginator'; import { Stats, StatsRow } from '../types'; import GraphCard from './GraphCard'; import { DefaultChartProps } from './DefaultChart'; const toLowerIfString = (value: any) => type(value) === 'String' ? toLower(value) : value; const pickKeyFromPair = ([ key ]: StatsRow) => key; const pickValueFromPair = ([ , value ]: StatsRow) => value; interface SortableBarGraphProps extends DefaultChartProps { sortingItems: Record; withPagination?: boolean; extraHeaderContent?: Function; } const SortableBarGraph = ({ stats, highlightedStats, title, sortingItems, extraHeaderContent, withPagination = true, ...rest }: SortableBarGraphProps) => { const [ order, setOrder ] = useState<{ orderField?: string; orderDir?: OrderDir }>({ orderField: undefined, orderDir: undefined, }); const [ currentPage, setCurrentPage ] = useState(1); const [ itemsPerPage, setItemsPerPage ] = useState(50); const getSortedPairsForStats = (stats: Stats, sortingItems: Record) => { const pairs = toPairs(stats); const sortedPairs = !order.orderField ? pairs : sortBy( pipe( order.orderField === Object.keys(sortingItems)[0] ? pickKeyFromPair : pickValueFromPair, toLowerIfString, ), pairs, ); return !order.orderDir || order.orderDir === 'ASC' ? sortedPairs : reverse(sortedPairs); }; 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 return [ ...page, ...rangeOf(firstPageLength - page.length, (i): StatsRow => [ `hidden_${i}`, 0 ]) ]; }; const renderPagination = (pagesCount: number) => ; const determineStats = (stats: Stats, highlightedStats: Stats | undefined, sortingItems: Record) => { const sortedPairs = getSortedPairsForStats(stats, sortingItems); const sortedKeys = sortedPairs.map(pickKeyFromPair); // The highlighted stats have to be ordered based on the regular stats, not on its own values const sortedHighlightedPairs = highlightedStats && toPairs( { ...zipObj(sortedKeys, sortedKeys.map(() => 0)), ...highlightedStats }, ); if (sortedPairs.length <= itemsPerPage) { return { currentPageStats: fromPairs(sortedPairs), currentPageHighlightedStats: sortedHighlightedPairs && fromPairs(sortedHighlightedPairs), }; } const pages = splitEvery(itemsPerPage, sortedPairs); const highlightedPages = sortedHighlightedPairs && splitEvery(itemsPerPage, sortedHighlightedPairs); return { currentPageStats: fromPairs(determineCurrentPagePairs(pages)), currentPageHighlightedStats: highlightedPages && fromPairs(determineCurrentPagePairs(highlightedPages)), pagination: renderPagination(pages.length), max: roundTen(Math.max(...sortedPairs.map(pickValueFromPair))), }; }; const { currentPageStats, currentPageHighlightedStats, pagination, max } = determineStats( stats, highlightedStats && Object.keys(highlightedStats).length > 0 ? highlightedStats : undefined, sortingItems, ); const activeCities = Object.keys(currentPageStats); const computeTitle = () => ( {title}
{ setOrder({ orderField, orderDir }); setCurrentPage(1); }} />
{withPagination && Object.keys(stats).length > 50 && (
{ setItemsPerPage(itemsPerPage); setCurrentPage(1); }} />
)} {extraHeaderContent && (
{extraHeaderContent(pagination ? activeCities : undefined)}
)}
); return ( ); }; export default SortableBarGraph;