|
|
|
|
@ -2,7 +2,7 @@ import { useMemo } from 'react' |
|
|
|
|
|
|
|
|
|
import orderBy from 'lodash/orderBy' |
|
|
|
|
|
|
|
|
|
import { ProfileTypes } from 'config' |
|
|
|
|
import { ProfileTypes, SportTypes } from 'config' |
|
|
|
|
import { TournamentListProps } from 'features/TournamentList' |
|
|
|
|
import type { Match } from 'features/Matches' |
|
|
|
|
import { useHeaderFiltersStore } from 'features/HeaderFilters' |
|
|
|
|
@ -13,6 +13,7 @@ interface TournamentsSortProps { |
|
|
|
|
isFavorite: boolean, |
|
|
|
|
isLive: boolean, |
|
|
|
|
isSuperTournament: boolean, |
|
|
|
|
sportType: SportTypes, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
@ -23,7 +24,7 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
} = useHeaderFiltersStore() |
|
|
|
|
const { isInFavorites } = useUserFavoritesStore() |
|
|
|
|
|
|
|
|
|
const compareLeague = (id: number) => { |
|
|
|
|
const compareLeague = (id: number| string) => { |
|
|
|
|
if (selectedLeague[0] === 'all_competitions') { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
@ -36,14 +37,16 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
const tournaments = matches.reduce((acc: TournamentListProps, match: Match) => { |
|
|
|
|
if (matches.length === 0) return {} |
|
|
|
|
|
|
|
|
|
const uniqTournamentId = `${match.sportType}_${match.tournament.id}` |
|
|
|
|
|
|
|
|
|
const tournamentInFavorites = isInFavorites( |
|
|
|
|
ProfileTypes.TOURNAMENTS, |
|
|
|
|
// в избранном могут быть только обычные турниры
|
|
|
|
|
match.tournament.is_super_tournament ? match.group.id : match.tournament.id, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (!acc[match.tournament.id] && compareSport(match, selectedSport) |
|
|
|
|
&& compareLeague(match.tournament.id)) { |
|
|
|
|
if (!acc[`${match.sportType}_${match.tournament.id}`] && compareSport(match, selectedSport) |
|
|
|
|
&& compareLeague(uniqTournamentId)) { |
|
|
|
|
const tournament = { |
|
|
|
|
...match.tournament, |
|
|
|
|
countryId: match.countryId, |
|
|
|
|
@ -52,7 +55,7 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
matches: [match], |
|
|
|
|
sportType: match.sportType, |
|
|
|
|
} |
|
|
|
|
acc[match.tournament.id] = { |
|
|
|
|
acc[uniqTournamentId] = { |
|
|
|
|
tournament: { |
|
|
|
|
...tournament, |
|
|
|
|
}, |
|
|
|
|
@ -63,17 +66,18 @@ export const useTournaments = (matches: Array<Match>) => { |
|
|
|
|
isFavorite: tournamentInFavorites, |
|
|
|
|
isLive: match.live, |
|
|
|
|
isSuperTournament: Boolean(match.tournament.is_super_tournament), |
|
|
|
|
sportType: match.sportType, |
|
|
|
|
}) |
|
|
|
|
} else if (compareSport(match, selectedSport) && compareLeague(match.tournament.id)) { |
|
|
|
|
acc[match.tournament.id] = { |
|
|
|
|
...acc[match.tournament.id], |
|
|
|
|
} else if (compareSport(match, selectedSport) && compareLeague(uniqTournamentId)) { |
|
|
|
|
acc[uniqTournamentId] = { |
|
|
|
|
...acc[uniqTournamentId], |
|
|
|
|
tournament: { |
|
|
|
|
...acc[match.tournament.id].tournament, |
|
|
|
|
live: acc[match.tournament.id]?.tournament.live |
|
|
|
|
? acc[match.tournament.id]?.tournament.live |
|
|
|
|
...acc[uniqTournamentId].tournament, |
|
|
|
|
live: acc[uniqTournamentId]?.tournament.live |
|
|
|
|
? acc[uniqTournamentId]?.tournament.live |
|
|
|
|
: match.live, |
|
|
|
|
}, |
|
|
|
|
tournamentMatches: [...acc[match.tournament.id].tournamentMatches, match], |
|
|
|
|
tournamentMatches: [...acc[uniqTournamentId].tournamentMatches, match], |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return acc |
|
|
|
|
|