From 9a6d3cae1b751aa125e8ce9c900eb959880d65a9 Mon Sep 17 00:00:00 2001 From: Rakov Roman Date: Fri, 27 Jan 2023 10:47:51 +0300 Subject: [PATCH] fix(#188): check geo, redirect to diwan --- src/features/GlobalStores/index.tsx | 31 +++++++++++++++++++++++++++-- src/requests/getGeoInfo.tsx | 20 +++++++++++++++++++ src/requests/index.tsx | 1 + 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/requests/getGeoInfo.tsx diff --git a/src/features/GlobalStores/index.tsx b/src/features/GlobalStores/index.tsx index 80cb9c19..8a1d450f 100644 --- a/src/features/GlobalStores/index.tsx +++ b/src/features/GlobalStores/index.tsx @@ -1,4 +1,10 @@ -import { ReactNode, useEffect } from 'react' +import { + ReactNode, + useEffect, + useState, +} from 'react' + +import { useLocation } from 'react-router' import { useMatomo } from '@jonkoops/matomo-tracker-react' @@ -6,7 +12,10 @@ import { getLanguageUrlParam } from 'helpers/languageUrlParam' import { AuthStore } from 'features/AuthStore' import { LexicsStore } from 'features/LexicsStore' -import { useLocation } from 'react-router' + +import { getGeoInfo } from 'requests' + +import { redirectToUrl } from 'helpers' const initialLanguage = getLanguageUrlParam() @@ -17,6 +26,7 @@ type Props = { export const GlobalStores = ({ children }: Props) => { const { pathname, search } = useLocation() const { trackPageView } = useMatomo() + const [isGeoReady, setIsGeoReady] = useState(false) useEffect(() => { const isProduction = process.env.REACT_APP_ENV === 'production' @@ -24,6 +34,23 @@ export const GlobalStores = ({ children }: Props) => { if (isProduction) trackPageView() }, [trackPageView, pathname, search]) + useEffect(() => { + (async () => { + if (pathname === '/' && search === '') { + const geo = await getGeoInfo() + + if (geo.country_code === 'TN') { + redirectToUrl('https://diwan.insports.tv') + return + } + } + setIsGeoReady(true) + })() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) + + if (!isGeoReady) return null + return ( diff --git a/src/requests/getGeoInfo.tsx b/src/requests/getGeoInfo.tsx new file mode 100644 index 00000000..70ffe866 --- /dev/null +++ b/src/requests/getGeoInfo.tsx @@ -0,0 +1,20 @@ +import { AUTH_SERVICE } from 'config' + +import { callApi } from 'helpers' + +export type GeoInfoType = { + city: string, + country_code: string, + time_zone: string, +} + +export const getGeoInfo = async (): Promise => { + const config = { + method: 'GET', + } + + return callApi({ + config, + url: `${AUTH_SERVICE}/geoinfo`, + }) +} diff --git a/src/requests/index.tsx b/src/requests/index.tsx index e1fd2c8e..fd1e81e3 100644 --- a/src/requests/index.tsx +++ b/src/requests/index.tsx @@ -25,4 +25,5 @@ export * from './getPlayerPlaylists' export * from './getSubscriptions' export * from './buySubscription' export * from './saveMatchStats' +export * from './getGeoInfo' export * from './getTokenVirtualUser'