|
|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
/* eslint-disable no-param-reassign */ |
|
|
|
import { |
|
|
|
import { |
|
|
|
useCallback, |
|
|
|
useCallback, |
|
|
|
useEffect, |
|
|
|
useEffect, |
|
|
|
@ -5,11 +6,16 @@ import { |
|
|
|
useState, |
|
|
|
useState, |
|
|
|
} from 'react' |
|
|
|
} from 'react' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { useQuery } from 'react-query' |
|
|
|
|
|
|
|
import { querieKeys, PAGES } from 'config' |
|
|
|
|
|
|
|
|
|
|
|
import { useRouteMatch } from 'react-router-dom' |
|
|
|
import { useRouteMatch } from 'react-router-dom' |
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
MatchDto, |
|
|
|
MatchDto, |
|
|
|
|
|
|
|
MatchesTimeline, |
|
|
|
TimelineTournamentDto, |
|
|
|
TimelineTournamentDto, |
|
|
|
|
|
|
|
getLiveScores, |
|
|
|
getTimelineMatches, |
|
|
|
getTimelineMatches, |
|
|
|
} from 'requests' |
|
|
|
} from 'requests' |
|
|
|
|
|
|
|
|
|
|
|
@ -17,8 +23,7 @@ import { Match, prepareMatches } from 'helpers' |
|
|
|
|
|
|
|
|
|
|
|
import { useAuthStore } from 'features/AuthStore' |
|
|
|
import { useAuthStore } from 'features/AuthStore' |
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
|
|
|
import { useMatchSwitchesStore } from 'features/MatchSwitches' |
|
|
|
import { PAGES } from 'config' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type TimelineTournamentList = Array<Omit<TimelineTournamentDto, 'matches'> & { |
|
|
|
export type TimelineTournamentList = Array<Omit<TimelineTournamentDto, 'matches'> & { |
|
|
|
matches: Array<Match>, |
|
|
|
matches: Array<Match>, |
|
|
|
@ -35,10 +40,12 @@ export const useTimeline = () => { |
|
|
|
selectedSport, |
|
|
|
selectedSport, |
|
|
|
setSportIds, |
|
|
|
setSportIds, |
|
|
|
} = useHeaderFiltersStore() |
|
|
|
} = useHeaderFiltersStore() |
|
|
|
|
|
|
|
const { isScoreHidden } = useMatchSwitchesStore() |
|
|
|
|
|
|
|
|
|
|
|
const [isTimelineFetching, setIsTimelineFetching] = useState(true) |
|
|
|
const [isTimelineFetching, setIsTimelineFetching] = useState(true) |
|
|
|
const [onlineUpcomingMatches, setOnlineUpcomingMatches] = useState<Array<Match>>([]) |
|
|
|
const [onlineUpcomingMatches, setOnlineUpcomingMatches] = useState<Array<Match>>([]) |
|
|
|
const [tournamentList, setTournamentList] = useState<TimelineTournamentList>([]) |
|
|
|
const [tournamentList, setTournamentList] = useState<TimelineTournamentList>([]) |
|
|
|
|
|
|
|
const [timeline, setTimeline] = useState<MatchesTimeline>() |
|
|
|
|
|
|
|
|
|
|
|
const prepareMatchesDto = useCallback((matches: Array<MatchDto>) => prepareMatches( |
|
|
|
const prepareMatchesDto = useCallback((matches: Array<MatchDto>) => prepareMatches( |
|
|
|
matches, |
|
|
|
matches, |
|
|
|
@ -50,21 +57,22 @@ export const useTimeline = () => { |
|
|
|
(async () => { |
|
|
|
(async () => { |
|
|
|
setIsTimelineFetching(true) |
|
|
|
setIsTimelineFetching(true) |
|
|
|
try { |
|
|
|
try { |
|
|
|
const timeline = await getTimelineMatches() |
|
|
|
const timelineFetched = await getTimelineMatches() |
|
|
|
const convertedMatches = timeline.online_upcoming[0].matches |
|
|
|
setTimeline(timelineFetched) |
|
|
|
|
|
|
|
const convertedMatches = timelineFetched.online_upcoming[0].matches |
|
|
|
const preparedMatches = prepareMatchesDto(convertedMatches) |
|
|
|
const preparedMatches = prepareMatchesDto(convertedMatches) |
|
|
|
setOnlineUpcomingMatches(preparedMatches) |
|
|
|
setOnlineUpcomingMatches(preparedMatches) |
|
|
|
|
|
|
|
|
|
|
|
setTournamentList([ |
|
|
|
setTournamentList([ |
|
|
|
...timeline.favorite.map((item) => ({ |
|
|
|
...timelineFetched.favorite.map((item) => ({ |
|
|
|
...item, |
|
|
|
...item, |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
})), |
|
|
|
})), |
|
|
|
...timeline.promo.map((item) => ({ |
|
|
|
...timelineFetched.promo.map((item) => ({ |
|
|
|
...item, |
|
|
|
...item, |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
})), |
|
|
|
})), |
|
|
|
...timeline.others.map((item) => ({ |
|
|
|
...timelineFetched.others.map((item) => ({ |
|
|
|
...item, |
|
|
|
...item, |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
matches: prepareMatchesDto(item.matches), |
|
|
|
})), |
|
|
|
})), |
|
|
|
@ -76,6 +84,49 @@ export const useTimeline = () => { |
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []) |
|
|
|
}, []) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useQuery({ |
|
|
|
|
|
|
|
queryFn: async () => { |
|
|
|
|
|
|
|
let isLiveMatch = false |
|
|
|
|
|
|
|
if (timeline) { |
|
|
|
|
|
|
|
for (const [, value] of Object.entries(timeline)) { |
|
|
|
|
|
|
|
if (isLiveMatch) break |
|
|
|
|
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
|
|
|
|
value.forEach((t) => { |
|
|
|
|
|
|
|
const liveMatch = t.matches.find((match) => match.live) |
|
|
|
|
|
|
|
if (liveMatch) { |
|
|
|
|
|
|
|
isLiveMatch = true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!isScoreHidden && isLiveMatch) { |
|
|
|
|
|
|
|
const scores = await getLiveScores() |
|
|
|
|
|
|
|
tournamentList.forEach((tournament) => { |
|
|
|
|
|
|
|
tournament.matches.forEach((match) => { |
|
|
|
|
|
|
|
const score = scores.find((s) => ( |
|
|
|
|
|
|
|
s.match_id === match.id && s.sport_id === match.sportType |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
if (score) { |
|
|
|
|
|
|
|
match.team1.score = score?.team1.score ?? match.team1.score |
|
|
|
|
|
|
|
match.team2.score = score?.team2.score ?? match.team2.score |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
onlineUpcomingMatches.forEach((upcomingMatch) => { |
|
|
|
|
|
|
|
const score = scores.find((s) => ( |
|
|
|
|
|
|
|
s.match_id === upcomingMatch.id && s.sport_id === upcomingMatch.sportType |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
if (score) { |
|
|
|
|
|
|
|
upcomingMatch.team1.score = score?.team1.score ?? upcomingMatch.team1.score |
|
|
|
|
|
|
|
upcomingMatch.team2.score = score?.team2.score ?? upcomingMatch.team2.score |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
queryKey: querieKeys.liveMatchScores, |
|
|
|
|
|
|
|
refetchInterval: 30000, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const filteredTournamentsBySport = useMemo(() => { |
|
|
|
const filteredTournamentsBySport = useMemo(() => { |
|
|
|
if (isHomePage && selectedSport) { |
|
|
|
if (isHomePage && selectedSport) { |
|
|
|
return tournamentList.filter((t) => compareSport(t.sport_id, selectedSport)) |
|
|
|
return tournamentList.filter((t) => compareSport(t.sport_id, selectedSport)) |
|
|
|
|