Passed
Push — master ( 4fc8e9...0ebefa )
by Kevin Van
01:32 queued 12s
created

Matches.tsx ➔ getData   C

Complexity

Conditions 9

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 6.6666
c 0
b 0
f 0
cc 9
1
import axios from "axios"
2
import classNames from "classnames"
3
import { Link, graphql, useStaticQuery } from "gatsby"
4
import Moment from "moment-timezone"
5
import "moment/locale/nl-be"
6
import React, { FunctionComponent, useEffect, useState } from "react"
7
import LazyLoad from "react-lazyload"
8
9
import { mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
10
import Icon from "./Icon"
11
import "./Matches.scss"
12
import Spinner from "./Spinner"
13
14
const MatchesRow: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
15
  const d = Moment.tz(match.date, `Europe/Brussels`)
16
  const date = d.format(`dddd D MMMM YYYY`)
17
  const time = d.format(`HH:mm`)
18
  const matchPlayed =
19
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
20
    false
21
22
  return (
23
    <article>
24
      <header className="matches__calendar__title">
25
        <h3>
26
          <span className="matches__calendar__date">{date}</span>
27
          <span className="matches__calendar__separator">|</span>
28
          <span className="matches__calendar__type">{match.competitionType}</span>
29
        </h3>
30
      </header>
31
      <main className="matches__calendar__main">
32
        <div
33
          className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
34
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
35
          })}
36
        >
37
          {match.homeClub?.abbreviation || match.homeClub?.name}
38
          <LazyLoad debounce={false}>
39
            <img
40
              src={match.homeClub?.logo}
41
              alt={match.homeClub?.name}
42
              className="matches__calendar__logo matches__calendar__logo--home"
43
            />
44
          </LazyLoad>
45
        </div>
46
47
        <div className="matches__calendar__score">
48
          {match.status !== 0 && (
49
            <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
50
          )}
51
          {(match.status === 0 || match.status === null) && !matchPlayed && <span>{time}</span>}
52
          {matchPlayed && (
53
            <span>
54
              {match.goalsHomeTeam} - {match.goalsAwayTeam}
55
            </span>
56
          )}
57
        </div>
58
59
        <div
60
          className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
61
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
62
          })}
63
        >
64
          <LazyLoad debounce={false}>
65
            <img
66
              src={match.awayClub?.logo}
67
              alt={match.awayClub?.name}
68
              className="matches__calendar__logo matches__calendar__logo--away"
69
            />
70
          </LazyLoad>
71
72
          {match.awayClub?.abbreviation || match.awayClub?.name}
73
        </div>
74
75
        <Link to={`/game/${match.id}`} className="matches__calendar__link">
76
          <Icon icon="fa-info-circle" />
77
        </Link>
78
      </main>
79
    </article>
80
  )
81
}
82
83
const Matches: FunctionComponent<MatchesProps> = ({ teamId }: MatchesProps) => {
84
  const [data, setData] = useState<Match[]>([])
85
86
  const {
87
    site: {
88
      siteMetadata: { kcvvPsdApi },
89
    },
90
  }: MatchesQueryData = useStaticQuery(graphql`
91
    {
92
      site {
93
        siteMetadata {
94
          kcvvPsdApi
95
        }
96
      }
97
    }
98
  `)
99
100
  Moment.locale(`nl-BE`)
101
102
  useEffect(() => {
103
    async function getData() {
104
      const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`)
105
      setData(response.data)
106
    }
107
    getData()
108
  }, [kcvvPsdApi, teamId])
109
110
  return (
111
    <div className={`matches__wrapper`}>
112
      {data.length > 0 || <Spinner />}
113
      {data
114
        .sort((a, b) => a.timestamp - b.timestamp)
115
        .map((match, i) => (
116
          <MatchesRow match={match} key={i} />
117
        ))}
118
    </div>
119
  )
120
}
121
122
export default Matches
123