diff --git a/apps/amakrushi/src/context/ContextProvider.tsx b/apps/amakrushi/src/context/ContextProvider.tsx index effc6f5..9c60c33 100644 --- a/apps/amakrushi/src/context/ContextProvider.tsx +++ b/apps/amakrushi/src/context/ContextProvider.tsx @@ -408,6 +408,9 @@ const ContextProvider: FC<{ longitude: sessionStorage.getItem('longitude'), city: sessionStorage.getItem('city'), state: sessionStorage.getItem('state'), + subDistrict: sessionStorage.getItem('subDistrict'), + village: sessionStorage.getItem('village'), + captureMode: sessionStorage.getItem('captureMode'), ip: sessionStorage.getItem('ip'), asrId: sessionStorage.getItem('asrId'), userId: localStorage.getItem('userID'), diff --git a/apps/amakrushi/src/utils/location.ts b/apps/amakrushi/src/utils/location.ts index a68679b..16e0525 100644 --- a/apps/amakrushi/src/utils/location.ts +++ b/apps/amakrushi/src/utils/location.ts @@ -1,37 +1,64 @@ +import { logEvent } from 'firebase/analytics'; import { toast } from 'react-hot-toast'; +import { analytics } from './firebase'; export async function recordUserLocation() { + let lat = 0, long = 0; + + async function saveUserLocation(position: any) { + // Capturing user location through GPS + sessionStorage.setItem('latitude', position.coords.latitude); + sessionStorage.setItem('longitude', position.coords.longitude); + lat = position.coords.latitude; + long = position.coords.longitude; + } + try { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(saveUserLocation); } + // Fetching user's ip let apiRes: any = await fetch('https://api.ipify.org?format=json'); apiRes = await apiRes.json(); - if (apiRes?.ip) { - navigator.permissions.query({ name: 'geolocation' }).then(async (res: any) => { - - let locationRes: any = await fetch(`https://geoip.samagra.io/city/${apiRes.ip}`); - locationRes = await locationRes.json(); - sessionStorage.setItem('city', locationRes.city); - sessionStorage.setItem('state', locationRes.regionName); - sessionStorage.setItem('ip', apiRes?.ip); - - if (res.state != 'granted') { + navigator.permissions.query({ name: 'geolocation' }).then(async (res: any) => { + // If user doesn't grant gps permission + if (res.state != 'granted') { + if (apiRes?.ip) { + let locationRes: any = await fetch(`https://geoip.samagra.io/city/${apiRes.ip}`); + locationRes = await locationRes.json(); + let latLongRes: any = await fetch(`https://geoip.samagra.io/georev?lat=${locationRes?.lat}&lon=${locationRes?.lon}`); + latLongRes = await latLongRes.json(); + //@ts-ignore + logEvent(analytics, 'location_captured_through_ip'); + sessionStorage.setItem('city', latLongRes?.district); + sessionStorage.setItem('state', latLongRes?.state); + sessionStorage.setItem('subDistrict', latLongRes?.subDistrict) + sessionStorage.setItem('village', latLongRes?.village || '') + sessionStorage.setItem('ip', apiRes?.ip); sessionStorage.setItem('latitude', locationRes.lat); sessionStorage.setItem('longitude', locationRes.lon); + sessionStorage.setItem('captureMode', 'ip'); } - }) - } + } else { + // If user has provided geolocation access then + let locationRes: any = await fetch(`https://geoip.samagra.io/georev?lat=${lat || sessionStorage.getItem('latitude')}&lon=${long || sessionStorage.getItem('longitude')}`); + //@ts-ignore + logEvent(analytics, 'location_captured_through_gps'); + locationRes = await locationRes.json(); + sessionStorage.setItem('city', locationRes?.district); + sessionStorage.setItem('state', locationRes?.state); + sessionStorage.setItem('subDistrict', locationRes?.subDistrict) + sessionStorage.setItem('village', locationRes?.village || '') + sessionStorage.setItem('ip', apiRes?.ip); + sessionStorage.setItem('captureMode', 'gps'); + } + + }) } catch (err) { console.log(err) } } -function saveUserLocation(position: any) { - sessionStorage.setItem('latitude', position.coords.latitude); - sessionStorage.setItem('longitude', position.coords.longitude); -} -