|
|
|
|
@ -1,10 +1,54 @@ |
|
|
|
|
import { useCallback } from 'react' |
|
|
|
|
|
|
|
|
|
import { getHomeMatches } from 'requests' |
|
|
|
|
import filter from 'lodash/filter' |
|
|
|
|
import isPast from 'date-fns/isPast' |
|
|
|
|
import differenceInMinutes from 'date-fns/differenceInMinutes' |
|
|
|
|
|
|
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
import { |
|
|
|
|
getHomeMatches, |
|
|
|
|
Matches, |
|
|
|
|
MatchesBySection, |
|
|
|
|
} from 'requests' |
|
|
|
|
|
|
|
|
|
import { MatchStatuses, useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
import { useMatchSwitchesStore } from 'features/MatchSwitches' |
|
|
|
|
|
|
|
|
|
const matchesFilteredByStatus = (matches : Matches, status : MatchStatuses | null) => { |
|
|
|
|
if (!status) return matches |
|
|
|
|
const filteredMatches = filter(matches, (match) => { |
|
|
|
|
const localDate = new Date(`${match.date}+03:00`) |
|
|
|
|
const matchIsStarted = isPast(new Date(match.date)) |
|
|
|
|
const difTime = differenceInMinutes(new Date(), localDate) |
|
|
|
|
|
|
|
|
|
switch (status) { |
|
|
|
|
case MatchStatuses.Soon: |
|
|
|
|
return !matchIsStarted && (difTime > -60) |
|
|
|
|
case MatchStatuses.Live: |
|
|
|
|
return match.live && matchIsStarted |
|
|
|
|
case MatchStatuses.Finished: |
|
|
|
|
return matchIsStarted && (match.storage || match.has_video) |
|
|
|
|
default: return false |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
return filteredMatches |
|
|
|
|
} |
|
|
|
|
const setMatches = ( |
|
|
|
|
matches : MatchesBySection, |
|
|
|
|
status : MatchStatuses | null, |
|
|
|
|
): MatchesBySection => { |
|
|
|
|
if (matches.isVideoSections) { |
|
|
|
|
return { |
|
|
|
|
...matches, |
|
|
|
|
broadcast: matchesFilteredByStatus(matches.broadcast, status), |
|
|
|
|
features: matchesFilteredByStatus(matches.features, status), |
|
|
|
|
highlights: matchesFilteredByStatus(matches.highlights, status), |
|
|
|
|
} |
|
|
|
|
} return { |
|
|
|
|
...matches, |
|
|
|
|
broadcast: matchesFilteredByStatus(matches.broadcast, status), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useHomePage = () => { |
|
|
|
|
const { |
|
|
|
|
selectedDateFormatted, |
|
|
|
|
@ -19,11 +63,12 @@ export const useHomePage = () => { |
|
|
|
|
availableMatchesOnly, |
|
|
|
|
date: selectedDateFormatted, |
|
|
|
|
limit, |
|
|
|
|
matchStatus: selectedMatchStatus, |
|
|
|
|
matchStatus: null, |
|
|
|
|
offset, |
|
|
|
|
sportType: selectedSportTypeId, |
|
|
|
|
tournamentId: selectedTournamentId, |
|
|
|
|
}), |
|
|
|
|
}) |
|
|
|
|
.then((matches) => setMatches(matches, selectedMatchStatus)), |
|
|
|
|
[ |
|
|
|
|
selectedDateFormatted, |
|
|
|
|
selectedMatchStatus, |
|
|
|
|
@ -32,6 +77,5 @@ export const useHomePage = () => { |
|
|
|
|
availableMatchesOnly, |
|
|
|
|
], |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
return { fetchMatches } |
|
|
|
|
} |
|
|
|
|
|