Passed
Push — feature/matches-tsx ( 2e76e9 )
by Kevin Van
06:29
created

src/components/Matches.tsx   A

Complexity

Total Complexity 1
Complexity/F 1

Size

Lines of Code 121
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 94
mnd 0
bc 0
fnc 1
dl 0
loc 121
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Matches.tsx ➔ getData 0 3 1
1
import React, { FunctionComponent, useEffect, useState } from "react"
2
import { graphql, Link, useStaticQuery } from "gatsby"
3
4
import axios from "axios"
5
import LazyLoad from "react-lazyload"
6
import classNames from "classnames"
7
import Moment from "moment-timezone"
8
import "moment/locale/nl-be"
9
10
import { mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
11
12
import "./Matches.scss"
13
import Icon from "./Icon"
14
15
const MatchesRow: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => {
16
  const d = Moment.tz(match.date, `Europe/Brussels`)
17
  const date = d.format(`dddd D MMMM YYYY`)
18
  const time = d.format(`HH:mm`)
19
  const matchPlayed = (match.status === 0 && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) || false
20
21
  return (
22
    <article>
23
      <header className="matches__calendar__title">
24
        <h3>
25
          <span className="matches__calendar__date">{date}</span>
26
          <span className="matches__calendar__separator">|</span>
27
          <span className="matches__calendar__type">{match.competitionType}</span>
28
        </h3>
29
      </header>
30
      <main className="matches__calendar__main">
31
        <div
32
          className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
33
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
34
          })}
35
        >
36
          {match.homeClub.abbreviation || match.homeClub.name}
37
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 && !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
          {match.awayClub.abbreviation || match.awayClub.name}
72
        </div>
73
74
        <Link to={`/game/${match.id}`} className="matches__calendar__link">
75
          <Icon icon="fa-info-circle" />
76
        </Link>
77
      </main>
78
    </article>
79
  )
80
}
81
82
const Matches: FunctionComponent<MatchesProps> = ({ teamId }: MatchesProps) => {
83
  const [data, setData] = useState<Match[]>([])
84
85
  const {
86
    site: {
87
      siteMetadata: { kcvvPsdApi },
88
    },
89
  }: MatchesQueryData = useStaticQuery(graphql`
90
    {
91
      site {
92
        siteMetadata {
93
          kcvvPsdApi
94
        }
95
      }
96
    }
97
  `)
98
99
  Moment.locale(`nl-BE`)
100
101
  useEffect(() => {
102
    async function getData() {
103
      const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`)
104
      setData(response.data)
105
    }
106
    getData()
107
  }, [])
108
109
  return (
110
    <div className={`matches__wrapper`}>
111
      {data
112
        .sort((a, b) => a.timestamp - b.timestamp)
113
        .map((match, i) => (
114
          <MatchesRow match={match} key={i} />
115
        ))}
116
    </div>
117
  )
118
}
119
120
export default Matches
121