fix: #272

reset selectedsport if in current day we don't have this sportId
pull/64/head
Andrei Dekterev 3 years ago
parent 8956dbc169
commit e8dac0541d
  1. 30
      src/features/HeaderFilters/store/hooks/index.tsx
  2. 10
      src/features/MatchesGrid/index.tsx
  3. 12
      src/features/NoNetworkPopup/index.tsx
  4. 4
      src/features/SportsFilter/components/SelectSportPopup/index.tsx
  5. 12
      src/features/TournamentList/hooks.tsx

@ -5,15 +5,20 @@ import {
useState, useState,
} from 'react' } from 'react'
import { useLocation } from 'react-router-dom' import { useLocation } from 'react-router-dom'
import { isToday } from 'date-fns' import { isToday } from 'date-fns'
import type { TournamentType } from 'requests/getMatches/types' import type { TournamentType } from 'requests/getMatches/types'
import { useQueryParamStore } from 'hooks' import { useQueryParamStore } from 'hooks'
import { getSportLexic } from 'helpers'
import { SPORT_NAMES, SportTypes } from 'config'
import { filterKeys } from '../config' import { filterKeys } from '../config'
import { isValidDate } from '../helpers/isValidDate' import { isValidDate } from '../helpers/isValidDate'
import { Match } from '../../../Matches' import type { Match } from '../../../Matches'
export const useFilters = () => { export const useFilters = () => {
const { search } = useLocation() const { search } = useLocation()
@ -32,13 +37,26 @@ 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 compareSport = useCallback((match: Match, sportNames: Array<string>) => {
if (sportNames[0] === 'all_sports') {
return true
}
const sport = getSportLexic(match.sportType)
return (sportNames.indexOf(sport) >= 0 || sportNames.indexOf(`${sport}_popup`) >= 0)
}, [])
const updateSportIds = useCallback((matches:Array<Match>) => {
const sportTypeIds = new Set<number>([]) const sportTypeIds = new Set<number>([])
matches.forEach((match) => { matches.forEach((match) => {
sportTypeIds.add(match.sportType) sportTypeIds.add(match.sportType)
}) })
setSportIds(Array.from(sportTypeIds)) setSportIds(Array.from(sportTypeIds))
}, [])
if (!Array.from(sportTypeIds)?.some((id: SportTypes) => SPORT_NAMES[id] === selectedSport[0])
) {
setSelectedSport(['all_sports'])
}
}, [selectedSport])
const resetFilters = useCallback(() => { const resetFilters = useCallback(() => {
setIsShowTournament(true) setIsShowTournament(true)
@ -70,7 +88,7 @@ export const useFilters = () => {
]) ])
const store = useMemo(() => ({ const store = useMemo(() => ({
getSportIds, compareSport,
isShowTournament, isShowTournament,
isTodaySelected, isTodaySelected,
resetFilters, resetFilters,
@ -88,8 +106,9 @@ export const useFilters = () => {
setSportIds, setSportIds,
sportIds, sportIds,
updateDate, updateDate,
updateSportIds,
}), [ }), [
getSportIds, compareSport,
isShowTournament, isShowTournament,
isTodaySelected, isTodaySelected,
resetFilters, resetFilters,
@ -107,6 +126,7 @@ export const useFilters = () => {
setSportIds, setSportIds,
sportIds, sportIds,
updateDate, updateDate,
updateSportIds,
]) ])
return store return store

@ -18,11 +18,13 @@ export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
const isHomePage = useRouteMatch(PAGES.home)?.isExact const isHomePage = useRouteMatch(PAGES.home)?.isExact
const { const {
getSportIds, compareSport,
isShowTournament, isShowTournament,
selectedDate, selectedDate,
selectedFilters, selectedFilters,
selectedLeague, selectedLeague,
selectedSport,
updateSportIds,
} = useHeaderFiltersStore() } = useHeaderFiltersStore()
const filteredMatches = () => { const filteredMatches = () => {
@ -38,11 +40,11 @@ export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
return matches.filter((match) => ((selectedLeague.indexOf(match.tournament.id) >= 0 return matches.filter((match) => ((selectedLeague.indexOf(match.tournament.id) >= 0
|| selectedLeague[0] === 'all_competitions'))) || selectedLeague[0] === 'all_competitions')))
} }
return matches
}
return matches.filter((match) => compareSport(match, selectedSport))
}
useEffect(() => { useEffect(() => {
getSportIds(matches) updateSportIds(matches)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDate, matches]) }, [selectedDate, matches])

@ -1,6 +1,4 @@
import { useState } from 'react' import { useToggle } from 'hooks'
import { useNoNetworkPopupStore } from 'features/NoNetworkPopup'
import { import {
ArrowLoader, ArrowLoader,
@ -11,24 +9,26 @@ import {
ScButton, ScButton,
} from './styled' } from './styled'
import { useNoNetworkPopupStore } from './store'
export * from './store' export * from './store'
export const NoNetworkPopup = () => { export const NoNetworkPopup = () => {
const [isClose, setIsClose] = useState(false)
const { isOnline } = useNoNetworkPopupStore() const { isOnline } = useNoNetworkPopupStore()
const { isOpen, toggle } = useToggle()
if (isOnline) return null if (isOnline) return null
return ( return (
<ScModalContainer <ScModalContainer
isOpen={!isOnline && !isClose} isOpen={!isOnline && !isOpen}
withCloseButton={false} withCloseButton={false}
> >
<ScBody> <ScBody>
<ArrowLoader /> <ArrowLoader />
<Title t='lost_connection' /> <Title t='lost_connection' />
<SubTitle t='check_connection' /> <SubTitle t='check_connection' />
<ScButton onClick={() => setIsClose(true)}>Ok</ScButton> <ScButton onClick={toggle}>Ok</ScButton>
</ScBody> </ScBody>
</ScModalContainer> </ScModalContainer>
) )

@ -30,9 +30,7 @@ export const SelectSportPopup = ({
sportIds, sportIds,
sports, sports,
}: Props) => { }: Props) => {
const sportNames = sportIds const sportNames = sportIds?.map((id) => String(getSportLexic(Number(id))))
&& Array.from(sportIds)
.map((id) => String(getSportLexic(Number(id))))
sportNames?.unshift('all_sports') sportNames?.unshift('all_sports')
return ( return (

@ -2,8 +2,6 @@ import { useMemo } from 'react'
import orderBy from 'lodash/orderBy' import orderBy from 'lodash/orderBy'
import { getSportLexic } from 'helpers/getSportLexic'
import { ProfileTypes } from 'config' import { ProfileTypes } from 'config'
import { TournamentListProps } from 'features/TournamentList' import { TournamentListProps } from 'features/TournamentList'
import type { Match } from 'features/Matches' import type { Match } from 'features/Matches'
@ -19,19 +17,12 @@ interface TournamentsSortProps {
export const useTournaments = (matches: Array<Match>) => { export const useTournaments = (matches: Array<Match>) => {
const { const {
compareSport,
selectedLeague, selectedLeague,
selectedSport, selectedSport,
} = useHeaderFiltersStore() } = useHeaderFiltersStore()
const { isInFavorites } = useUserFavoritesStore() const { isInFavorites } = useUserFavoritesStore()
const compareSport = (match: Match, sportNames: Array<string>) => {
if (sportNames[0] === 'all_sports') {
return true
}
const sport = getSportLexic(match.sportType)
return (sportNames.indexOf(sport) >= 0 || sportNames.indexOf(`${sport}_popup`) >= 0)
}
const compareLeague = (id: number) => { const compareLeague = (id: number) => {
if (selectedLeague[0] === 'all_competitions') { if (selectedLeague[0] === 'all_competitions') {
return true return true
@ -95,6 +86,7 @@ export const useTournaments = (matches: Array<Match>) => {
), [tournamentSort]) ), [tournamentSort])
return { return {
compareSport,
tournamentSort: tournamentsSorted, tournamentSort: tournamentsSorted,
tournaments, tournaments,
} }

Loading…
Cancel
Save