Ott 586 new filter for match (#253)

* feat: 🎸 OTT-586

filter

* feat: 🎸 OTT-586-new-filter-for-match

add filter for matches by status

* feat: 🎸 OTT-586

fix line

* feat: 🎸 OTT-586

fix line

Co-authored-by: Zoia <zizi2405@yandex.ru>
keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
Zoia 5 years ago committed by GitHub
parent ac2e0a55a0
commit f15ab39e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 54
      src/features/HomePage/hooks.tsx

@ -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 }
}

Loading…
Cancel
Save