Passed
Push — master ( 2ea05b...51ebb5 )
by Kevin Van
07:00
created

src/components/ClubcalendarMatches.tsx   A

Complexity

Total Complexity 12
Complexity/F 6

Size

Lines of Code 109
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 95
mnd 10
bc 10
fnc 2
dl 0
loc 109
rs 10
bpm 5
cpm 6
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ClubcalendarMatches.tsx ➔ getDataA 0 3 1
A ClubcalendarMatches.tsx ➔ getDataB 0 3 1
1
import { useEffect, useState } from "react"
2
import { Match } from "../Types/Match"
3
import { useSiteMetaData } from "../hooks/use-site-metadata"
4
import { request } from "../scripts/helper"
5
import React from "react"
6
import { Spinner } from "./Spinner"
7
import { DateTime, Settings } from "luxon"
8
import "./ClubcalendarMatches.scss"
9
10
export const ClubcalendarMatches = () => {
11
  Settings.defaultZone = `Europe/Brussels`
12
  Settings.defaultLocale = `nl-be`
13
14
  const [dataA, setDataA] = useState<Match[]>([])
15
  const [dataB, setDataB] = useState<Match[]>([])
16
17
  const { kcvvPsdApi } = useSiteMetaData()
18
19
  useEffect(() => {
20
    async function getDataA() {
21
      const response = await request.get(`${kcvvPsdApi}/matches/1`)
22
      setDataA(response.data)
23
    }
24
    async function getDataB() {
25
      const response = await request.get(`${kcvvPsdApi}/matches/2`)
26
      setDataB(response.data)
27
    }
28
    getDataA()
29
    getDataB()
30
  }, [kcvvPsdApi])
31
32
  const data = dataA.concat(dataB)
33
34
  const filteredAndSortedData = data
35
    .filter((match: Match) => match.competitionType === "Competitie")
36
    .sort((a: Match, b: Match) => {
37
      const dateA = DateTime.fromFormat(a.date, "yyyy-MM-dd HH:mm")
38
      const dateB = DateTime.fromFormat(b.date, "yyyy-MM-dd HH:mm")
39
      return dateA.toMillis() - dateB.toMillis()
40
    })
41
42
  return (
43
    <div className="clubcalendar__wrapper">
44
      {data.length === 0 && <Spinner />}
45
      <div className="clubcalendar__grid-header">
46
        <div>Datum</div>
47
        <div>Uur</div>
48
        <div>Thuisploeg</div>
49
        <div>Uitploeg</div>
50
      </div>
51
      <div className="clubcalendar__grid-body">
52
        {filteredAndSortedData.map((match, index) => {
53
          let addClass = false
54
          if (index > 0) {
55
            const prevMatch = filteredAndSortedData[index - 1]
56
            const currentDate = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`)
57
            const prevDate = DateTime.fromFormat(prevMatch.date, `yyyy-MM-dd HH:mm`)
58
            const diffHours = currentDate.diff(prevDate, "hours").hours
59
            addClass = diffHours >= 48
60
          }
61
62
          return (
63
            <ClubcalendarCard
64
              match={match}
65
              key={match.id}
66
              highlight={addClass}
67
            />
68
          )
69
        })}
70
      </div>
71
    </div>
72
  )
73
}
74
75
export const ClubcalendarCard = ({
76
  match,
77
  highlight,
78
}: {
79
  match: Match
80
  highlight?: boolean
81
}) => {
82
  const dateTime = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`)
83
84
  return (
85
    <div className={`clubcalendar__grid-row ${highlight ? "highlight-row" : ""}`}>
86
      <div className="clubcalendar__cell">
87
        <time dateTime={dateTime.toFormat(`yyyy-MM-dd`)}>
88
          {dateTime.toFormat(`EEE dd/MM/yy`)}
89
        </time>
90
      </div>
91
      <div className="clubcalendar__cell">
92
        <time dateTime={dateTime.toFormat(`H:mm`)}>
93
          {dateTime.toFormat(`H:mm`)}
94
        </time>
95
      </div>
96
      <div className={`clubcalendar__cell ${match.homeClub?.name.includes("Kcvv Elewijt") ? "highlight-club" : ""}`}>
97
        {match.homeClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
98
        {` `}
99
        {match.homeTeamId === null || (match.homeTeamId === 1 ? `A` : `B`)}
100
      </div>
101
      <div className={`clubcalendar__cell ${match.awayClub?.name.includes("Kcvv Elewijt") ? "highlight-club" : ""}`}>
102
        {match.awayClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
103
        {` `}
104
        {match.awayTeamId === null || (match.awayTeamId === 1 ? `A` : `B`)}
105
      </div>
106
    </div>
107
  )
108
}
109