|
1
|
|
|
import React, { useState } from 'react'; |
|
2
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
|
3
|
|
|
import { faMapMarkedAlt as mapIcon } from '@fortawesome/free-solid-svg-icons'; |
|
4
|
|
|
import { Dropdown, DropdownItem, DropdownMenu, UncontrolledTooltip } from 'reactstrap'; |
|
5
|
|
|
import * as PropTypes from 'prop-types'; |
|
6
|
|
|
import './OpenMapModalBtn.scss'; |
|
7
|
|
|
|
|
8
|
1 |
|
const propTypes = { |
|
9
|
|
|
modalTitle: PropTypes.string.isRequired, |
|
10
|
|
|
locations: PropTypes.arrayOf(PropTypes.object), |
|
11
|
|
|
activeCities: PropTypes.arrayOf(PropTypes.string), |
|
12
|
|
|
}; |
|
13
|
|
|
|
|
14
|
1 |
|
const OpenMapModalBtn = (MapModal) => { |
|
15
|
1 |
|
const OpenMapModalBtn = ({ modalTitle, locations = [], activeCities }) => { |
|
16
|
8 |
|
const [ mapIsOpened, setMapIsOpened ] = useState(false); |
|
17
|
8 |
|
const [ dropdownIsOpened, setDropdownIsOpened ] = useState(false); |
|
18
|
8 |
|
const [ locationsToShow, setLocationsToShow ] = useState([]); |
|
19
|
|
|
|
|
20
|
8 |
|
const buttonRef = React.createRef(); |
|
21
|
8 |
|
const filterLocations = (locations) => locations.filter(({ cityName }) => activeCities.includes(cityName)); |
|
22
|
8 |
|
const toggleMap = () => setMapIsOpened(!mapIsOpened); |
|
23
|
8 |
|
const onClick = () => { |
|
24
|
3 |
|
if (!activeCities) { |
|
25
|
1 |
|
setLocationsToShow(locations); |
|
26
|
1 |
|
setMapIsOpened(true); |
|
27
|
|
|
|
|
28
|
1 |
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
setDropdownIsOpened(true); |
|
32
|
|
|
}; |
|
33
|
16 |
|
const openMapWithLocations = (filtered) => () => { |
|
34
|
2 |
|
setLocationsToShow(filtered ? filterLocations(locations) : locations); |
|
35
|
1 |
|
setMapIsOpened(true); |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
8 |
|
return ( |
|
39
|
|
|
<React.Fragment> |
|
40
|
|
|
<button className="btn btn-link open-map-modal-btn__btn" ref={buttonRef} onClick={onClick}> |
|
41
|
|
|
<FontAwesomeIcon icon={mapIcon} /> |
|
42
|
|
|
</button> |
|
43
|
4 |
|
<UncontrolledTooltip placement="left" target={() => buttonRef.current}>Show in map</UncontrolledTooltip> |
|
44
|
1 |
|
<Dropdown isOpen={dropdownIsOpened} toggle={() => setDropdownIsOpened(!dropdownIsOpened)} inNavbar> |
|
45
|
|
|
<DropdownMenu right> |
|
46
|
|
|
<DropdownItem onClick={openMapWithLocations(false)}>Show all locations</DropdownItem> |
|
47
|
|
|
<DropdownItem onClick={openMapWithLocations(true)}>Show locations in current page</DropdownItem> |
|
48
|
|
|
</DropdownMenu> |
|
49
|
|
|
</Dropdown> |
|
50
|
|
|
<MapModal toggle={toggleMap} isOpen={mapIsOpened} title={modalTitle} locations={locationsToShow} /> |
|
51
|
|
|
</React.Fragment> |
|
52
|
|
|
); |
|
53
|
|
|
}; |
|
54
|
|
|
|
|
55
|
1 |
|
OpenMapModalBtn.propTypes = propTypes; |
|
56
|
|
|
|
|
57
|
1 |
|
return OpenMapModalBtn; |
|
58
|
|
|
}; |
|
59
|
|
|
|
|
60
|
|
|
export default OpenMapModalBtn; |
|
61
|
|
|
|