Passed
Push — master ( 7003f8...9bda5a )
by Kevin Van
01:42 queued 11s
created

Ranking.tsx ➔ getData   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
1
import axios from "axios"
2
import { graphql, useStaticQuery } from "gatsby"
3
import React, { Fragment, FunctionComponent, useEffect, useState } from "react"
4
5
import { sortRankings } from "../scripts/helper"
6
import "./Ranking.scss"
7
import Spinner from "./Spinner"
8
9
const Ranking: FunctionComponent<RankingProps> = ({ teamId }: RankingProps) => {
10
  const [data, setData] = useState<RankingDataObject[]>([])
11
12
  const {
13
    site: {
14
      siteMetadata: { kcvvPsdApi },
15
    },
16
  }: RankingData = useStaticQuery(graphql`
17
    {
18
      site {
19
        siteMetadata {
20
          kcvvPsdApi
21
        }
22
      }
23
    }
24
  `)
25
26
  useEffect(() => {
27
    async function getData() {
28
      const response = await axios.get(`${kcvvPsdApi}/ranking/${teamId}`)
29
      setData(response.data)
30
    }
31
    getData()
32
  }, [kcvvPsdApi, teamId])
33
34
  return (
35
    <section className={`ranking__wrapper`}>
36
      {data.length > 0 || <Spinner />}
37
      {data && renderRankings(data)}
38
    </section>
39
  )
40
}
41
42
const renderRanking = (ranking: RankingDataObject, i: number): JSX.Element => {
43
  return (
44
    <div className={`ranking`} key={i}>
45
      <h4>{ranking.name.replace(`Voetbal : `, ``)}</h4>
46
      <table>
47
        <thead>
48
          <tr>
49
            <th className={`table__column__number`}>#</th>
50
            <th className={`table__column__string`}>Team</th>
51
            <th className={`table__column__number show-for-medium`}>P</th>
52
            <th className={`table__column__number`}>W</th>
53
            <th className={`table__column__number`}>D</th>
54
            <th className={`table__column__number`}>L</th>
55
            <th className={`table__column__number show-for-medium`}>G+</th>
56
            <th className={`table__column__number show-for-medium`}>G-</th>
57
            <th className={`table__column__number`}>+/-</th>
58
            <th className={`table__column__number`}>Pts</th>
59
          </tr>
60
        </thead>
61
        <tbody>
62
          {ranking.teams.sort(sortRankings).map((team: RankingDataTeamObject, j: number) => (
63
            <tr key={j}>
64
              <td className={`table__column__number`}>{team.rank || `-`}</td>
65
              <td
66
                className={`table__column__string ${
67
                  team.team?.club?.localName?.toLowerCase().includes(`elewijt`) ? `table__column--highlight` : ``
68
                }`}
69
              >
70
                {team.team?.club?.localName || ``}
71
              </td>
72
              <td className={`table__column__number show-for-medium`}>{team.matchesPlayed || `0`}</td>
73
              <td className={`table__column__number`}>{team.wins || `0`}</td>
74
              <td className={`table__column__number`}>{team.draws || `0`}</td>
75
              <td className={`table__column__number`}>{team.losses || `0`}</td>
76
              <td className={`table__column__number show-for-medium`}>{team.goalsScored || `0`}</td>
77
              <td className={`table__column__number show-for-medium`}>{team.goalsConceded || `0`}</td>
78
              <td className={`table__column__number`}>{team.goalsScored - team.goalsConceded || `0`}</td>
79
              <td className={`table__column__number`}>{team.points || `0`}</td>
80
            </tr>
81
          ))}
82
        </tbody>
83
      </table>
84
    </div>
85
  )
86
}
87
88
const renderRankings = (rankings: RankingDataObject[]): JSX.Element => {
89
  return (
90
    <Fragment>
91
      {rankings
92
        .filter((ranking: RankingDataObject) => ranking.teams.length > 0)
93
        .sort((a, b) => a.name.localeCompare(b.name))
94
        .map((ranking: RankingDataObject, i: number) => {
95
          return renderRanking(ranking, i)
96
        })}
97
    </Fragment>
98
  )
99
}
100
101
export default Ranking
102