Updated cities chart so that map shows locations from current page when result set is paginated

This commit is contained in:
Alejandro Celaya 2019-03-10 10:56:36 +01:00
parent b6f6b1ae9d
commit 478ee59bb0
3 changed files with 25 additions and 8 deletions

View file

@ -116,9 +116,15 @@ const ShortUrlVisits = ({ processStatsFromVisits }) => class ShortUrlVisits exte
<SortableBarGraph <SortableBarGraph
stats={cities} stats={cities}
title="Cities" title="Cities"
extraHeaderContent={( extraHeaderContent={(filterLocations) =>
mapLocations.length > 0 && <OpenMapModalBtn modalTitle="Cities" locations={mapLocations} /> mapLocations.length > 0 && (
)} <OpenMapModalBtn
modalTitle="Cities"
locations={mapLocations}
filterLocations={filterLocations}
/>
)
}
sortingItems={{ sortingItems={{
name: 'City name', name: 'City name',
amount: 'Visits amount', amount: 'Visits amount',

View file

@ -16,7 +16,7 @@ export default class SortableBarGraph extends React.Component {
stats: PropTypes.object.isRequired, stats: PropTypes.object.isRequired,
title: PropTypes.string.isRequired, title: PropTypes.string.isRequired,
sortingItems: PropTypes.object.isRequired, sortingItems: PropTypes.object.isRequired,
extraHeaderContent: PropTypes.node, extraHeaderContent: PropTypes.func,
withPagination: PropTypes.bool, withPagination: PropTypes.bool,
}; };
@ -96,6 +96,11 @@ export default class SortableBarGraph extends React.Component {
render() { render() {
const { stats, sortingItems, title, extraHeaderContent, withPagination = true } = this.props; const { stats, sortingItems, title, extraHeaderContent, withPagination = true } = this.props;
const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems); const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems);
const filterLocations = (locations) => {
const validCities = keys(currentPageStats);
return locations.filter(({ cityName }) => validCities.includes(cityName));
};
const computedTitle = ( const computedTitle = (
<React.Fragment> <React.Fragment>
{title} {title}
@ -122,7 +127,11 @@ export default class SortableBarGraph extends React.Component {
/> />
</div> </div>
)} )}
{extraHeaderContent && <div className="float-right">{extraHeaderContent}</div>} {extraHeaderContent && (
<div className="float-right">
{extraHeaderContent(pagination ? filterLocations : undefined)}
</div>
)}
</React.Fragment> </React.Fragment>
); );

View file

@ -10,22 +10,24 @@ export default class OpenMapModalBtn extends React.Component {
static propTypes = { static propTypes = {
modalTitle: PropTypes.string.isRequired, modalTitle: PropTypes.string.isRequired,
locations: PropTypes.arrayOf(PropTypes.object), locations: PropTypes.arrayOf(PropTypes.object),
filterLocations: PropTypes.func,
}; };
state = { mapIsOpened: false }; state = { mapIsOpened: false };
render() { render() {
const { modalTitle, locations = [] } = this.props; const { modalTitle, locations = [], filterLocations } = this.props;
const toggleMap = () => this.setState(({ mapIsOpened }) => ({ mapIsOpened: !mapIsOpened })); const toggleMap = () => this.setState(({ mapIsOpened }) => ({ mapIsOpened: !mapIsOpened }));
const buttonRef = React.createRef(); const buttonRef = React.createRef();
const filteredLocations = filterLocations ? filterLocations(locations) : locations;
return ( return (
<React.Fragment> <React.Fragment>
<button className="btn btn-link open-map-modal-btn__btn" ref={buttonRef} onClick={toggleMap}> <button className="btn btn-link open-map-modal-btn__btn" ref={buttonRef} onClick={toggleMap}>
<FontAwesomeIcon icon={mapIcon} /> <FontAwesomeIcon icon={mapIcon} />
</button> </button>
<UncontrolledTooltip placement="bottom" target={() => buttonRef.current}>Show in map</UncontrolledTooltip> <UncontrolledTooltip placement="left" target={() => buttonRef.current}>Show in map</UncontrolledTooltip>
<MapModal toggle={toggleMap} isOpen={this.state.mapIsOpened} title={modalTitle} locations={locations} /> <MapModal toggle={toggleMap} isOpen={this.state.mapIsOpened} title={modalTitle} locations={filteredLocations} />
</React.Fragment> </React.Fragment>
); );
} }