src/visits/helpers/OpenMapModalBtn.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 61
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 51
mnd 2
bc 2
fnc 0
dl 0
loc 61
ccs 21
cts 21
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
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 { useToggle } from '../../utils/helpers/hooks';
7
import './OpenMapModalBtn.scss';
8
9 1
const propTypes = {
10
  modalTitle: PropTypes.string.isRequired,
11
  locations: PropTypes.arrayOf(PropTypes.object),
12
  activeCities: PropTypes.arrayOf(PropTypes.string),
13
};
14
15 1
const OpenMapModalBtn = (MapModal) => {
16 1
  const OpenMapModalBtn = ({ modalTitle, locations = [], activeCities }) => {
17 8
    const [ mapIsOpened, , openMap, closeMap ] = useToggle();
18 8
    const [ dropdownIsOpened, toggleDropdown, openDropdown ] = useToggle();
19 8
    const [ locationsToShow, setLocationsToShow ] = useState([]);
20
21 8
    const buttonRef = React.createRef();
22 8
    const filterLocations = (locations) => locations.filter(({ cityName }) => activeCities.includes(cityName));
23 8
    const onClick = () => {
24 3
      if (!activeCities) {
25 1
        setLocationsToShow(locations);
26 1
        openMap();
27
28 1
        return;
29
      }
30
31 2
      openDropdown();
32
    };
33 16
    const openMapWithLocations = (filtered) => () => {
34 2
      setLocationsToShow(filtered ? filterLocations(locations) : locations);
35 1
      openMap();
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
        <Dropdown isOpen={dropdownIsOpened} toggle={toggleDropdown} 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={closeMap} 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