From 5233f5a07b43043a4f1b81c4e05979f3aeb44131 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 10 Mar 2019 12:09:54 +0100 Subject: [PATCH] Updated OpenMapModalBtn so that it allows showing only active cities --- src/visits/ShortUrlVisits.js | 11 ++-- src/visits/SortableBarGraph.js | 8 +-- src/visits/helpers/MapModal.js | 4 ++ src/visits/helpers/OpenMapModalBtn.js | 77 ++++++++++++++++++--------- 4 files changed, 62 insertions(+), 38 deletions(-) diff --git a/src/visits/ShortUrlVisits.js b/src/visits/ShortUrlVisits.js index 9c74356f..1de3c183 100644 --- a/src/visits/ShortUrlVisits.js +++ b/src/visits/ShortUrlVisits.js @@ -116,14 +116,9 @@ const ShortUrlVisits = ({ processStatsFromVisits }) => class ShortUrlVisits exte - mapLocations.length > 0 && ( - - ) + extraHeaderContent={(activeCities) => + mapLocations.length > 0 && + } sortingItems={{ name: 'City name', diff --git a/src/visits/SortableBarGraph.js b/src/visits/SortableBarGraph.js index 2d0d5e60..4096467a 100644 --- a/src/visits/SortableBarGraph.js +++ b/src/visits/SortableBarGraph.js @@ -96,11 +96,7 @@ export default class SortableBarGraph extends React.Component { render() { const { stats, sortingItems, title, extraHeaderContent, withPagination = true } = this.props; const { currentPageStats, pagination, max } = this.determineStats(stats, sortingItems); - const filterLocations = (locations) => { - const validCities = keys(currentPageStats); - - return locations.filter(({ cityName }) => validCities.includes(cityName)); - }; + const activeCities = keys(currentPageStats); const computedTitle = ( {title} @@ -129,7 +125,7 @@ export default class SortableBarGraph extends React.Component { )} {extraHeaderContent && (
- {extraHeaderContent(pagination ? filterLocations : undefined)} + {extraHeaderContent(pagination ? activeCities : undefined)}
)}
diff --git a/src/visits/helpers/MapModal.js b/src/visits/helpers/MapModal.js index 6a616bca..45cc7304 100644 --- a/src/visits/helpers/MapModal.js +++ b/src/visits/helpers/MapModal.js @@ -27,6 +27,10 @@ const OpenStreetMapTile = () => ( ); const calculateMapProps = (locations) => { + if (locations.length === 0) { + return {}; + } + if (locations.length > 1) { return { bounds: locations.map(prop('latLong')) }; } diff --git a/src/visits/helpers/OpenMapModalBtn.js b/src/visits/helpers/OpenMapModalBtn.js index 18a88f4f..2289ee06 100644 --- a/src/visits/helpers/OpenMapModalBtn.js +++ b/src/visits/helpers/OpenMapModalBtn.js @@ -1,34 +1,63 @@ -import React from 'react'; +import React, { useState } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faMapMarkedAlt as mapIcon } from '@fortawesome/free-solid-svg-icons'; -import { UncontrolledTooltip } from 'reactstrap'; +import { Dropdown, DropdownItem, DropdownMenu, UncontrolledTooltip } from 'reactstrap'; import * as PropTypes from 'prop-types'; import MapModal from './MapModal'; import './OpenMapModalBtn.scss'; -export default class OpenMapModalBtn extends React.Component { - static propTypes = { - modalTitle: PropTypes.string.isRequired, - locations: PropTypes.arrayOf(PropTypes.object), - filterLocations: PropTypes.func, +const propTypes = { + modalTitle: PropTypes.string.isRequired, + locations: PropTypes.arrayOf(PropTypes.object), + activeCities: PropTypes.arrayOf(PropTypes.string), +}; + +const OpenMapModalBtn = ({ modalTitle, locations = [], activeCities }) => { + const [ mapIsOpened, setMapIsOpened ] = useState(false); + const [ dropdownIsOpened, setDropdownIsOpened ] = useState(false); + const [ locationsToShow, setLocationsToShow ] = useState([]); + + const buttonRef = React.createRef(); + const filterLocations = (locations) => locations.filter(({ cityName }) => activeCities.includes(cityName)); + const toggleMap = () => setMapIsOpened(!mapIsOpened); + const onClick = () => { + if (mapIsOpened) { + setMapIsOpened(false); + + return; + } + + if (!activeCities) { + setLocationsToShow(locations); + setMapIsOpened(true); + + return; + } + + setDropdownIsOpened(true); + }; + const openMapWithLocations = (filtered) => () => { + setLocationsToShow(filtered ? filterLocations(locations) : locations); + setMapIsOpened(true); }; - state = { mapIsOpened: false }; + return ( + + + buttonRef.current}>Show in map + setDropdownIsOpened(!dropdownIsOpened)} inNavbar> + + Show all locations + Show locations in current page + + + + + ); +}; - render() { - const { modalTitle, locations = [], filterLocations } = this.props; - const toggleMap = () => this.setState(({ mapIsOpened }) => ({ mapIsOpened: !mapIsOpened })); - const buttonRef = React.createRef(); - const filteredLocations = filterLocations ? filterLocations(locations) : locations; +OpenMapModalBtn.propTypes = propTypes; - return ( - - - buttonRef.current}>Show in map - - - ); - } -} +export default OpenMapModalBtn;