Passed
Push — release/2.3.0 ( 10d604 )
by Kevin Van
01:58 queued 10s
created

src/components/MatchesOverview.tsx   A

Complexity

Total Complexity 7
Complexity/F 7

Size

Lines of Code 98
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 68
mnd 6
bc 6
fnc 1
dl 0
loc 98
rs 10
bpm 6
cpm 7
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A MatchesOverview.tsx ➔ getData 0 15 5
1
import React, { Fragment, FunctionComponent, useEffect, useState } from "react"
2
import { graphql, useStaticQuery } from "gatsby"
3
4
import "./MatchesOverview.scss"
5
import Spinner from "./Spinner"
6
import axios from "axios"
7
import Moment from "moment-timezone"
8
import "moment/locale/nl-be"
9
10
import { mapPsdStatus } from "../scripts/helper"
11
import { MatchTeaserDetail } from "./MatchTeaser"
12
13
const MatchesOverview: FunctionComponent<MatchesOverviewProps> = ({
14
  include = [],
15
  exclude = [],
16
  action = `next`,
17
  details = false,
18
}: MatchesOverviewProps) => {
19
  if (action !== `prev` && action !== `next`) {
20
    throw new Error(`Invalid action provided`)
21
  }
22
23
  if (include.length > 0 && exclude.length > 0) {
24
    throw new Error(`Can't include and exclude teams at same moment`)
25
  }
26
27
  const [data, setData] = useState<Match[]>([])
28
29
  const {
30
    site: {
31
      siteMetadata: { kcvvPsdApi },
32
    },
33
  }: MatchesOverviewQueryData = useStaticQuery(graphql`
34
    {
35
      site {
36
        siteMetadata {
37
          kcvvPsdApi
38
        }
39
      }
40
    }
41
  `)
42
43
  useEffect(() => {
44
    async function getData() {
45
      let params = {}
46
47
      if (include.length > 0) {
48
        params = { include: include.join() }
49
      }
50
      if (exclude.length > 0) {
51
        console.log(exclude)
52
        params = { exclude: exclude.join() }
53
      }
54
55
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
56
        params: params,
57
      })
58
      setData(response.data)
59
    }
60
    getData()
61
  }, [])
62
63
  Moment.locale(`nl-be`)
64
65
  return (
66
    <div className="matches_overview__wrapper">
67
      {data.length > 0 || <Spinner />}
68
      <div>
69
        {data
70
          .sort((a, b) => a.timestamp - b.timestamp)
71
          .map((match: Match, i) => {
72
            if (details) {
73
              return <MatchTeaserDetail match={match} key={i} />
74
            } else {
75
              const matchTime = Moment.tz(match.date, `Europe/Brussels`)
76
              return (
77
                <div className="matches_overview__item" key={i}>
78
                  <span className={`label`}>{match.teamName.replace(`Voetbal : `, ``)}</span>
79
                  <span className={`matches_overview__date`}>{matchTime.format(`ddd D MMMM - H:mm`)}</span>
80
                  {match.status !== 0 ? (
81
                    <span className={`label alert matches_overview__status`}>{mapPsdStatus(match.status)}</span>
82
                  ) : (
83
                    ``
84
                  )}
85
                  <h6>
86
                    {match.homeClub.name} - {match.awayClub.name}
87
                  </h6>
88
                </div>
89
              )
90
            }
91
          })}
92
      </div>
93
    </div>
94
  )
95
}
96
97
export default MatchesOverview
98