{"version":3,"file":"browserGeolocation-d731a977.js","sources":["../../../../app/frontend/components/directory/utilities/setCookie.js","../../../../app/frontend/components/directory/utilities/getCookie.js","../../../../app/frontend/components/directory/utilities/loadingUI.js","../../../../app/frontend/components/b2c/directory/browserGeolocation.js"],"sourcesContent":["export default function setCookie (name, value, days) {\n const d = new Date;\n d.setTime(d.getTime() + 24*60*60*1000*days);\n document.cookie = `${name}=${value};path=/;expires=${d.toGMTString()}`;\n}\n","export function getCookie (name) {\n const value = `; ${document.cookie}`;\n const parts = value.split(`; ${name}=`);\n if (parts.length === 2) return parts.pop().split(';').shift();\n}\n","const loadingUI = {\n start(message) {\n let node = this.createNodes(message);\n document.body.insertBefore(node, document.body.childNodes[0]);\n },\n\n stop() {\n let node = document.querySelector(\".loading-ui-element__container\");\n node.remove();\n },\n\n createNodes(message) {\n let node = document.createElement(\"div\");\n let container = document.createElement(\"div\");\n let paragraph = document.createElement(\"p\");\n let spinnerHolder = document.createElement(\"div\");\n\n spinnerHolder.classList.add(\"spinner-container\");\n container.classList.add(\"loading-ui-element__container\");\n\n paragraph.innerHTML = message;\n spinnerHolder.innerHTML = `\n \n \n \n `;\n\n container.appendChild(spinnerHolder);\n container.appendChild(paragraph);\n node.appendChild(container);\n node.classList.add(\"loading-ui-element\");\n\n return node;\n },\n};\n\nexport default loadingUI;\n","import setCookie from \"~/components/directory/utilities/setCookie\";\nimport { getCookie } from \"~/components/directory/utilities/getCookie\";\nimport loadingUI from \"~/components/directory/utilities/loadingUI\";\n\nexport async function getGeoLocationStatus() {\n const [lat, lng] = currentCookies();\n const geolocationTimeStamp = getCookie('geolocation_timestamp');\n // check if the coors as well as the timestamp are present\n // the timestamp acts as a way to make sure that the user location stays fresh\n if (!isNaN(lat) && !isNaN(lng) && geolocationTimeStamp !== null && geolocationTimeStamp !== undefined) {\n return 'fresh';\n } else if (await hasGeolocationEnabled()) {\n return updateLocationState();\n }\n return 'unavailable';\n}\n\nexport async function onGeolocationGranted(callback) {\n if ('geolocation' in navigator) {\n const result = await navigator.permissions.query({name: 'geolocation'});\n result.addEventListener('change', (e) => {\n if (e.target.state === 'granted') {\n return callback();\n }\n });\n }\n}\n\n\nexport async function setUpGeolocation() {\n const status = await getGeoLocationStatus();\n if (status === 'stale') {\n reloadPage();\n }\n return status;\n}\n\nfunction reloadPage() {\n loadingUI.start('Getting clinics nearest to you');\n window.location.href = `${window.location.pathname}?geo=1`;\n}\n\nasync function hasGeolocationEnabled() {\n if ('geolocation' in navigator) {\n const result = await navigator.permissions.query({name: 'geolocation'})\n return result.state !== 'denied';\n }\n return false;\n}\n\nasync function updateLocationState() {\n const previousLocation = currentCookies();\n setGeolocationTimeStamp();\n try {\n const currentLocation = await getCurrentLocation();\n if (hasPositionChanged(previousLocation, currentLocation)) {\n setPositionCookies(...currentLocation);\n return 'stale';\n }\n return 'fresh';\n\n } catch {\n return 'unavailable';\n }\n}\n\n// the geolocation expiry exists for the browser to check the user location after a certain length of time\n// this is in case the user location changes\n// it uses the current time as a placeholder value, but is not used.\nfunction setGeolocationTimeStamp() {\n setCookie('geolocation_timestamp', new Date(), 7);\n}\n\nfunction setPositionCookies(lat, lng) {\n if(lat !== null && lng !== null) {\n setCookie('lat', lat, 365);\n setCookie('lng', lng, 365);\n }\n}\n\nfunction currentCookies() {\n const lat = getCookie('lat');\n const lng = getCookie('lng');\n return [lat, lng].map(coor => parseFloat(coor))\n}\n\nasync function getCurrentLocation() {\n const extractCoords = (position) => ([position.coords.latitude, position.coords.longitude]);\n return new Promise((resolve, reject) => {\n navigator.geolocation.getCurrentPosition(\n position => resolve(extractCoords(position)),\n err => reject(err),\n );\n });\n}\n\nfunction hasPositionChanged(oldVal, newVal) {\n const [oldLat, oldLng] = oldVal;\n const [newLat, newLng] = newVal;\n\n if(isNaN(oldLng) || isNaN(oldLat)){\n return true;\n }\n\n const latChanged = Math.abs(oldLat - newLat) > 0.001\n const lngChanged = Math.abs(oldLng - newLng) > 0.001\n\n return latChanged || lngChanged;\n}\n\n\n\n"],"names":["setCookie","name","value","days","d","getCookie","parts","loadingUI","message","node","container","paragraph","spinnerHolder","getGeoLocationStatus","lat","lng","currentCookies","geolocationTimeStamp","hasGeolocationEnabled","updateLocationState","setUpGeolocation","status","reloadPage","previousLocation","setGeolocationTimeStamp","currentLocation","getCurrentLocation","hasPositionChanged","setPositionCookies","coor","extractCoords","position","resolve","reject","err","oldVal","newVal","oldLat","oldLng","newLat","newLng","latChanged","lngChanged"],"mappings":"AAAe,SAASA,EAAWC,EAAMC,EAAOC,EAAM,CACpD,MAAMC,EAAI,IAAI,KACdA,EAAE,QAAQA,EAAE,QAAS,EAAG,GAAG,GAAG,GAAG,IAAKD,CAAI,EAC1C,SAAS,OAAS,GAAGF,CAAI,IAAIC,CAAK,mBAAmBE,EAAE,YAAa,CAAA,EACtE,CCJO,SAASC,EAAWJ,EAAM,CAE/B,MAAMK,EADQ,KAAK,SAAS,MAAM,GACd,MAAM,KAAKL,CAAI,GAAG,EACtC,GAAIK,EAAM,SAAW,EAAG,OAAOA,EAAM,IAAK,EAAC,MAAM,GAAG,EAAE,OACxD,CCJA,MAAMC,EAAY,CAChB,MAAMC,EAAS,CACb,IAAIC,EAAO,KAAK,YAAYD,CAAO,EACnC,SAAS,KAAK,aAAaC,EAAM,SAAS,KAAK,WAAW,CAAC,CAAC,CAC7D,EAED,MAAO,CACM,SAAS,cAAc,gCAAgC,EAC7D,OAAM,CACZ,EAED,YAAYD,EAAS,CACnB,IAAIC,EAAO,SAAS,cAAc,KAAK,EACnCC,EAAY,SAAS,cAAc,KAAK,EACxCC,EAAY,SAAS,cAAc,GAAG,EACtCC,EAAgB,SAAS,cAAc,KAAK,EAEhD,OAAAA,EAAc,UAAU,IAAI,mBAAmB,EAC/CF,EAAU,UAAU,IAAI,+BAA+B,EAEvDC,EAAU,UAAYH,EACtBI,EAAc,UAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,aAa1BF,EAAU,YAAYE,C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