|
|
|
|
@ -1,9 +1,20 @@ |
|
|
|
|
import { useEffect } from 'react' |
|
|
|
|
import { useEffect, useMemo } from 'react' |
|
|
|
|
|
|
|
|
|
import orderBy from 'lodash/orderBy' |
|
|
|
|
|
|
|
|
|
import { getSportLexic } from 'helpers/getSportLexic' |
|
|
|
|
|
|
|
|
|
import { ProfileTypes } from 'config' |
|
|
|
|
import { TournamentListProps } from 'features/TournamentList' |
|
|
|
|
import type { Match } from 'features/Matches' |
|
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
import { useUserFavoritesStore } from 'features/UserFavorites/store' |
|
|
|
|
|
|
|
|
|
interface TournamentsSortProps { |
|
|
|
|
id: number, |
|
|
|
|
isFavorite: boolean, |
|
|
|
|
isLive: boolean, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
const { |
|
|
|
|
@ -12,6 +23,7 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
selectedSport, |
|
|
|
|
setSportIds, |
|
|
|
|
} = useHeaderFiltersStore() |
|
|
|
|
const { isInFavorites } = useUserFavoritesStore() |
|
|
|
|
|
|
|
|
|
const compareSport = (match: Match, sportNames: Array<string>) => { |
|
|
|
|
if (sportNames[0] === 'all_sports') { |
|
|
|
|
@ -28,16 +40,24 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
return (selectedLeague.indexOf(id) >= 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const tournamentSort: Array<number> = [] |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
const tournamentSort: Array<TournamentsSortProps> = [] |
|
|
|
|
const sportIds = new Set<number>([]) |
|
|
|
|
|
|
|
|
|
const tournaments = matches.reduce((acc: TournamentListProps, match: Match) => { |
|
|
|
|
if (matches.length === 0) return {} |
|
|
|
|
|
|
|
|
|
const tournamentInFavorites = isInFavorites( |
|
|
|
|
ProfileTypes.TOURNAMENTS, |
|
|
|
|
match.tournament.id, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (!acc[match.tournament.id] && compareSport(match, selectedSport) |
|
|
|
|
&& compareLeague(match.tournament.id)) { |
|
|
|
|
&& compareLeague(match.tournament.id)) { |
|
|
|
|
const tournament = { |
|
|
|
|
...match.tournament, |
|
|
|
|
countryId: match.countryId, |
|
|
|
|
isFavorite: tournamentInFavorites, |
|
|
|
|
live: match.live, |
|
|
|
|
matches: [match], |
|
|
|
|
sportType: match.sportType, |
|
|
|
|
@ -48,7 +68,11 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
}, |
|
|
|
|
tournamentMatches: [match], |
|
|
|
|
} |
|
|
|
|
tournamentSort.push(match.tournament.id) |
|
|
|
|
tournamentSort.push({ |
|
|
|
|
id: match.tournament.id, |
|
|
|
|
isFavorite: tournamentInFavorites, |
|
|
|
|
isLive: match.live, |
|
|
|
|
}) |
|
|
|
|
sportIds.add(match.sportType) |
|
|
|
|
} else if (compareSport(match, selectedSport) && compareLeague(match.tournament.id)) { |
|
|
|
|
acc[match.tournament.id] = { |
|
|
|
|
@ -65,13 +89,19 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
return acc |
|
|
|
|
}, {}) |
|
|
|
|
|
|
|
|
|
const tournamentsSorted = useMemo(() => orderBy( |
|
|
|
|
tournamentSort, |
|
|
|
|
['isLive', 'isFavorite'], |
|
|
|
|
['desc', 'desc'], |
|
|
|
|
), [tournamentSort]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
sportIds && setSportIds(sportIds) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [selectedDate, matches]) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
tournamentSort, |
|
|
|
|
tournamentSort: tournamentsSorted, |
|
|
|
|
tournaments, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|