Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: uncaught error in GeoblockingProvider #3938

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
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
24 changes: 8 additions & 16 deletions src/components/common/GeoblockingProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
import { AppRoutes } from '@/config/routes'
import { createContext, type ReactElement, type ReactNode, useEffect, useState } from 'react'
import useAsync from '@/hooks/useAsync'
import { createContext, type ReactElement, type ReactNode } from 'react'

export const GeoblockingContext = createContext<boolean | null>(null)

const checkBlocked = async () => {
const res = await fetch(AppRoutes.swap, { method: 'HEAD' })
return res.status === 403
}

/**
* Endpoint returns a 403 if the requesting user is from one of the OFAC sanctioned countries
*/
const GeoblockingProvider = ({ children }: { children: ReactNode }): ReactElement => {
const [isBlockedCountry, setIsBlockedCountry] = useState<boolean | null>(null)

useEffect(() => {
const fetchSwaps = async () => {
await fetch(AppRoutes.swap, { method: 'HEAD' }).then((res) => {
if (res.status === 403) {
setIsBlockedCountry(true)
} else {
setIsBlockedCountry(false)
}
})
}

fetchSwaps()
}, [])
const [isBlockedCountry = null] = useAsync(checkBlocked, [])

return <GeoblockingContext.Provider value={isBlockedCountry}>{children}</GeoblockingContext.Provider>
}
Expand Down
Loading