mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 13:36:20 +00:00
Improved useToggle hook so that it also returns enabler and disabler
This commit is contained in:
@@ -3,6 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import { faMapMarkedAlt as mapIcon } from '@fortawesome/free-solid-svg-icons';
|
||||
import { Dropdown, DropdownItem, DropdownMenu, UncontrolledTooltip } from 'reactstrap';
|
||||
import * as PropTypes from 'prop-types';
|
||||
import { useToggle } from '../../utils/helpers/hooks';
|
||||
import './OpenMapModalBtn.scss';
|
||||
|
||||
const propTypes = {
|
||||
@@ -13,26 +14,25 @@ const propTypes = {
|
||||
|
||||
const OpenMapModalBtn = (MapModal) => {
|
||||
const OpenMapModalBtn = ({ modalTitle, locations = [], activeCities }) => {
|
||||
const [ mapIsOpened, setMapIsOpened ] = useState(false);
|
||||
const [ dropdownIsOpened, setDropdownIsOpened ] = useState(false);
|
||||
const [ mapIsOpened, , openMap, closeMap ] = useToggle();
|
||||
const [ dropdownIsOpened, toggleDropdown, openDropdown ] = useToggle();
|
||||
const [ locationsToShow, setLocationsToShow ] = useState([]);
|
||||
|
||||
const buttonRef = React.createRef();
|
||||
const filterLocations = (locations) => locations.filter(({ cityName }) => activeCities.includes(cityName));
|
||||
const toggleMap = () => setMapIsOpened(!mapIsOpened);
|
||||
const onClick = () => {
|
||||
if (!activeCities) {
|
||||
setLocationsToShow(locations);
|
||||
setMapIsOpened(true);
|
||||
openMap();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
setDropdownIsOpened(true);
|
||||
openDropdown();
|
||||
};
|
||||
const openMapWithLocations = (filtered) => () => {
|
||||
setLocationsToShow(filtered ? filterLocations(locations) : locations);
|
||||
setMapIsOpened(true);
|
||||
openMap();
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -41,13 +41,13 @@ const OpenMapModalBtn = (MapModal) => {
|
||||
<FontAwesomeIcon icon={mapIcon} />
|
||||
</button>
|
||||
<UncontrolledTooltip placement="left" target={() => buttonRef.current}>Show in map</UncontrolledTooltip>
|
||||
<Dropdown isOpen={dropdownIsOpened} toggle={() => setDropdownIsOpened(!dropdownIsOpened)} inNavbar>
|
||||
<Dropdown isOpen={dropdownIsOpened} toggle={toggleDropdown} inNavbar>
|
||||
<DropdownMenu right>
|
||||
<DropdownItem onClick={openMapWithLocations(false)}>Show all locations</DropdownItem>
|
||||
<DropdownItem onClick={openMapWithLocations(true)}>Show locations in current page</DropdownItem>
|
||||
</DropdownMenu>
|
||||
</Dropdown>
|
||||
<MapModal toggle={toggleMap} isOpen={mapIsOpened} title={modalTitle} locations={locationsToShow} />
|
||||
<MapModal toggle={closeMap} isOpen={mapIsOpened} title={modalTitle} locations={locationsToShow} />
|
||||
</React.Fragment>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user