feat(#357): add refresh scores for home page

pull/96/head
Andrei Dekterev 3 years ago
parent 02ca2e1400
commit 0e68373893
  1. 1
      .eslintrc
  2. 4
      src/config/queries.tsx
  3. 7
      src/features/MatchCard/CardFrontside/index.tsx
  4. 6
      src/features/MatchCard/index.tsx
  5. 3
      src/features/MatchPage/components/MatchDescription/index.tsx
  6. 30
      src/features/MatchesGrid/index.tsx
  7. 28
      src/requests/getLiveScores.tsx

@ -11,6 +11,7 @@
"postro4no" "postro4no"
], ],
"rules": { "rules": {
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/array-type": [ "@typescript-eslint/array-type": [
"warn", "warn",
{ "default" : "generic" } { "default" : "generic" }

@ -0,0 +1,4 @@
export const querieKeys = {
matchScore: 'matchScore',
liveMatchScores: 'liveMatchScores',
}

@ -41,12 +41,14 @@ import {
import { useCardPreview } from './hooks' import { useCardPreview } from './hooks'
import { getPrepareTimeFormat } from '../helpers' import { getPrepareTimeFormat } from '../helpers'
import { getPrepareDateFormat } from '../helpers/getPrepareDateFormat' import { getPrepareDateFormat } from '../helpers/getPrepareDateFormat'
import { LiveScore } from '../../../requests/getLiveScores'
type Props = { type Props = {
isNeedFormatTimeChanged: boolean, isNeedFormatTimeChanged: boolean,
match: Match, match: Match,
onClick: () => void, onClick: () => void,
onKeyPress: (e: KeyboardEvent<HTMLLIElement>) => void, onKeyPress: (e: KeyboardEvent<HTMLLIElement>) => void,
score?: LiveScore,
} }
export const CardFrontside = ({ export const CardFrontside = ({
@ -54,6 +56,7 @@ export const CardFrontside = ({
match, match,
onClick, onClick,
onKeyPress, onKeyPress,
score,
}: Props) => { }: Props) => {
const location = useLocation() const location = useLocation()
const { const {
@ -165,14 +168,14 @@ export const CardFrontside = ({
<TeamName nameObj={team1} /> <TeamName nameObj={team1} />
{team1InFavorites && <FavoriteSign />} {team1InFavorites && <FavoriteSign />}
</NameSignWrapper> </NameSignWrapper>
{showScore && <Score>{team1.score}</Score>} {showScore && <Score>{score?.team1.score ?? team1.score}</Score>}
</Team> </Team>
<Team isMatchPage={isMatchPage}> <Team isMatchPage={isMatchPage}>
<NameSignWrapper> <NameSignWrapper>
<TeamName nameObj={team2} /> <TeamName nameObj={team2} />
{team2InFavorites && <FavoriteSign />} {team2InFavorites && <FavoriteSign />}
</NameSignWrapper> </NameSignWrapper>
{showScore && <Score>{team2.score}</Score>} {showScore && <Score>{score?.team2.score ?? team2.score}</Score>}
</Team> </Team>
</Teams> </Teams>
{!isMatchPage && ( {!isMatchPage && (

@ -2,15 +2,18 @@ import type { Match } from 'features/Matches'
import { isMobileDevice } from 'config/userAgent' import { isMobileDevice } from 'config/userAgent'
import { LiveScore } from 'requests/getLiveScores'
import { CardFrontside } from './CardFrontside' import { CardFrontside } from './CardFrontside'
import { CardFrontsideMobile } from './CardFrontside/MatchCardMobile' import { CardFrontsideMobile } from './CardFrontside/MatchCardMobile'
import { useCard } from './hooks' import { useCard } from './hooks'
type Props = { type Props = {
match: Match, match: Match,
score?: LiveScore,
} }
export const MatchCard = ({ match }: Props) => { export const MatchCard = ({ match, score }: Props) => {
const { const {
isNeedFormatTimeChanged, isNeedFormatTimeChanged,
isOwnedMatches, isOwnedMatches,
@ -27,6 +30,7 @@ export const MatchCard = ({ match }: Props) => {
onKeyPress={onKeyPress} onKeyPress={onKeyPress}
isNeedFormatTimeChanged={isNeedFormatTimeChanged} isNeedFormatTimeChanged={isNeedFormatTimeChanged}
isOwnedMatches={isOwnedMatches} isOwnedMatches={isOwnedMatches}
score={score}
/> />
) )
} }

@ -20,6 +20,7 @@ import { parseDate } from 'helpers/parseDate'
import { ProfileTypes } from 'config' import { ProfileTypes } from 'config'
import { isMobileDevice } from 'config/userAgent' import { isMobileDevice } from 'config/userAgent'
import { querieKeys } from 'config/queries'
import { usePageParams } from 'hooks/usePageParams' import { usePageParams } from 'hooks/usePageParams'
@ -61,7 +62,7 @@ export const MatchDescription = () => {
return null return null
}, },
queryKey: 'matchScore', queryKey: querieKeys.matchScore,
refetchInterval: 5000, refetchInterval: 5000,
}) })

@ -1,14 +1,19 @@
import { memo, useEffect } from 'react' import { memo, useEffect } from 'react'
import { useRouteMatch } from 'react-router-dom' import { useRouteMatch } from 'react-router-dom'
import { useQuery } from 'react-query'
import { PAGES } from 'config/pages' import { PAGES } from 'config/pages'
import { getLiveScores, LiveScore } from 'requests/getLiveScores'
import { MatchCard } from 'features/MatchCard' import { MatchCard } from 'features/MatchCard'
import { TournamentList } from 'features/TournamentList' import { TournamentList } from 'features/TournamentList'
import type { Match } from 'features/Matches' import type { Match } from 'features/Matches'
import { useHeaderFiltersStore } from 'features/HeaderFilters' import { useHeaderFiltersStore } from 'features/HeaderFilters'
import { Wrapper } from './styled' import { Wrapper } from './styled'
import { useMatchSwitchesStore } from '../MatchSwitches'
import { querieKeys } from '../../config/queries'
type MatchesGridProps = { type MatchesGridProps = {
matches: Array<Match>, matches: Array<Match>,
@ -16,6 +21,7 @@ type MatchesGridProps = {
export const MatchesGrid = memo(({ matches }: MatchesGridProps) => { export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
const isHomePage = useRouteMatch(PAGES.home)?.isExact const isHomePage = useRouteMatch(PAGES.home)?.isExact
const { isScoreHidden } = useMatchSwitchesStore()
const { const {
compareSport, compareSport,
@ -48,19 +54,39 @@ export const MatchesGrid = memo(({ matches }: MatchesGridProps) => {
return matches return matches
} }
const { data: liveMatchScores } = useQuery({
queryFn: async () => {
if (!isScoreHidden && matches.filter(({ live }) => live)?.length > 0) {
const scores = await getLiveScores()
return scores
}
return []
},
queryKey: querieKeys.liveMatchScores,
refetchInterval: 5000,
})
useEffect(() => { useEffect(() => {
if (!isHomePage) return if (!isHomePage) return
updateSportIds(matches) updateSportIds(matches)
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDate, matches]) }, [selectedDate, matches])
return ( return (
<Wrapper> <Wrapper>
{isHomePage && isShowTournament ? ( {isHomePage && isShowTournament ? (
<TournamentList matches={filteredMatches()} /> <TournamentList matches={filteredMatches()} />
) : ( ) : (
filteredMatches().map((match) => <MatchCard key={match.id} match={match} />) filteredMatches().map((match) => (
<MatchCard
key={match.id}
match={match}
score={liveMatchScores?.find(
({ match_id, sport_id }: LiveScore) => match_id === match.id
&& sport_id === match.sportType,
)}
/>
))
)} )}
</Wrapper> </Wrapper>
) )

@ -0,0 +1,28 @@
import { callApi } from 'helpers'
import { API_ROOT } from 'config'
type ScoreTeam = {
id: number,
score: number | null,
}
export type LiveScore = {
match_id: number,
sport_id: number,
team1:ScoreTeam,
team2: ScoreTeam,
}
export const getLiveScores = (): Promise<Array<LiveScore>> => {
const url = `${API_ROOT}/v1/matches/live/scores`
const config = {
method: 'GET',
}
return callApi({
config,
url,
})
}
Loading…
Cancel
Save