-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/ip-fix' into fix/weather-advisory
- Loading branch information
Showing
2 changed files
with
47 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|