|
|
|
|
@ -1,3 +1,4 @@ |
|
|
|
|
import { useEffect } from 'react' |
|
|
|
|
import { getSportLexic } from 'helpers/getSportLexic' |
|
|
|
|
|
|
|
|
|
import { TournamentListProps } from 'features/TournamentList' |
|
|
|
|
@ -5,7 +6,11 @@ import type { Match } from 'features/Matches' |
|
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
|
|
|
|
|
export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
const { selectedSport } = useHeaderFiltersStore() |
|
|
|
|
const { |
|
|
|
|
selectedLeague, |
|
|
|
|
selectedSport, |
|
|
|
|
setSportIds, |
|
|
|
|
} = useHeaderFiltersStore() |
|
|
|
|
|
|
|
|
|
const compareSport = (match: Match, sportNames: Array<string>) => { |
|
|
|
|
if (sportNames[0] === 'all_sports') { |
|
|
|
|
@ -15,11 +20,20 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
return (sportNames.indexOf(sport) >= 0 || sportNames.indexOf(`${sport}_popup`) >= 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const compareLeague = (id: number) => { |
|
|
|
|
if (selectedLeague[0] === 'all_competitions') { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
return (selectedLeague.indexOf(id) >= 0) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const tournamentSort: Array<number> = [] |
|
|
|
|
const sportIds = new Set<number>([]) |
|
|
|
|
|
|
|
|
|
const tournaments = matches.reduce((acc: TournamentListProps, match: Match) => { |
|
|
|
|
if (matches.length === 0) return {} |
|
|
|
|
if (!acc[match.tournament.id] && compareSport(match, selectedSport)) { |
|
|
|
|
if (!acc[match.tournament.id] && compareSport(match, selectedSport) |
|
|
|
|
&& compareLeague(match.tournament.id)) { |
|
|
|
|
const tournament = { |
|
|
|
|
...match.tournament, |
|
|
|
|
countryId: match.countryId, |
|
|
|
|
@ -34,7 +48,8 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
tournamentMatches: [match], |
|
|
|
|
} |
|
|
|
|
tournamentSort.push(match.tournament.id) |
|
|
|
|
} else if (compareSport(match, selectedSport)) { |
|
|
|
|
sportIds.add(match.sportType) |
|
|
|
|
} else if (compareSport(match, selectedSport) && compareLeague(match.tournament.id)) { |
|
|
|
|
acc[match.tournament.id] = { |
|
|
|
|
...acc[match.tournament.id], |
|
|
|
|
tournament: { |
|
|
|
|
@ -49,6 +64,11 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
return acc |
|
|
|
|
}, {}) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
sportIds && setSportIds(sportIds) |
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [matches]) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
tournamentSort, |
|
|
|
|
tournaments, |
|
|
|
|
|