From f15ab39e7946aeec7e25ef7e7efe3ae50bf3a072 Mon Sep 17 00:00:00 2001 From: Zoia <43918051+zizi62@users.noreply.github.com> Date: Fri, 18 Dec 2020 09:52:14 -0500 Subject: [PATCH] Ott 586 new filter for match (#253) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- src/features/HomePage/hooks.tsx | 54 ++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/features/HomePage/hooks.tsx b/src/features/HomePage/hooks.tsx index 5ae0188a..9e258d58 100644 --- a/src/features/HomePage/hooks.tsx +++ b/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 } }