From 9752a5eeb6e0fcf8a47660c4f330f7fe96a994aa Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Tue, 8 Nov 2022 16:43:22 +0300 Subject: [PATCH] feat(#159): add fav tournament after visit tournament landing --- src/features/AuthStore/hooks/useAuth.tsx | 5 ++++- src/features/MatchPage/index.tsx | 18 +++++++++++++++++- src/features/TournamentPage/hooks.tsx | 12 ++++++++++++ src/helpers/parseUrlParams/parseUrlParams.tsx | 9 +++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/helpers/parseUrlParams/parseUrlParams.tsx diff --git a/src/features/AuthStore/hooks/useAuth.tsx b/src/features/AuthStore/hooks/useAuth.tsx index 1ad5dfb5..5fd9bd43 100644 --- a/src/features/AuthStore/hooks/useAuth.tsx +++ b/src/features/AuthStore/hooks/useAuth.tsx @@ -103,7 +103,10 @@ export const useAuth = () => { markUserLoaded() if (page) { const route = `${page}${page === '/' ? search : ''}` - history.push(route) + history.push(`${route}${( + page.includes('tournaments') || page.includes('matches')) + ? '?from=landing' + : ''}`) setPage('') setSearch('') } diff --git a/src/features/MatchPage/index.tsx b/src/features/MatchPage/index.tsx index 0fd2afe1..ed4ec6ad 100644 --- a/src/features/MatchPage/index.tsx +++ b/src/features/MatchPage/index.tsx @@ -18,6 +18,7 @@ import { isIOS } from 'config/userAgent' import { usePageLogger } from 'hooks/usePageLogger' import { usePageParams } from 'hooks/usePageParams' +import { checkUrlParams } from 'helpers/parseUrlParams/parseUrlParams' import { MatchPageStore, useMatchPageStore } from './store' import { SubscriptionGuard } from './components/SubscriptionGuard' import { LiveMatch } from './components/LiveMatch' @@ -31,6 +32,7 @@ const MatchPageComponent = () => { const { addRemoveFavorite, userFavorites } = useUserFavoritesStore() const { isStarted, profile } = useMatchPageStore() + const isFavorite = profile && userFavorites.find((fav) => fav.id === profile?.tournament.id) const { profileType, @@ -40,7 +42,6 @@ const MatchPageComponent = () => { useEffect(() => { let timer = 0 timer = window.setTimeout(() => { - const isFavorite = profile && userFavorites.find((fav) => fav.id === profile?.tournament.id) if (profile && !isFavorite) { addRemoveFavorite({ action: FavoritesActions.ADD, @@ -57,6 +58,21 @@ const MatchPageComponent = () => { } // eslint-disable-next-line react-hooks/exhaustive-deps }, [profile, profileType]) + useEffect(() => { + if ( + profile + && !isFavorite + && checkUrlParams('from') === 'landing') { + addRemoveFavorite({ + action: FavoritesActions.ADD, + id: profile?.tournament.id, + isAuto: true, + sport: sportType, + type: ProfileTypes.TOURNAMENTS, + }) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [profile]) const playFromScout = profile?.has_video && !profile?.live && !profile.has_hls const playFromOTT = (!profile?.has_video && (profile?.live || profile?.storage)) diff --git a/src/features/TournamentPage/hooks.tsx b/src/features/TournamentPage/hooks.tsx index dc4067c0..0d6fa16e 100644 --- a/src/features/TournamentPage/hooks.tsx +++ b/src/features/TournamentPage/hooks.tsx @@ -8,10 +8,13 @@ import { useHistory } from 'react-router' import type { TournamentInfo } from 'requests' import { getTournamentInfo, getTournamentMatches } from 'requests' +import { checkUrlParams } from 'helpers/parseUrlParams/parseUrlParams' + import { usePageParams } from 'hooks/usePageParams' import { useName } from 'features/Name' import { isPermittedTournament } from '../../helpers/isPermittedTournament' +import { useProfileCard } from '../ProfileCard/hooks' export const useTournamentPage = () => { const [tournamentProfile, setTournamentProfile] = useState(null) @@ -19,6 +22,8 @@ export const useTournamentPage = () => { const country = useName(tournamentProfile?.country || {}) const history = useHistory() + const { isFavorite, toggleFavorites } = useProfileCard() + useEffect(() => { if (!isPermittedTournament(tournamentId, sportType)) { history.push('/') @@ -32,6 +37,13 @@ export const useTournamentPage = () => { tournamentId, ]) + useEffect(() => { + !isFavorite + && checkUrlParams('from') === 'landing' + && toggleFavorites() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) + const fetchMatches = useCallback( (limit: number, offset: number) => getTournamentMatches({ limit, diff --git a/src/helpers/parseUrlParams/parseUrlParams.tsx b/src/helpers/parseUrlParams/parseUrlParams.tsx new file mode 100644 index 00000000..f960c546 --- /dev/null +++ b/src/helpers/parseUrlParams/parseUrlParams.tsx @@ -0,0 +1,9 @@ +export const parseUrlParams = (key: string) => { + const params = new URLSearchParams(window.location.search) + return params.get('from') +} + +export const checkUrlParams = (key: string) => { + const params = new URLSearchParams(window.location.search) + return params.get(key) +}