Passed
Push — feature/full-redesign ( 7ff54a...588bf5 )
by Kevin Van
03:53
created

src/components/MatchesOverview.tsx   A

Complexity

Total Complexity 10
Complexity/F 10

Size

Lines of Code 108
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 77
mnd 9
bc 9
fnc 1
dl 0
loc 108
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 { useStaticQuery, graphql } from "gatsby"
3
import moment from "moment"
4
import "moment-timezone"
5
import "moment/locale/nl-be"
6
import React from "react"
7
import { useEffect, useState } from "react"
8
9
import { Match } from "../Types/Match"
10
import { MatchesOverviewProps, MatchesOverviewQueryData } from "../Types/MatchesOverview"
11
import { mapPsdStatus } from "../scripts/helper"
12
import { MatchTeaserDetail } from "./MatchTeaser"
13
14
export const MatchesOverview = ({
15
  include = [],
16
  exclude = [],
17
  action = `next`,
18
  details = false,
19
}: MatchesOverviewProps) => {
20
  if (action !== `prev` && action !== `next`) {
21
    throw new Error(`Invalid action provided`)
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
  const [loading, setLoading] = useState<boolean>(true)
29
30
  const {
31
    site: {
32
      siteMetadata: { kcvvPsdApi },
33
    },
34
  }: MatchesOverviewQueryData = useStaticQuery(graphql`
35
    {
36
      site {
37
        siteMetadata {
38
          kcvvPsdApi
39
        }
40
      }
41
    }
42
  `)
43
44
  useEffect(() => {
45
    async function getData() {
46
      let params = {}
47
48
      if (include.length > 0) {
49
        params = { include: include.join() }
50
      }
51
      if (exclude.length > 0) {
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
      setLoading(false)
60
    }
61
    getData()
62
  }, [action, exclude, include, kcvvPsdApi])
63
64
  moment.tz.setDefault(`Europe/Brussels`)
65
  moment.locale(`nl-be`)
66
  moment.localeData(`nl-be`)
67
68
  return (
69
    <div className="matches_overview__wrapper">
70
      {/* {data.length > 0 || loading === false || <Spinner />} */}
71
      {data.length <= 0 && loading === false && <div>Er staan voorlopig geen wedstrijden gepland</div>}
72
      {data.length > 0 && (
73
        <div>
74
          {data
75
            .sort((a, b) => a.timestamp - b.timestamp)
76
            .map((match: Match, i) => {
77
              if (details) {
78
                return <MatchTeaserDetail match={match} key={i} />
79
              } else {
80
                const matchTime = moment(match.date)
81
                return (
82
                  <div className="matches_overview__item" key={i}>
83
                    <span className={`label`}>{match.teamName.replace(`Voetbal : `, ``)}</span>
84
                    <span className={`matches_overview__date`}>{matchTime.format(`ddd D MMMM - H:mm`)}</span>
85
                    {+match.status !== 0 && (
86
                      <span className={`label alert matches_overview__status`}>{mapPsdStatus(match.status)}</span>
87
                    )}
88
                    <h6>
89
                      {match.homeClub?.abbreviation || match.homeClub?.name} -{` `}
90
                      {match.awayClub?.abbreviation || match.awayClub?.name}
91
                    </h6>
92
                  </div>
93
                )
94
              }
95
            })}
96
        </div>
97
      )}
98
    </div>
99
  )
100
}
101
102
MatchesOverview.defaultProps = {
103
  include: [],
104
  exclude: [],
105
  action: `next`,
106
  details: false,
107
}
108