{"size": 4163, "ext": "js", "max_stars_repo_path": "app/src/data/fetcher/SportFetcher.js", "max_stars_repo_name": "sorawit/band", "max_stars_repo_stars_event_min_datetime": "2019-08-15T04:08:55.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-14T07:29:10.000Z", "max_issues_repo_path": "app/src/data/fetcher/SportFetcher.js", "max_issues_repo_name": "sorawit/band", "max_issues_repo_issues_event_min_datetime": "2019-08-15T10:12:57.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-28T00:10:11.000Z", "max_forks_repo_path": "app/src/data/fetcher/SportFetcher.js", "max_forks_repo_name": "sorawit/band", "max_forks_repo_forks_event_min_datetime": "2019-08-29T01:50:53.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-12T10:46:47.000Z"}
{"max_stars_count": 147, "max_issues_count": 172, "max_forks_count": 16, "avg_line_length": 28.5136986301, "max_line_length": 80, "alphanum_fraction": 0.560653375}
import BaseFetcher from 'data/BaseFetcher'
import { withRouter } from 'react-router-dom'
import moment from 'moment'
import { Utils } from 'band.js'
import { decodeScores } from 'utils/helper'
import { getProvider } from 'data/Providers'
import { getSportTeamByCode } from 'utils/sportTeam'

export const SportCountByTCDFetcher = withRouter(
  class extends BaseFetcher {
    shouldFetch(prevProps) {
      return (
        prevProps.tcdAddress !== this.props.tcdAddress ||
        prevProps.home !== this.props.home ||
        prevProps.away !== this.props.away ||
        prevProps.type !== this.props.type
      )
    }

    async fetch() {
      const { type } = this.props
      let params = { key: type }
      const { home, away } = this.props
      params = home ? { ...params, home } : { ...params }
      params = away ? { ...params, away } : { ...params }

      const sportCount = await Utils.getDataRequest(
        `/sports/${this.props.tcdAddress}/count`,
        params,
      )
      return sportCount
    }
  },
)

export const SportByTCDFetcher = withRouter(
  class extends BaseFetcher {
    shouldFetch(prevProps) {
      return (
        prevProps.tcdAddress !== this.props.tcdAddress ||
        prevProps.currentPage !== this.props.currentPage ||
        prevProps.home !== this.props.home ||
        prevProps.away !== this.props.away ||
        prevProps.type !== this.props.type
      )
    }

    async fetch() {
      const {
        tcdAddress,
        currentPage,
        nSportList,
        home,
        away,
        type,
      } = this.props
      const skip = (currentPage - 1) * nSportList
      let params =
        skip > 0
          ? {
              limit: nSportList,
              skip,
              key: type,
            }
          : {
              limit: nSportList,
              key: type,
            }
      params = home ? { ...params, home } : { ...params }
      params = away ? { ...params, away } : { ...params }

      const sports = await Utils.getDataRequest(`/sports/${tcdAddress}`, params)

      return sports.map(sport => {
        const home = sport.home
        const away = sport.away
        const [scoreHome, scoreAway] = decodeScores(sport.value)
        return {
          time: moment(new Date(sport.date).getTime()),
          hasStartTime: sport.sportStartTime && sport.sportStartTime !== '9999',
          lastUpdate: moment(sport.timestamp * 1000),
          keyOnChain: sport.key,
          home,
          away,
          homeFullName: getSportTeamByCode(sport.league, home).label,
          awayFullName: getSportTeamByCode(sport.league, away).label,
          scoreAway: scoreAway,
          scoreHome: scoreHome,
        }
      })
    }
  },
)

export const SportProvidersByTypeTimeTeamFetcher = withRouter(
  class extends BaseFetcher {
    shouldFetch(prevProps) {
      return (
        prevProps.tcdAddress !== this.props.tcdAddress ||
        prevProps.keyOnChain !== this.props.keyOnChain ||
        prevProps.home !== this.props.home ||
        prevProps.away !== this.props.away
      )
    }

    async fetch() {
      const { tcdAddress, keyOnChain, home, away } = this.props

      const reports = await Utils.getDataRequest(`/${tcdAddress}/data-points`, {
        key: keyOnChain,
      })

      const { reportedData } = reports[0] || {
        reportedData: [],
        time: Date.now(),
      }

      const providers = {}
      for (const [k, v] of Object.entries(reportedData)) {
        const { timestamp, value } = v
        const [scoreHome, scoreAway] = decodeScores(value)
        providers[k] = {
          name: getProvider(k).name,
          image: getProvider(k).image,
          address: k,
          away: away,
          home: home,
          lastUpdate: moment(timestamp * 1000),
          scoreAway: scoreAway,
          scoreHome: scoreHome,
          status: 'status',
        }
      }

      const allProviders = Object.values(providers).map(
        ({ balls, ...rest }) => ({ ...balls, ...rest }),
      )
      allProviders.sort((a, b) =>
        a.lastUpdate.isBefore(b.lastUpdate) ? 1 : -1,
      )

      return allProviders
    }
  },
)
