Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ProgressBarExecutingOrdersUpdater,
} from 'modules/orderProgressBar'
import { OrdersNotificationsUpdater } from 'modules/orders'
import { GeoDataUpdater } from 'modules/rwa'
import { BlockedListSourcesUpdater, useSourceChainId } from 'modules/tokensList'
import { TradeType, useTradeTypeInfo } from 'modules/trade'
import { UsdPricesUpdater } from 'modules/usdAmount'
Expand Down Expand Up @@ -116,6 +117,7 @@ export function Updaters(): ReactNode {
/>
<RestrictedTokensListUpdater isRwaGeoblockEnabled={!!isRwaGeoblockEnabled} />
<BlockedListSourcesUpdater />
<GeoDataUpdater />
<TokensListsTagsUpdater />

<WidgetTokensUpdater />
Expand Down
1 change: 1 addition & 0 deletions apps/cowswap-frontend/src/modules/rwa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './hooks/useGeoStatus'
export * from './hooks/useRwaTokenStatus'
export * from './pure/RwaConsentModal'
export * from './containers/RwaConsentModalContainer'
export * from './updaters/GeoDataUpdater'
36 changes: 25 additions & 11 deletions apps/cowswap-frontend/src/modules/rwa/state/geoDataAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,44 @@ const initialGeoData: GeoData = {

export const geoDataAtom = atom<GeoData>(initialGeoData)

export const fetchGeoDataAtom = atom(null, async (get, set) => {
const current = get(geoDataAtom)

// Don't fetch if already loaded or loading
if (current.country !== null || current.isLoading) {
return
}

set(geoDataAtom, { ...current, isLoading: true })
async function doFetchGeoData(set: (update: GeoData) => void, current: GeoData): Promise<void> {
set({ ...current, isLoading: true })

try {
const response = await fetch('https://api.country.is')
const data = await response.json()

set(geoDataAtom, {
set({
country: data.country || null,
isLoading: false,
error: null,
})
} catch (error) {
set(geoDataAtom, {
set({
country: null,
isLoading: false,
error: error instanceof Error ? error.message : 'Failed to fetch geo data',
})
}
}

export const fetchGeoDataAtom = atom(null, async (get, set) => {
const current = get(geoDataAtom)

if (current.country !== null || current.isLoading) {
return
}

await doFetchGeoData((update) => set(geoDataAtom, update), current)
})

// for cases when user changes wallet
export const refetchGeoDataAtom = atom(null, async (get, set) => {
const current = get(geoDataAtom)

if (current.isLoading) {
return
}

await doFetchGeoData((update) => set(geoDataAtom, update), current)
})
22 changes: 22 additions & 0 deletions apps/cowswap-frontend/src/modules/rwa/updaters/GeoDataUpdater.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useSetAtom } from 'jotai'
import { useEffect } from 'react'

import { usePrevious } from '@cowprotocol/common-hooks'
import { useWalletInfo } from '@cowprotocol/wallet'

import { refetchGeoDataAtom } from '../state/geoDataAtom'

export function GeoDataUpdater(): null {
const { account } = useWalletInfo()
const refetchGeoData = useSetAtom(refetchGeoDataAtom)
const prevAccount = usePrevious(account)

useEffect(() => {
// only refetch when wallet actually changes (not on initial render)
if (prevAccount !== account) {
refetchGeoData()
}
}, [account, prevAccount, refetchGeoData])

return null
}