Skip to content

Commit

Permalink
fix: get the ip as json ans improve alert error message
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiParraCrespo committed Nov 11, 2024
1 parent fd0cdca commit 923339f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/module/common/query/useGetIPCountry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useQuery } from "react-query";
import { QueryResult } from "query-utils";
import Queries from "../../../query/queries";
import { useNetInfo } from "@react-native-community/netinfo";
import { Alert } from "react-native";

export type CoinPrice = Record<string, string | undefined>;

Expand All @@ -14,10 +15,11 @@ export const useGetIPCountry = (): QueryResult<string | undefined> & { isEnabled
[Queries.GET_COUNTRY_BY_IP, netInfo],
async () => {
try {
const ip = await (await fetch(`https://api.ipify.org`)).text(); // Unlimited usage
const { ip } = await (await fetch(`https://api.ipify.org?format=json`)).json(); // Unlimited usage
const geoIpInfo = await (await fetch(`http://ip-api.com/json/${ip}`)).json(); // 45 requests per minute
return geoIpInfo?.countryCode;
} catch (e) {
} catch (e: any) {
Alert.alert("Error", e.message);
// eslint-disable-next-line no-console
console.warn("Error loading IP geo info", JSON.stringify(e));
return undefined;
Expand Down

0 comments on commit 923339f

Please sign in to comment.