UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

61 lines (51 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCenterMap = useCenterMap; exports.useMapBounds = useMapBounds; exports.usePrintMedia = usePrintMedia; var _leaflet = require("leaflet"); var _react = require("react"); var _hooks = require("../components/MapProvider/hooks"); var _usehooksTs = require("usehooks-ts"); var _lodash = require("lodash"); var _reactLeaflet = require("react-leaflet"); function useMapBounds() { const { orgUnits } = (0, _hooks.useMapOrganisationUnit)(); const geoJSONObject = (0, _react.useMemo)(() => (0, _leaflet.geoJSON)({ type: "FeatureCollection", features: orgUnits === null || orgUnits === void 0 ? void 0 : orgUnits.map(orgUnit => orgUnit.geoJSON) }), [orgUnits]); const center = (0, _react.useMemo)(() => { return geoJSONObject.getBounds().getCenter(); }, [orgUnits]); const bounds = (0, _react.useMemo)(() => { return geoJSONObject.getBounds(); }, [orgUnits]); return { center, bounds }; } function useCenterMap(_ref) { let { bounds } = _ref; const map = (0, _reactLeaflet.useMap)(); const [ref, { width, height }] = (0, _usehooksTs.useElementSize)(); (0, _react.useEffect)(() => { if (!(0, _lodash.isEmpty)(bounds)) { map.fitBounds(bounds); } }, [width, height]); return ref; } function usePrintMedia() { return (0, _usehooksTs.useMediaQuery)("@media print"); }