Passed
Pull Request — develop (#758)
by Kevin Van
04:28
created

srcBU/components/MatchesOverview.tsx   A

Complexity

Total Complexity 10
Complexity/F 10

Size

Lines of Code 102
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A MatchesOverview.tsx ➔ getData 0 15 5
1
import axios from "axios"
2
import { graphql, useStaticQuery } from "gatsby"
3
import moment from "moment-timezone"
4
import "moment-timezone/node_modules/moment/locale/nl-be"
5
import React, { FunctionComponent, useEffect, useState } from "react"
6
7
import { mapPsdStatus } from "../scripts/helper"
8
import { MatchTeaserDetail } from "./MatchTeaser"
9
import "./MatchesOverview.scss"
10
import Spinner from "./Spinner"
11
12
const MatchesOverview: FunctionComponent<MatchesOverviewProps> = ({
13
  include = [],
14
  exclude = [],
15
  action = `next`,
16
  details = false,
17
}: MatchesOverviewProps) => {
18
  if (action !== `prev` && action !== `next`) {
19
    throw new Error(`Invalid action provided`)
20
  }
21
22
  if (include.length > 0 && exclude.length > 0) {
23
    throw new Error(`Can't include and exclude teams at same moment`)
24
  }
25
26
  const [data, setData] = useState<Match[]>([])
27
  const [loading, setLoading] = useState<boolean>(true)
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
        params = { exclude: exclude.join() }
52
      }
53
54
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
55
        params: params,
56
      })
57
      setData(response.data)
58
      setLoading(false)
59
    }
60
    getData()
61
  }, [])
62
63
  moment.tz.setDefault(`Europe/Brussels`)
64
  moment.locale(`nl-be`)
65
  moment.localeData(`nl-be`)
66
67
  return (
68
    <div className="matches_overview__wrapper">
69
      {data.length > 0 || loading === false || <Spinner />}
70
      {data.length <= 0 && loading === false && <div>Er staan voorlopig geen wedstrijden gepland</div>}
71
      {data.length > 0 && (
72
        <div>
73
          {data
74
            .sort((a, b) => a.timestamp - b.timestamp)
75
            .map((match: Match, i) => {
76
              if (details) {
77
                return <MatchTeaserDetail match={match} key={i} />
78
              } else {
79
                const matchTime = moment(match.date)
80
                return (
81
                  <div className="matches_overview__item" key={i}>
82
                    <span className={`label`}>{match.teamName.replace(`Voetbal : `, ``)}</span>
83
                    <span className={`matches_overview__date`}>{matchTime.format(`ddd D MMMM - H:mm`)}</span>
84
                    {+match.status !== 0 && (
85
                      <span className={`label alert matches_overview__status`}>{mapPsdStatus(match.status)}</span>
86
                    )}
87
                    <h6>
88
                      {match.homeClub?.abbreviation || match.homeClub?.name} -{` `}
89
                      {match.awayClub?.abbreviation || match.awayClub?.name}
90
                    </h6>
91
                  </div>
92
                )
93
              }
94
            })}
95
        </div>
96
      )}
97
    </div>
98
  )
99
}
100
101
export default MatchesOverview
102