src/components/MatchesOverview.tsx   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 11

Size

Lines of Code 93
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 74
mnd 10
bc 10
fnc 1
dl 0
loc 93
rs 10
bpm 10
cpm 11
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A MatchesOverview.tsx ➔ getData 0 15 5
1
import { Match } from "../Types/Match"
2
import { MatchesOverviewProps } from "../Types/MatchesOverview"
3
import { useSiteMetaData } from "../hooks/use-site-metadata"
4
import { mapPsdStatus, request } from "../scripts/helper"
5
import { MatchTeaserDetail } from "./MatchTeaser"
6
import "./MatchesOverview.scss"
7
import { Spinner } from "./Spinner"
8
import { DateTime, Settings } from "luxon"
9
import React from "react"
10
import { useEffect, useState } from "react"
11
12
export const MatchesOverview = ({
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
  if (include.length > 0 && exclude.length > 0) {
22
    throw new Error(`Can't include and exclude teams at same moment`)
23
  }
24
25
  const [data, setData] = useState<Match[]>([])
26
  const [loading, setLoading] = useState<boolean>(true)
27
28
  const { kcvvPsdApi } = useSiteMetaData()
29
30
  useEffect(() => {
31
    async function getData() {
32
      let params = {}
33
34
      if (include.length > 0) {
35
        params = { include: include.join() }
36
      }
37
      if (exclude.length > 0) {
38
        params = { exclude: exclude.join() }
39
      }
40
41
      const response = await request.get(`${kcvvPsdApi}/matches/${action}`, {
42
        params: params,
43
      })
44
      setData(response.data)
45
      setLoading(false)
46
    }
47
    getData()
48
  }, [action, exclude, include, kcvvPsdApi])
49
50
  Settings.defaultZone = `Europe/Brussels`
51
  Settings.defaultLocale = `nl-be`
52
53
  return (
54
    <div className="matches_overview__wrapper">
55
      {data.length > 0 || !loading ? null : <Spinner />}
56
      {!data.length && !loading && <div>Er staan voorlopig geen wedstrijden gepland</div>}
57
      {data.length > 0 && (
58
        <div>
59
          {data
60
            .sort((a, b) => a.timestamp - b.timestamp)
61
            .map((match: Match, i) => {
62
              if (details) {
63
                return <MatchTeaserDetail match={match} key={i} />
64
              } else {
65
                const matchTime = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`)
66
                return (
67
                  <div className="matches_overview__item" key={i}>
68
                    <span className={`label`}>{match.teamName.replace(`Voetbal : `, ``)}</span>
69
                    <span className={`matches_overview__date`}>{matchTime.toFormat(`ccc d MMMM - HH:mm`)}</span>
70
                    {+match.status !== 0 && (
71
                      <span className={`label alert matches_overview__status`}>{mapPsdStatus(match.status)}</span>
72
                    )}
73
                    <h6>
74
                      {match.homeClub?.abbreviation || match.homeClub?.name} -{` `}
75
                      {match.awayClub?.abbreviation || match.awayClub?.name}
76
                    </h6>
77
                  </div>
78
                )
79
              }
80
            })}
81
        </div>
82
      )}
83
    </div>
84
  )
85
}
86
87
MatchesOverview.defaultProps = {
88
  include: [],
89
  exclude: [],
90
  action: `next`,
91
  details: false,
92
}
93