fix(887): reuse same playlist data in match page (#320)
parent
a2b2299492
commit
3b10015c9a
@ -1,50 +1,69 @@ |
|||||||
import { |
import { |
||||||
useCallback, |
useCallback, |
||||||
useState, |
|
||||||
useEffect, |
useEffect, |
||||||
|
useState, |
||||||
} from 'react' |
} from 'react' |
||||||
|
|
||||||
import type { Settings } from 'features/MatchPopup' |
import isEmpty from 'lodash/isEmpty' |
||||||
|
|
||||||
import { getPlayerPlaylists, Episodes } from 'requests' |
import type { Episodes } from 'requests' |
||||||
|
import { getPlayerPlaylists } from 'requests' |
||||||
|
|
||||||
import { usePageId, useSportNameParam } from 'hooks' |
import { usePageId, useSportNameParam } from 'hooks' |
||||||
|
|
||||||
import { PlaylistOption, PlaylistTypes } from 'features/MatchPage/types' |
import { PlaylistOption, PlaylistTypes } from 'features/MatchPage/types' |
||||||
|
import { |
||||||
|
defaultSettings, |
||||||
|
Settings, |
||||||
|
useMatchPopupStore, |
||||||
|
} from 'features/MatchPopup' |
||||||
|
|
||||||
type Args = { |
export const useEpisodes = () => { |
||||||
initialEpisodes?: Episodes, |
const { |
||||||
initialSelectedPlaylist?: PlaylistOption, |
handlePlaylistClick, |
||||||
} |
matchPlaylists: playlists, |
||||||
|
selectedPlaylist, |
||||||
export const useEpisodes = ({ initialEpisodes, initialSelectedPlaylist }: Args) => { |
settings, |
||||||
const [episodes, setEpisodes] = useState(initialEpisodes || []) |
} = useMatchPopupStore() |
||||||
|
const [episodes, setEpisodes] = useState<Episodes>([]) |
||||||
const { sportType } = useSportNameParam() |
const { sportType } = useSportNameParam() |
||||||
const matchId = usePageId() |
const matchId = usePageId() |
||||||
|
|
||||||
const fetchEpisodes = useCallback(( |
const fetchEpisodes = useCallback(( |
||||||
selectedPlaylist: PlaylistOption, |
playlistOption: PlaylistOption, |
||||||
settings?: Settings, |
popupSettings: Settings = defaultSettings, |
||||||
) => { |
) => { |
||||||
if (!selectedPlaylist) return |
if (playlistOption.type === PlaylistTypes.PLAYER) { |
||||||
|
|
||||||
if (selectedPlaylist.type === PlaylistTypes.PLAYER) { |
|
||||||
getPlayerPlaylists({ |
getPlayerPlaylists({ |
||||||
matchId, |
matchId, |
||||||
playerId: selectedPlaylist.id, |
playerId: playlistOption.id, |
||||||
settings, |
settings: popupSettings, |
||||||
sportType, |
sportType, |
||||||
}).then(setEpisodes) |
}).then(setEpisodes) |
||||||
} else if (selectedPlaylist.type === PlaylistTypes.MATCH) { |
} else if (playlistOption.type === PlaylistTypes.MATCH) { |
||||||
setEpisodes(selectedPlaylist.data) |
setEpisodes(playlistOption.data) |
||||||
} |
} |
||||||
}, [matchId, sportType]) |
}, [matchId, sportType]) |
||||||
|
|
||||||
useEffect(() => { |
useEffect(() => { |
||||||
if (initialSelectedPlaylist?.type === PlaylistTypes.MATCH) { |
if (!selectedPlaylist && playlists && !isEmpty(playlists.match)) { |
||||||
fetchEpisodes(initialSelectedPlaylist) |
handlePlaylistClick(playlists.match[0]) |
||||||
|
} |
||||||
|
}, [ |
||||||
|
selectedPlaylist, |
||||||
|
playlists, |
||||||
|
handlePlaylistClick, |
||||||
|
]) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
if (selectedPlaylist) { |
||||||
|
fetchEpisodes(selectedPlaylist, settings) |
||||||
} |
} |
||||||
}, [initialSelectedPlaylist, fetchEpisodes]) |
}, [ |
||||||
|
settings, |
||||||
|
selectedPlaylist, |
||||||
|
fetchEpisodes, |
||||||
|
]) |
||||||
|
|
||||||
return { episodes, fetchEpisodes } |
return { episodes } |
||||||
} |
} |
||||||
|
|||||||
@ -1,94 +0,0 @@ |
|||||||
import { useEffect, useState } from 'react' |
|
||||||
|
|
||||||
import isEmpty from 'lodash/isEmpty' |
|
||||||
|
|
||||||
import { getMatchPlaylists, Episodes } from 'requests' |
|
||||||
|
|
||||||
import { usePageId, useSportNameParam } from 'hooks' |
|
||||||
|
|
||||||
import type { PlaylistOption, Playlists } from 'features/MatchPage/types' |
|
||||||
|
|
||||||
import { usePlaylistLexics } from 'features/MatchPopup/store/hooks/usePlaylistLexics' |
|
||||||
import { useMatchPopupStore } from 'features/MatchPopup' |
|
||||||
import { buildPlaylists } from '../helpers/buildPlaylists' |
|
||||||
import { useEpisodes } from './useEpisodes' |
|
||||||
|
|
||||||
const initialPlaylists: Playlists = { |
|
||||||
interview: [], |
|
||||||
match: [], |
|
||||||
players: { |
|
||||||
team1: [], |
|
||||||
team2: [], |
|
||||||
}, |
|
||||||
} |
|
||||||
|
|
||||||
type Args = { |
|
||||||
initialEpisodes?: Episodes, |
|
||||||
initialSelectedPlaylist?: PlaylistOption, |
|
||||||
isFinishedMatch: boolean, |
|
||||||
} |
|
||||||
|
|
||||||
export const usePlaylists = ({ |
|
||||||
initialEpisodes, |
|
||||||
initialSelectedPlaylist, |
|
||||||
isFinishedMatch, |
|
||||||
}: Args) => { |
|
||||||
const [playlists, setPlaylists] = useState<Playlists>(initialPlaylists) |
|
||||||
const [selectedPlaylist, setSelectedPlaylist] = useState(initialSelectedPlaylist) |
|
||||||
const { sportType } = useSportNameParam() |
|
||||||
const { fetchLexics } = usePlaylistLexics() |
|
||||||
const matchId = usePageId() |
|
||||||
const { settings } = useMatchPopupStore() |
|
||||||
|
|
||||||
const { |
|
||||||
episodes, |
|
||||||
fetchEpisodes, |
|
||||||
} = useEpisodes({ |
|
||||||
initialEpisodes, |
|
||||||
initialSelectedPlaylist, |
|
||||||
}) |
|
||||||
useEffect(() => { |
|
||||||
if (selectedPlaylist) { |
|
||||||
fetchEpisodes(selectedPlaylist, settings) |
|
||||||
} |
|
||||||
}, [ |
|
||||||
fetchEpisodes, |
|
||||||
selectedPlaylist, |
|
||||||
settings, |
|
||||||
]) |
|
||||||
|
|
||||||
useEffect(() => { |
|
||||||
if (isFinishedMatch) { |
|
||||||
getMatchPlaylists({ |
|
||||||
matchId, |
|
||||||
selectedActions: [], |
|
||||||
sportType, |
|
||||||
}).then(fetchLexics) |
|
||||||
.then(buildPlaylists) |
|
||||||
.then(setPlaylists) |
|
||||||
} |
|
||||||
}, [ |
|
||||||
fetchLexics, |
|
||||||
isFinishedMatch, |
|
||||||
matchId, |
|
||||||
sportType, |
|
||||||
]) |
|
||||||
|
|
||||||
useEffect(() => { |
|
||||||
if (!selectedPlaylist && !isEmpty(playlists.match)) { |
|
||||||
setSelectedPlaylist(playlists.match[0]) |
|
||||||
} |
|
||||||
}, [selectedPlaylist, playlists]) |
|
||||||
|
|
||||||
const onPlaylistSelect = (option: PlaylistOption) => { |
|
||||||
if (option === selectedPlaylist) return |
|
||||||
setSelectedPlaylist(option) |
|
||||||
} |
|
||||||
|
|
||||||
return { |
|
||||||
episodes, |
|
||||||
onPlaylistSelect, |
|
||||||
playlists, |
|
||||||
selectedPlaylist, |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,37 +0,0 @@ |
|||||||
import { useEffect } from 'react' |
|
||||||
import { useHistory, useLocation } from 'react-router' |
|
||||||
|
|
||||||
import type { Episodes } from 'requests' |
|
||||||
|
|
||||||
import type { PlaylistOption } from 'features/MatchPage/types' |
|
||||||
|
|
||||||
export type RouteState = { |
|
||||||
/** |
|
||||||
* Данные плейлиста если был выбран игрок |
|
||||||
*/ |
|
||||||
episodes?: Episodes, |
|
||||||
|
|
||||||
/** |
|
||||||
* Выбранный плейлист на попапе матчей |
|
||||||
*/ |
|
||||||
selectedPlaylist: PlaylistOption, |
|
||||||
} |
|
||||||
|
|
||||||
export const useRouteState = () => { |
|
||||||
// считываем стейт из роутера
|
|
||||||
// если есть стейт то на этот матч мы переходили
|
|
||||||
// из попапа матчей и статус матча Завершенный
|
|
||||||
// и запрос на получение ссылки лив матча можно пропустить
|
|
||||||
const { state } = useLocation<RouteState | undefined>() |
|
||||||
const history = useHistory() |
|
||||||
|
|
||||||
useEffect(() => { |
|
||||||
// сбрасываем роут стейт
|
|
||||||
history.replace(history.location.pathname) |
|
||||||
}, [history]) |
|
||||||
|
|
||||||
return { |
|
||||||
initialEpisodes: state?.episodes, |
|
||||||
initialSelectedPlaylist: state?.selectedPlaylist, |
|
||||||
} |
|
||||||
} |
|
||||||
@ -1,65 +1,31 @@ |
|||||||
import type { MouseEvent } from 'react' |
import type { MouseEvent } from 'react' |
||||||
import { useCallback, useState } from 'react' |
import { useState } from 'react' |
||||||
import { useHistory } from 'react-router' |
|
||||||
|
|
||||||
import { ProfileTypes } from 'config' |
import type { PlaylistOption, PlayerPlaylistOption } from 'features/MatchPage/types' |
||||||
import { getPlayerPlaylists } from 'requests' |
|
||||||
|
|
||||||
import type { PlayerPlaylistOption } from 'features/MatchPage/types' |
|
||||||
import type { MatchData, Settings } from 'features/MatchPopup/types' |
|
||||||
import type { RouteState } from 'features/MatchPage/hooks/useRouteState' |
|
||||||
import { PopupPages } from 'features/MatchPopup/types' |
import { PopupPages } from 'features/MatchPopup/types' |
||||||
import { getProfileUrl } from 'features/ProfileLink/helpers' |
|
||||||
|
|
||||||
type Args = { |
type Args = { |
||||||
goTo: (page: PopupPages) => void, |
goTo: (page: PopupPages) => void, |
||||||
match: MatchData, |
|
||||||
settings: Settings, |
|
||||||
} |
} |
||||||
|
|
||||||
export const usePlayerClickHandler = ({ |
export const usePlayerClickHandler = ({ |
||||||
goTo, |
goTo, |
||||||
match, |
|
||||||
settings, |
|
||||||
}: Args) => { |
}: Args) => { |
||||||
const history = useHistory() |
const [selectedPlaylist, setSelectedPlaylist] = useState<PlaylistOption>() |
||||||
const [selectedPlayer, setSelectedPlayer] = useState<PlayerPlaylistOption | null>(null) |
const handlePlaylistClick = (playlist: PlaylistOption, e?: MouseEvent) => { |
||||||
const handlePlayerClick = (player: PlayerPlaylistOption, e: MouseEvent) => { |
e?.stopPropagation() |
||||||
e.stopPropagation() |
if (playlist !== selectedPlaylist) { |
||||||
setSelectedPlayer(player) |
setSelectedPlaylist(playlist) |
||||||
|
} |
||||||
|
} |
||||||
|
const handlePlayerClick = (player: PlayerPlaylistOption, e?: MouseEvent) => { |
||||||
|
handlePlaylistClick(player, e) |
||||||
goTo(PopupPages.PLAYER_SETTINGS) |
goTo(PopupPages.PLAYER_SETTINGS) |
||||||
} |
} |
||||||
|
|
||||||
const handleWatchEpisodesClick = useCallback(async () => { |
|
||||||
if (!match || !selectedPlayer) return |
|
||||||
const episodes = await getPlayerPlaylists({ |
|
||||||
matchId: match.id, |
|
||||||
playerId: selectedPlayer.id, |
|
||||||
settings, |
|
||||||
sportType: match.sportType, |
|
||||||
}) |
|
||||||
|
|
||||||
const matchLink = getProfileUrl({ |
|
||||||
id: match.id, |
|
||||||
profileType: ProfileTypes.MATCHES, |
|
||||||
sportType: match.sportType, |
|
||||||
}) |
|
||||||
|
|
||||||
const routeState: RouteState = { |
|
||||||
episodes, |
|
||||||
selectedPlaylist: selectedPlayer, |
|
||||||
} |
|
||||||
history.push(matchLink, routeState) |
|
||||||
}, [ |
|
||||||
match, |
|
||||||
selectedPlayer, |
|
||||||
settings, |
|
||||||
history, |
|
||||||
]) |
|
||||||
|
|
||||||
return { |
return { |
||||||
handlePlayerClick, |
handlePlayerClick, |
||||||
handleWatchEpisodesClick, |
handlePlaylistClick, |
||||||
selectedPlayer, |
selectedPlaylist, |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue