From 1870f2c9636e73f7fddc10d8ce255254cfbb595a Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Fri, 20 May 2022 18:55:56 +0700 Subject: [PATCH] fix(#2445): add redirect to matchpage for brasil payment after pay --- .../components/BrazilPayment/hooks.tsx | 78 +++++++++++++++++++ .../components/BrazilPayment/index.tsx | 54 +++---------- .../components/PackageSelectionStep/index.tsx | 1 + src/requests/getMatchInfo.tsx | 1 + 4 files changed, 91 insertions(+), 43 deletions(-) create mode 100644 src/features/BuyMatchPopup/components/BrazilPayment/hooks.tsx diff --git a/src/features/BuyMatchPopup/components/BrazilPayment/hooks.tsx b/src/features/BuyMatchPopup/components/BrazilPayment/hooks.tsx new file mode 100644 index 00000000..97e7a2e4 --- /dev/null +++ b/src/features/BuyMatchPopup/components/BrazilPayment/hooks.tsx @@ -0,0 +1,78 @@ +import { + useEffect, + useState, + MouseEvent, +} from 'react' + +import { useHistory } from 'react-router-dom' + +import { ProfileTypes } from 'config' + +import isNumber from 'lodash/isNumber' + +import { useLexicsStore } from 'features/LexicsStore' +import { getProfileUrl } from 'features/ProfileLink/helpers' + +import { getMatchInfo } from 'requests/getMatchInfo' +import { getBrazilPaymentUrl } from 'requests/getBrazilPaymentUrl' +import type { Props } from './index' + +type ResponsePayment = { + url: string, +} + +type ResponsePaymentArray = ResponsePayment | null + +export const useBrazilPayment = ({ + match, + open, + selectedPackage, + setIsOpenBrasilian, +}: Props) => { + const history = useHistory() + + const [src, setSrc] = useState('') + const { translate } = useLexicsStore() + + const { + name, + nameLexic, + originalObject, + pass, + } = selectedPackage + + const teams = isNumber(nameLexic) ? translate(String(nameLexic)) : name + const pack = translate(String(pass)) + + const closePopup = async (e?: MouseEvent) => { + e?.stopPropagation() + setIsOpenBrasilian(false) + + const { id, sportType } = match + const accessMatch = await getMatchInfo(sportType, id) + + if (accessMatch?.access) { + const matchLink = getProfileUrl({ + id, + profileType: ProfileTypes.MATCHES, + sportType, + }) + history.push(matchLink) + } + } + + useEffect(() => { + if (open) { + (async () => { + const json: ResponsePaymentArray = await getBrazilPaymentUrl({ item: originalObject, product_name: `${pack} ${teams}` }) + setSrc(json?.url || '') + })() + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [selectedPackage, open]) + + return { + closePopup, + src, + } +} diff --git a/src/features/BuyMatchPopup/components/BrazilPayment/index.tsx b/src/features/BuyMatchPopup/components/BrazilPayment/index.tsx index 5606c310..d1871b1a 100644 --- a/src/features/BuyMatchPopup/components/BrazilPayment/index.tsx +++ b/src/features/BuyMatchPopup/components/BrazilPayment/index.tsx @@ -1,61 +1,29 @@ -import { - useEffect, - useState, - MouseEvent, -} from 'react' - import { Loader } from 'features/Loader' -import { useLexicsStore } from 'features/LexicsStore' -import { MatchPackage } from 'features/BuyMatchPopup/types' - -import { getBrazilPaymentUrl } from 'requests/getBrazilPaymentUrl' +import { MatchPackage, Match } from 'features/BuyMatchPopup/types' -import isNumber from 'lodash/isNumber' +import { useBrazilPayment } from './hooks' import { ScModal, LoaderWrapper } from './styled' -type Props = { +export type Props = { + match: Match, open: boolean, selectedPackage: MatchPackage, setIsOpenBrasilian: (open: boolean) => void, } -type ResponsePayment = { - url: string, -} - -type ResponsePaymentArray = ResponsePayment | null - export const BrazilPayment = ({ + match, open, selectedPackage, setIsOpenBrasilian, }: Props) => { - const { - name, - nameLexic, - originalObject, - pass, - } = selectedPackage - const [src, setSrc] = useState('') - const { translate } = useLexicsStore() - const teams = isNumber(nameLexic) ? translate(String(nameLexic)) : name - const pack = translate(String(pass)) - - const closePopup = (e?: MouseEvent) => { - e?.stopPropagation() - setIsOpenBrasilian(false) - } - - useEffect(() => { - if (open) { - (async () => { - const json: ResponsePaymentArray = await getBrazilPaymentUrl({ item: originalObject, product_name: `${pack} ${teams}` }) - setSrc(json?.url || '') - })() - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [selectedPackage, open]) + const { closePopup, src } = useBrazilPayment({ + match, + open, + selectedPackage, + setIsOpenBrasilian, + }) return ( diff --git a/src/features/BuyMatchPopup/components/PackageSelectionStep/index.tsx b/src/features/BuyMatchPopup/components/PackageSelectionStep/index.tsx index 8fd65222..262c65f1 100644 --- a/src/features/BuyMatchPopup/components/PackageSelectionStep/index.tsx +++ b/src/features/BuyMatchPopup/components/PackageSelectionStep/index.tsx @@ -109,6 +109,7 @@ export const PackageSelectionStep = () => { {selectedPackage && isOpenBrasilian && (