-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature(maps-screen): center map on user's location when user first o…
…pens the maps tab (#2895) * feat(map-screen): opening the map will center on user's location if given permission * refactor: remove comments and logs * refactor: prettier * refactor: relocating config setup for geolocation * feat: switched to allowing Geolocation library to request permissions in order to better request for ios * refactor: check code prettier * feat: focuses on users ip country as a backup option if permissions are denied * refactor: code minimization * feat: utilizing graphQL client cache instead of asyncStorage. Required a refactor that moves the retrieval to a hook instead of a standard function * refactor: minor reorganizations * refactor: check code --------- Co-authored-by: Craig Mahood <[email protected]>
- Loading branch information
Showing
14 changed files
with
508 additions
and
91 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
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useApolloClient } from "@apollo/client" | ||
import { useEffect, useState } from "react" | ||
import { updateCountryCode } from "@app/graphql/client-only-query" | ||
import { useCountryCodeQuery } from "@app/graphql/generated" | ||
import axios from "axios" | ||
import { CountryCode } from "libphonenumber-js/mobile" | ||
|
||
const useDeviceLocation = () => { | ||
const client = useApolloClient() | ||
const { data, error } = useCountryCodeQuery() | ||
|
||
const [loading, setLoading] = useState(true) | ||
const [countryCode, setCountryCode] = useState<CountryCode>("SV") | ||
|
||
useEffect(() => { | ||
if (error) { | ||
setLoading(false) | ||
} | ||
}, [error]) | ||
|
||
useEffect(() => { | ||
if (data) { | ||
const getLocation = async () => { | ||
try { | ||
const response = await axios.get("https://ipapi.co/json/", { | ||
timeout: 5000, | ||
}) | ||
const _countryCode = response?.data?.country_code | ||
if (_countryCode) { | ||
setCountryCode(_countryCode) | ||
updateCountryCode(client, _countryCode) | ||
} else { | ||
console.warn("no data. default of SV will be used") | ||
} | ||
// can throw a 429 for device's rate-limiting. resort to cached value if available | ||
} catch (err) { | ||
setCountryCode(data.countryCode as CountryCode) | ||
} | ||
setLoading(false) | ||
} | ||
getLocation() | ||
} | ||
}, [data, client, setLoading, setCountryCode]) | ||
|
||
return { | ||
countryCode, | ||
loading, | ||
} | ||
} | ||
|
||
export default useDeviceLocation |
Oops, something went wrong.