|
1
|
|
|
import React from 'react'; |
|
2
|
|
|
import { Modal, ModalBody } from 'reactstrap'; |
|
3
|
|
|
import { Map, TileLayer, Marker, Popup } from 'react-leaflet'; |
|
4
|
|
|
import { prop } from 'ramda'; |
|
5
|
|
|
import * as PropTypes from 'prop-types'; |
|
6
|
|
|
import './MapModal.scss'; |
|
7
|
|
|
|
|
8
|
1 |
|
const propTypes = { |
|
9
|
|
|
toggle: PropTypes.func, |
|
10
|
|
|
isOpen: PropTypes.bool, |
|
11
|
|
|
title: PropTypes.string, |
|
12
|
|
|
locations: PropTypes.arrayOf(PropTypes.shape({ |
|
13
|
|
|
cityName: PropTypes.string.isRequired, |
|
14
|
|
|
latLong: PropTypes.arrayOf(PropTypes.number).isRequired, |
|
15
|
|
|
count: PropTypes.number.isRequired, |
|
16
|
|
|
})), |
|
17
|
|
|
}; |
|
18
|
1 |
|
const defaultProps = { |
|
19
|
|
|
locations: [], |
|
20
|
|
|
}; |
|
21
|
|
|
|
|
22
|
1 |
|
const OpenStreetMapTile = () => ( |
|
23
|
|
|
<TileLayer |
|
24
|
|
|
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' |
|
25
|
|
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
|
26
|
|
|
/> |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
1 |
|
const calculateMapProps = (locations) => { |
|
30
|
3 |
|
if (locations.length === 0) { |
|
31
|
|
|
return {}; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
if (locations.length > 1) { |
|
35
|
3 |
|
return { bounds: locations.map(prop('latLong')) }; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// When there's only one location, an error is thrown if trying to calculate the bounds. |
|
39
|
|
|
// When that happens, we use zoom and center as a workaround |
|
40
|
|
|
const [{ latLong: center }] = locations; |
|
41
|
|
|
|
|
42
|
|
|
return { zoom: '10', center }; |
|
43
|
|
|
}; |
|
44
|
|
|
|
|
45
|
1 |
|
const MapModal = ({ toggle, isOpen, title, locations }) => ( |
|
46
|
3 |
|
<Modal toggle={toggle} isOpen={isOpen} className="map-modal__modal" contentClassName="map-modal__modal-content"> |
|
47
|
|
|
<ModalBody className="map-modal__modal-body"> |
|
48
|
|
|
<h3 className="map-modal__modal-title"> |
|
49
|
|
|
{title} |
|
50
|
|
|
<button type="button" className="close" onClick={toggle}>×</button> |
|
51
|
|
|
</h3> |
|
52
|
|
|
<Map {...calculateMapProps(locations)}> |
|
53
|
|
|
<OpenStreetMapTile /> |
|
54
|
|
|
{locations.map(({ cityName, latLong, count }, index) => ( |
|
55
|
6 |
|
<Marker key={index} position={latLong}> |
|
56
|
|
|
<Popup><b>{count}</b> visit{count > 1 ? 's' : ''} from <b>{cityName}</b></Popup> |
|
57
|
|
|
</Marker> |
|
58
|
|
|
))} |
|
59
|
|
|
</Map> |
|
60
|
|
|
</ModalBody> |
|
61
|
|
|
</Modal> |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
1 |
|
MapModal.propTypes = propTypes; |
|
65
|
1 |
|
MapModal.defaultProps = defaultProps; |
|
66
|
|
|
|
|
67
|
|
|
export default MapModal; |
|
68
|
|
|
|