{"version":3,"sources":["components/LeafletMap/index.tsx"],"names":["LeafletMap","props","icon","L","iconSize","iconUrl","getAssetUrl","mapRef","useRef","coordinates","zoom","position","setPosition","useState","updateCount","setUpdateCount","isRendering","getCoords","__awaiter","data","address","axios","get","e","console","log","lat","lon","updateMap","setTimeout","current","leafletElement","invalidateSize","useEffect","hasWindow","window","addEventListener","removeEventListener","React","createElement","Map","center","ref","TileLayer","attribution","url","Marker","Fragment"],"mappings":"kgBAoFeA,UAxEKC,IACnB,MAAMC,EAAOC,IAAED,KAAK,CACnBE,SAAU,CAAC,GAAI,IACfC,QAASC,YAAY,0CAGhBC,EAASC,iBAAO,OAEhB,YAAEC,EAAW,KAAEC,EAAO,IAAOT,GAC5BU,EAAUC,GAAeC,mBAASJ,IAClCK,EAAaC,GAAkBF,mBAAS,GAC/C,IAAIG,GAAuB,EAE3B,MAUMC,EAAYA,IAAWC,OAAA,6BAC5B,MAAMC,QAXkCC,EAWKnB,EAAMmB,QAXQF,OAAA,6BAC3D,IAEC,aADuBG,IAAMC,IAAI,4DAA8DF,IAC/ED,KACf,MAAOI,GAER,MADAC,QAAQC,IAAI,8BAA+BF,GACrCA,OANiCH,MAYxC,GAAID,EAAK,GAAI,CACZ,MAAM,IAAEO,EAAG,IAAEC,GAAQR,EAAK,GACtBH,GACHJ,EAAY,CAACc,EAAKC,QAKfC,EAAYA,KACjBC,WAAW,KACVtB,EAAOuB,QAAQC,eAAeC,kBAC5B,IAwBJ,OArBAC,oBAAU,KACLjB,IACEP,GAAgBA,EAAY,IAAOA,EAAY,IACnDQ,KAIEiB,KAEHC,OAAOC,iBAAiB,YAAaR,GAG/B,KACNZ,GAAc,EAEVkB,KACHC,OAAOE,oBAAoB,YAAaT,KAGxC,IAEEjB,GAAaA,EAAS,IAAOA,EAAS,GAK1C2B,IAAAC,cAACC,IAAG,CAACC,OAAQ9B,EAAUD,KAAMA,EAAMgC,IAAKnC,GACvC+B,IAAAC,cAACI,IAAS,CAACC,YAAY,wFAAuFC,IAAI,uDAClHP,IAAAC,cAACO,IAAM,CAACnC,SAAUA,EAAUT,KAAMA,KAN5BoC,IAAAC,cAAAD,IAAAS,SAAA","file":"assets/js/11.chunk.js?cb=35bd9de2bca6c380fbd3","sourcesContent":["import React, { useEffect, useState, useRef } from \"react\";\nimport { Map, Marker, TileLayer } from \"react-leaflet\";\nimport L from \"leaflet\";\nimport axios from \"axios\";\nimport { getAssetUrl, hasWindow } from \"@utils\";\n\ninterface IProps {\n\taddress?: string;\n\tcoordinates?: number[];\n\tzoom?: number;\n}\n\nconst LeafletMap = (props: IProps) => {\n\tconst icon = L.icon({\n\t\ticonSize: [25, 41],\n\t\ticonUrl: getAssetUrl(\"/assets/images/icons/marker-icon.png\"),\n\t});\n\n\tconst mapRef = useRef(null);\n\n\tconst { coordinates, zoom = 15 } = props;\n\tconst [position, setPosition] = useState(coordinates);\n\tconst [updateCount, setUpdateCount] = useState(0);\n\tlet isRendering: boolean = true;\n\n\tconst getCoordinatesFromAddress = async (address: string) => {\n\t\ttry {\n\t\t\tconst response = await axios.get(\"https://nominatim.openstreetmap.org/search?format=json&q=\" + address);\n\t\t\treturn response.data;\n\t\t} catch (e) {\n\t\t\tconsole.log(\"Error while getting address\", e);\n\t\t\tthrow e;\n\t\t}\n\t};\n\n\tconst getCoords = async () => {\n\t\tconst data = await getCoordinatesFromAddress(props.address);\n\t\tif (data[0]) {\n\t\t\tconst { lat, lon } = data[0];\n\t\t\tif (isRendering) {\n\t\t\t\tsetPosition([lat, lon]);\n\t\t\t}\n\t\t}\n\t};\n\n\tconst updateMap = () => {\n\t\tsetTimeout(() => {\n\t\t\tmapRef.current.leafletElement.invalidateSize();\n\t\t}, 0);\n\t};\n\n\tuseEffect(() => {\n\t\tif (isRendering) {\n\t\t\tif (!coordinates || !coordinates[0] || !coordinates[1]) {\n\t\t\t\tgetCoords();\n\t\t\t}\n\t\t}\n\n\t\tif (hasWindow) {\n\t\t\t/* update map when tab changed so tiles are drawn correctly */\n\t\t\twindow.addEventListener(\"tabChange\", updateMap);\n\t\t}\n\n\t\treturn () => {\n\t\t\tisRendering = false;\n\n\t\t\tif (hasWindow) {\n\t\t\t\twindow.removeEventListener(\"tabChange\", updateMap);\n\t\t\t}\n\t\t};\n\t}, []);\n\n\tif (!position || !position[0] || !position[1]) {\n\t\treturn <></>;\n\t}\n\n\treturn (\n\t\t<Map center={position} zoom={zoom} ref={mapRef}>\n\t\t\t<TileLayer attribution='&copy; <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMapin</a> tekijät' url=\"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png\" />\n\t\t\t<Marker position={position} icon={icon} />\n\t\t</Map>\n\t);\n};\n\nexport default LeafletMap;\n"],"sourceRoot":""}