Skip to content

Commit

Permalink
Cache country code from api (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
KuznetsovNikita authored Jan 10, 2024
1 parent 52fdbe3 commit 7a94281
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/uikit/src/state/country.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { IAppSdk } from '@tonkeeper/core/dist/AppSdk';
import { AppKey } from '@tonkeeper/core/dist/Keys';
import { useAppSdk } from '../hooks/appSdk';
import { QueryKey } from '../libs/queryKey';
Expand All @@ -8,10 +9,15 @@ export interface CountryIs {
ip: string;
}

const getMyCountryCode = async () => {
const getMyCountryCode = async (sdk: IAppSdk) => {
const url = 'https://api.country.is';
try {
const response = await fetch('https://api.country.is');
const country = await sdk.storage.get<string>(url);
if (country) return country;

const response = await fetch(url);
const json: CountryIs = await response.json();
await sdk.storage.set<string>(url, json.country);
return json.country;
} catch (e) {
return null;
Expand All @@ -26,8 +32,9 @@ export const useCountrySetting = () => {
};

export const useAutoCountry = () => {
const sdk = useAppSdk();
return useQuery<string | null, Error>([QueryKey.country, 'detect'], async () => {
return await getMyCountryCode();
return await getMyCountryCode(sdk);
});
};

Expand All @@ -36,7 +43,7 @@ export const useUserCountry = () => {
return useQuery<string | null, Error>([QueryKey.country], async () => {
let code = await sdk.storage.get<string>(AppKey.COUNTRY);
if (!code) {
code = await getMyCountryCode();
code = await getMyCountryCode(sdk);
}
return code;
});
Expand Down

0 comments on commit 7a94281

Please sign in to comment.