fix(#in-272): fix refresh list of the sports in filter

pull/64/head
Andrei Dekterev 3 years ago
parent a9925f7bb8
commit bf52b61309
  1. 11
      src/features/HeaderFilters/store/hooks/index.tsx
  2. 9
      src/features/MatchesGrid/index.tsx
  3. 11
      src/features/TournamentList/hooks.tsx

@ -13,6 +13,7 @@ import { useQueryParamStore } from 'hooks'
import { filterKeys } from '../config' import { filterKeys } from '../config'
import { isValidDate } from '../helpers/isValidDate' import { isValidDate } from '../helpers/isValidDate'
import { Match } from '../../../Matches'
export const useFilters = () => { export const useFilters = () => {
const { search } = useLocation() const { search } = useLocation()
@ -31,6 +32,14 @@ export const useFilters = () => {
const [sportIds, setSportIds] = useState<any>() const [sportIds, setSportIds] = useState<any>()
const isTodaySelected = isToday(selectedDate) const isTodaySelected = isToday(selectedDate)
const getSportIds = useCallback((matches:Array<Match>) => {
const sportTypeIds = new Set<number>([])
matches.forEach((match) => {
sportTypeIds.add(match.sportType)
})
setSportIds(Array.from(sportTypeIds))
}, [])
const resetFilters = useCallback(() => { const resetFilters = useCallback(() => {
setIsShowTournament(true) setIsShowTournament(true)
setSelectedFilters([]) setSelectedFilters([])
@ -61,6 +70,7 @@ export const useFilters = () => {
]) ])
const store = useMemo(() => ({ const store = useMemo(() => ({
getSportIds,
isShowTournament, isShowTournament,
isTodaySelected, isTodaySelected,
resetFilters, resetFilters,
@ -79,6 +89,7 @@ export const useFilters = () => {
sportIds, sportIds,
updateDate, updateDate,
}), [ }), [
getSportIds,
isShowTournament, isShowTournament,
isTodaySelected, isTodaySelected,
resetFilters, resetFilters,

@ -1,4 +1,4 @@
import { memo } from 'react' import { memo, useEffect } from 'react'
import { useRouteMatch } from 'react-router-dom' import { useRouteMatch } from 'react-router-dom'
import { PAGES } from 'config/pages' import { PAGES } from 'config/pages'
@ -18,7 +18,9 @@ export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
const isHomePage = useRouteMatch(PAGES.home)?.isExact const isHomePage = useRouteMatch(PAGES.home)?.isExact
const { const {
getSportIds,
isShowTournament, isShowTournament,
selectedDate,
selectedFilters, selectedFilters,
selectedLeague, selectedLeague,
} = useHeaderFiltersStore() } = useHeaderFiltersStore()
@ -39,6 +41,11 @@ export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
return matches return matches
} }
useEffect(() => {
getSportIds(matches)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDate, matches])
return ( return (
<Wrapper> <Wrapper>
{isHomePage && isShowTournament ? ( {isHomePage && isShowTournament ? (

@ -1,4 +1,4 @@
import { useEffect, useMemo } from 'react' import { useMemo } from 'react'
import orderBy from 'lodash/orderBy' import orderBy from 'lodash/orderBy'
@ -19,10 +19,8 @@ interface TournamentsSortProps {
export const useTournaments = (matches: Array<Match>) => { export const useTournaments = (matches: Array<Match>) => {
const { const {
selectedDate,
selectedLeague, selectedLeague,
selectedSport, selectedSport,
setSportIds,
} = useHeaderFiltersStore() } = useHeaderFiltersStore()
const { isInFavorites } = useUserFavoritesStore() const { isInFavorites } = useUserFavoritesStore()
@ -43,7 +41,6 @@ export const useTournaments = (matches: Array<Match>) => {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
const tournamentSort: Array<TournamentsSortProps> = [] const tournamentSort: Array<TournamentsSortProps> = []
const sportIds = new Set<number>([])
const tournaments = matches.reduce((acc: TournamentListProps, match: Match) => { const tournaments = matches.reduce((acc: TournamentListProps, match: Match) => {
if (matches.length === 0) return {} if (matches.length === 0) return {}
@ -76,7 +73,6 @@ export const useTournaments = (matches: Array<Match>) => {
isLive: match.live, isLive: match.live,
isSuperTournament: Boolean(match.tournament.is_super_tournament), isSuperTournament: Boolean(match.tournament.is_super_tournament),
}) })
sportIds.add(match.sportType)
} else if (compareSport(match, selectedSport) && compareLeague(match.tournament.id)) { } else if (compareSport(match, selectedSport) && compareLeague(match.tournament.id)) {
acc[match.tournament.id] = { acc[match.tournament.id] = {
...acc[match.tournament.id], ...acc[match.tournament.id],
@ -98,11 +94,6 @@ export const useTournaments = (matches: Array<Match>) => {
['desc', 'desc', 'desc'], ['desc', 'desc', 'desc'],
), [tournamentSort]) ), [tournamentSort])
useEffect(() => {
sportIds && setSportIds(sportIds)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDate, matches])
return { return {
tournamentSort: tournamentsSorted, tournamentSort: tournamentsSorted,
tournaments, tournaments,

Loading…
Cancel
Save