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

MiniRanking.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 "./MiniRanking.scss"
7
import Spinner from "./Spinner"
8
9
const MiniRanking: FunctionComponent<MiniRankingProps> = ({ teamId, homeTeam, awayTeam }: MiniRankingProps) => {
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, homeTeam, awayTeam)}
38
    </section>
39
  )
40
}
41
42
const renderRanking = (ranking: RankingDataObject, homeTeam: string, awayTeam: string, i: number): JSX.Element => (
43
  <div className={`ranking`} key={i}>
44
    <h4>{ranking.name.replace(`Voetbal : `, ``)}</h4>
45
    <table>
46
      <thead>
47
        <tr>
48
          <th className={`table__column__number`}>#</th>
49
          <th className={`table__column__string`}>Team</th>
50
          <th className={`table__column__number show-for-medium`}>P</th>
51
          <th className={`table__column__number`}>W</th>
52
          <th className={`table__column__number`}>D</th>
53
          <th className={`table__column__number`}>L</th>
54
          <th className={`table__column__number show-for-medium`}>G+</th>
55
          <th className={`table__column__number show-for-medium`}>G-</th>
56
          <th className={`table__column__number`}>+/-</th>
57
          <th className={`table__column__number`}>Pts</th>
58
        </tr>
59
      </thead>
60
      <tbody>
61
        {ranking.teams.sort(sortRankings).map(
62
          (team: RankingDataTeamObject, j: number) =>
63
            (team.team?.club?.localName.includes(homeTeam) || team.team?.club?.localName.includes(awayTeam)) && (
64
              <tr key={j}>
65
                <td className={`table__column__number`}>{team.rank || `-`}</td>
66
                <td
67
                  className={`table__column__string ${
68
                    team.team?.club?.localName?.toLowerCase().includes(`elewijt`) ? `table__column--highlight` : ``
69
                  }`}
70
                >
71
                  {team.team?.club?.localName || ``}
72
                </td>
73
                <td className={`table__column__number show-for-medium`}>{team.matchesPlayed || `0`}</td>
74
                <td className={`table__column__number`}>{team.wins || `0`}</td>
75
                <td className={`table__column__number`}>{team.draws || `0`}</td>
76
                <td className={`table__column__number`}>{team.losses || `0`}</td>
77
                <td className={`table__column__number show-for-medium`}>{team.goalsScored || `0`}</td>
78
                <td className={`table__column__number show-for-medium`}>{team.goalsConceded || `0`}</td>
79
                <td className={`table__column__number`}>{team.goalsScored - team.goalsConceded || `0`}</td>
80
                <td className={`table__column__number`}>{team.points || `0`}</td>
81
              </tr>
82
            )
83
        )}
84
      </tbody>
85
    </table>
86
  </div>
87
)
88
89
const renderRankings = (rankings: RankingDataObject[], homeTeam: string, awayTeam: string): JSX.Element => (
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) => i === 0 && renderRanking(ranking, homeTeam, awayTeam, i))}
95
  </Fragment>
96
)
97
98
export default MiniRanking
99