Passed
Push — feature/netlify-search ( 939dfd...fbe251 )
by Kevin Van
05:37
created

src/components/MatchesOverview.tsx   A

Complexity

Total Complexity 7
Complexity/F 7

Size

Lines of Code 97
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 67
mnd 6
bc 6
fnc 1
dl 0
loc 97
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 14 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
        params = { exclude: exclude.join() }
52
      }
53
54
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
55
        params: params,
56
      })
57
      setData(response.data)
58
    }
59
    getData()
60
  }, [])
61
62
  Moment.locale(`nl-be`)
63
64
  return (
65
    <div className="matches_overview__wrapper">
66
      {data.length > 0 || <Spinner />}
67
      <div>
68
        {data
69
          .sort((a, b) => a.timestamp - b.timestamp)
70
          .map((match: Match, i) => {
71
            if (details) {
72
              return <MatchTeaserDetail match={match} key={i} />
73
            } else {
74
              const matchTime = Moment.tz(match.date, `Europe/Brussels`)
75
              return (
76
                <div className="matches_overview__item" key={i}>
77
                  <span className={`label`}>{match.teamName.replace(`Voetbal : `, ``)}</span>
78
                  <span className={`matches_overview__date`}>{matchTime.format(`ddd D MMMM - H:mm`)}</span>
79
                  {match.status !== 0 ? (
80
                    <span className={`label alert matches_overview__status`}>{mapPsdStatus(match.status)}</span>
81
                  ) : (
82
                    ``
83
                  )}
84
                  <h6>
85
                    {match.homeClub.name} - {match.awayClub.name}
86
                  </h6>
87
                </div>
88
              )
89
            }
90
          })}
91
      </div>
92
    </div>
93
  )
94
}
95
96
export default MatchesOverview
97