{ "version": 3, "sources": ["../javascripts/actions/geo.ts"], "sourcesContent": ["import type { Dispatch as ReduxDispatch } from '@reduxjs/toolkit';\n\nimport type { BaseAction } from '../@types/actions';\nimport * as http from '../Http';\nimport requestCache from '../utils/request-cache';\n\nimport type { GoogleMapsGeocode } from '../@types/GoogleMaps';\nimport { buildGetRequest } from '../Http/requestTemplates';\nimport { get } from '../helpers/settings';\nimport { getMarket } from '../i18n';\n\nexport const RCV_LOCATION = 'RCV/LOCATION_FROM_IP';\ntype RCV_LOCATION_TYPE = BaseAction & {\n response: google.maps.places.PlaceResult | undefined;\n radius: number | null;\n};\n\nexport const REQ_LOCATION_FROM_POSTAL_CODE = 'REQ/LOCATION_FROM_POSTAL_CODE';\nexport const RCV_LOCATION_FROM_POSTAL_CODE = 'RCV/LOCATION_FROM_POSTAL_CODE';\ntype REQ_LOCATION_FROM_POSTAL_CODE_TYPE = BaseAction;\ntype RCV_LOCATION_FROM_POSTAL_CODE_TYPE = BaseAction & {\n response: google.maps.places.PlaceResult | undefined;\n};\n\nexport const REQ_LOCATION_FROM_SEARCH = 'REQ/LOCATION_FROM_SEARCH';\nexport const RCV_LOCATION_FROM_SEARCH = 'RCV/LOCATION_FROM_SEARCH';\ntype REQ_LOCATION_FROM_SEARCH_TYPE = BaseAction;\ntype RCV_LOCATION_FROM_SEARCH_TYPE = BaseAction & {\n response: google.maps.places.PlaceResult[];\n};\n\nexport const REQ_CLEAR_LOCATION_SEARCH = 'REQ/CLEAR_LOCATION_SEARCH';\ntype REQ_CLEAR_LOCATION_SEARCH_TYPE = BaseAction;\n\nexport const REQUEST_FAILED = 'LOCATION/REQUEST_FAILED';\ntype REQUEST_FAILED_TYPE = BaseAction & {\n action: typeof REQ_LOCATION_FROM_POSTAL_CODE | typeof REQ_LOCATION_FROM_SEARCH;\n};\n\nexport type GEO_ACTION =\n | RCV_LOCATION_TYPE\n | REQ_LOCATION_FROM_POSTAL_CODE_TYPE\n | RCV_LOCATION_FROM_POSTAL_CODE_TYPE\n | REQ_LOCATION_FROM_SEARCH_TYPE\n | RCV_LOCATION_FROM_SEARCH_TYPE\n | REQ_CLEAR_LOCATION_SEARCH_TYPE\n | REQUEST_FAILED_TYPE;\n\ntype Dispatch = ReduxDispatch;\n\nconst findIndex = (s: string[] | undefined, type: string) => {\n const index = s?.indexOf(type);\n return index !== undefined ? index > -1 : false;\n};\n\nconst getLocalityResult = (results: google.maps.places.PlaceResult[]) => {\n if (results.length === 1) {\n return results[0];\n }\n\n return results.find((r) => findIndex(r.types, 'locality'));\n};\n\nconst getPostalCodeResult = (results: google.maps.places.PlaceResult[]) =>\n results.find((r) => findIndex(r.types, 'postal_code'));\n\nexport const lookup = async (\n lat: number,\n lng: number,\n callback: (response: GoogleMapsGeocode) => void,\n) => {\n try {\n const response = await http.httpJson(\n `https://maps.googleapis.com/maps/api/geocode/json?address=${lat},${lng}&key=${get(\n 'GoogleApiKey',\n )}&language=sv`,\n buildGetRequest({ contentType: null }),\n );\n callback(response);\n } catch (_e) {\n //\n }\n};\n\nconst browser =\n (lat: number, lng: number, radius: number | null = null) =>\n (dispatch: Dispatch) =>\n lookup(lat, lng, (json) =>\n dispatch({ type: RCV_LOCATION, response: getLocalityResult(json.results), radius }),\n );\n\nconst resolveComponents = () => {\n const marketCode = getMarket().marketCode;\n switch (marketCode) {\n case 'NO':\n return 'region=NO&language=no&components=country:NO';\n default:\n return 'region=SE&language=sv&components=country:SE';\n }\n};\n\nexport const byPostalCode = (postalCode: string | null) => async (dispatch: Dispatch) => {\n if (requestCache.isPending(REQ_LOCATION_FROM_POSTAL_CODE)) {\n return;\n }\n requestCache.isPending(REQ_LOCATION_FROM_POSTAL_CODE, true);\n\n try {\n dispatch({ type: REQ_LOCATION_FROM_POSTAL_CODE });\n\n const response = await http.httpJson(\n `https://maps.googleapis.com/maps/api/geocode/json?address=${postalCode}&key=${get(\n 'GoogleApiKey',\n )}&${resolveComponents()}`,\n buildGetRequest({ contentType: null }),\n );\n dispatch({\n type: RCV_LOCATION_FROM_POSTAL_CODE,\n response: getPostalCodeResult(response.results),\n });\n } catch (_e) {\n dispatch({ type: REQUEST_FAILED, action: REQ_LOCATION_FROM_POSTAL_CODE });\n }\n requestCache.isPending(REQ_LOCATION_FROM_POSTAL_CODE, false);\n};\n\nexport const search = (query: string) => async (dispatch: Dispatch) => {\n if (requestCache.isPending(REQ_LOCATION_FROM_SEARCH)) {\n return;\n }\n requestCache.isPending(REQ_LOCATION_FROM_SEARCH, true);\n\n try {\n dispatch({ type: REQ_LOCATION_FROM_SEARCH });\n\n const response = await http.httpJson(\n `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(query)}&key=${get(\n 'GoogleApiKey',\n )}&${resolveComponents()}`,\n buildGetRequest({ contentType: null }),\n );\n\n dispatch({\n type: RCV_LOCATION_FROM_SEARCH,\n response: response.results,\n });\n } catch (_e) {\n dispatch({ type: REQUEST_FAILED, action: REQ_LOCATION_FROM_SEARCH });\n }\n requestCache.isPending(REQ_LOCATION_FROM_SEARCH, false);\n};\n\nexport const clear = () => (dispatch: Dispatch) => dispatch({ type: REQ_CLEAR_LOCATION_SEARCH });\n\nconst noop = () => {};\nexport const current =\n (onError?: (err?: GeolocationPositionError) => void) => (dispatch: Dispatch) => {\n onError = onError || noop;\n if (!process.env.SSR && navigator && navigator.geolocation) {\n navigator.geolocation.getCurrentPosition(\n (position) => browser(position.coords.latitude, position.coords.longitude)(dispatch),\n onError,\n );\n } else {\n onError();\n }\n };\n"], "mappings": "mMAWO,IAAMA,EAAe,uBAMfC,EAAgC,gCAChCC,EAAgC,gCAMhCC,EAA2B,2BAC3BC,EAA2B,2BAM3BC,EAA4B,4BAG5BC,EAAiB,0BAgBxBC,EAAY,CAACC,EAAyBC,IAAiB,CAC3D,IAAMC,EAAQF,GAAG,QAAQC,CAAI,EAC7B,OAAOC,IAAU,OAAYA,EAAQ,GAAK,EAC5C,EAEMC,EAAqBC,GACrBA,EAAQ,SAAW,EACdA,EAAQ,CAAC,EAGXA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U5B,EAA0B,EAAI,EAErD,GAAI,CACFsB,EAAS,CAAE,KAAMtB,CAAyB,CAAC,EAE3C,IAAMgB,EAAW,MAAWC,EAC1B,6DAA6D,mBAAmBa,CAAK,CAAC,QAAQZ,EAC5F,cACF,CAAC,IAAIM,EAAkB,CAAC,GACxBL,EAAgB,CAAE,YAAa,IAAK,CAAC,CACvC,EAEAG,EAAS,CACP,KAAMrB,EACN,SAAUe,EAAS,OACrB,CAAC,CACH,MAAa,CACXM,EAAS,CAAE,KAAMnB,EAAgB,OAAQH,CAAyB,CAAC,CACrE,CACA4B,EAAa,UAAU5B,EAA0B,EAAK,EACxD,EAEa+B,EAAQ,IAAOT,GAAuBA,EAAS,CAAE,KAAMpB,CAA0B,CAAC,EAEzF8B,EAAO,IAAM,CAAC,EACPC,EACVC,GAAwDZ,GAAuB,CAC9EY,EAAUA,GAAWF,EACG,WAAa,UAAU,YAC7C,UAAU,YAAY,mBACnBG,GAAaf,EAAQe,EAAS,OAAO,SAAUA,EAAS,OAAO,SAAS,EAAEb,CAAQ,EACnFY,CACF,EAEAA,EAAQ,CAEZ", "names": ["RCV_LOCATION", "REQ_LOCATION_FROM_POSTAL_CODE", "RCV_LOCATION_FROM_POSTAL_CODE", "REQ_LOCATION_FROM_SEARCH", "RCV_LOCATION_FROM_SEARCH", "REQ_CLEAR_LOCATION_SEARCH", "REQUEST_FAILED", "findIndex", "s", "type", "index", "getLocalityResult", "results", "r", "getPostalCodeResult", "lookup", "lat", "lng", "callback", "response", "httpJson", "get", "buildGetRequest", "browser", "radius", "dispatch", "json", "resolveComponents", "getMarket", "byPostalCode", "postalCode", "request_cache_default", "search", "query", "clear", "noop", "current", "onError", "position"] }