diff --git a/components/CoflCoins/CoflCoinsPurchase.tsx b/components/CoflCoins/CoflCoinsPurchase.tsx index dab7f63f4..18d332b63 100644 --- a/components/CoflCoins/CoflCoinsPurchase.tsx +++ b/components/CoflCoins/CoflCoinsPurchase.tsx @@ -7,9 +7,8 @@ import { postApiTopupPlaystore } from '../../api/_generated/skyApi' import { useCoflCoins } from '../../utils/Hooks' import CoflCoinPurchaseWizard from './CoflCoinPurchaseWizard' import PurchaseElement from './PurchaseElement' -import { Country, getCountry, getCountryFromUserLanguage } from '../../utils/CountryUtils' import CountrySelect from '../CountrySelect/CountrySelect' -import { USER_COUNTRY_CODE } from '../../utils/SettingsUtils' +import { useCountryDetection } from '../../hooks/useCountryDetection' import styles from './CoflCoinsPurchase.module.css' interface Props { @@ -21,8 +20,7 @@ function Payment(props: Props) { let [loadingId, setLoadingId] = useState('') let [currentRedirectLink, setCurrentRedirectLink] = useState('') let [showAll, setShowAll] = useState(false) - let [defaultCountry, setDefaultCountry] = useState() - let [selectedCountry, setSelectedCountry] = useState() + const { selectedCountry, handleCountryChange } = useCountryDetection() let [useWizard, setUseWizard] = useState(true) let [isGooglePlayAvailable, setIsGooglePlayAvailable] = useState(false) let coflCoins = useCoflCoins() @@ -63,9 +61,6 @@ function Payment(props: Props) { } } - // Load country - loadDefaultCountry() - // Check for Android Billing availability with a delay setTimeout(() => { checkGooglePlayAvailability() @@ -161,39 +156,6 @@ function Payment(props: Props) { setIsGooglePlayAvailable(available) } - async function loadDefaultCountry() { - let cachedCountryCode = localStorage.getItem(USER_COUNTRY_CODE) - if (cachedCountryCode) { - setDefaultCountry(getCountry(cachedCountryCode)) - setSelectedCountry(getCountry(cachedCountryCode)) - return - } - - let response: Response | null = null - try { - response = await fetch('https://api.country.is') - } catch (error) { - console.warn('Failed to fetch country from api.country.is:', error) - // Fallback to country from browser language - let country = getCountryFromUserLanguage() - setDefaultCountry(country) - setSelectedCountry(country) - return - } - - if (response && response.ok) { - let result = await response.json() - let country = getCountry(result.country) || getCountryFromUserLanguage() - setDefaultCountry(country) - setSelectedCountry(country) - localStorage.setItem(USER_COUNTRY_CODE, result.country) - } else { - let country = getCountryFromUserLanguage() - setDefaultCountry(country) - setSelectedCountry(country) - } - } - function onPayPaypal(productId: string, coflCoins?: number) { setLoadingId(coflCoins ? `${productId}_${coflCoins}` : productId) setCurrentRedirectLink('') @@ -294,11 +256,7 @@ function Payment(props: Props) { if (!props.cancellationRightLossConfirmed || !selectedCountry) { return (
- {defaultCountry ? ( - - ) : ( - - )} + {!props.cancellationRightLossConfirmed && ( @@ -320,16 +278,7 @@ function Payment(props: Props) {
- {defaultCountry ? ( - - ) : ( - - )} +