src/components/ScheurkalenderMatches.tsx   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 4.5

Size

Lines of Code 159
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 128
mnd 7
bc 7
fnc 2
dl 0
loc 159
rs 10
bpm 3.5
cpm 4.5
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ScheurkalenderMatches.tsx ➔ getDataB 0 3 1
A ScheurkalenderMatches.tsx ➔ getDataA 0 3 1
1
import { Match } from "../Types/Match"
2
import { MatchTeaserDetailProps } from "../Types/MatchTeaser"
3
import { useSiteMetaData } from "../hooks/use-site-metadata"
4
import { mapPsdStatus, request } from "../scripts/helper"
5
import "./ScheurkalenderMatches.scss"
6
import { Spinner } from "./Spinner"
7
import classNames from "classnames"
8
import { StaticImage } from "gatsby-plugin-image"
9
import { DateTime, Settings } from "luxon"
10
import React from "react"
11
import { useEffect, useState } from "react"
12
13
const officialCompetitionTypes = [
14
  'OFFICIAL',
15
  'COMPETITION',
16
  'Competitie'
17
]
18
19
export const ScheurkalenderMatches = () => {
20
  const [dataA, setDataA] = useState<Match[]>([])
21
  const [dataB, setDataB] = useState<Match[]>([])
22
23
  const { kcvvPsdApi } = useSiteMetaData()
24
25
  useEffect(() => {
26
    async function getDataA() {
27
      const response = await request.get(`${kcvvPsdApi}/matches/1`)
28
      setDataA(response.data)
29
    }
30
    async function getDataB() {
31
      const response = await request.get(`${kcvvPsdApi}/matches/2`)
32
      setDataB(response.data)
33
    }
34
    getDataA()
35
    getDataB()
36
  }, [kcvvPsdApi])
37
38
  const data = dataA.concat(dataB)
39
40
  return (
41
    <div className={`scheurkalender__wrapper`}>
42
      {data.length > 0 || <Spinner />}
43
      {data
44
        .filter((match: Match) => officialCompetitionTypes.includes(match.competitionType))
45
        .sort((a: Match, b: Match) => a.timestamp - b.timestamp)
46
        .map((match: Match) => (
47
          <MatchTeaserDetail match={match} key={match.id} />
48
        ))}
49
    </div>
50
  )
51
}
52
53
export const MatchTeaserDetail = ({ match }: MatchTeaserDetailProps) => {
54
  Settings.defaultZone = `Europe/Brussels`
55
  Settings.defaultLocale = `nl-be`
56
57
  const dateTime = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`)
58
  const matchPlayed =
59
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
60
    false
61
62
  return (
63
    <article className="match__teaser">
64
      <header>
65
        <div className="match__teaser__series">
66
          {(match.homeTeamId === 1 || match.awayTeamId === 1) && `1e Provinciale Vl. Brabant`}
67
          {(match.homeTeamId === 2 || match.awayTeamId === 2) && `4e Provinciale C`}
68
        </div>
69
        {match.status !== 0 && (
70
          <div className="match__teaser__datetime__wrapper match__teaser__datetime__wrapper--status">
71
            <time
72
              className="match__teaser__datetime match__teaser__datetime--date"
73
              dateTime={dateTime.toFormat(`yyyy-MM-dd - H:mm`)}
74
            >
75
              {dateTime.toFormat(`EEEE dd LLLL - H:mm`)}
76
            </time>
77
            <span className="match__teaser__datetime match__teaser__datetime--status">
78
              {mapPsdStatus(match.status)}
79
            </span>
80
          </div>
81
        )}
82
        {(match.status === 0 || match.status === null) && (
83
          <div className="match__teaser__datetime__wrapper">
84
            <time
85
              className="match__teaser__datetime match__teaser__datetime--date"
86
              dateTime={dateTime.toFormat(`yyyy-MM-dd`)}
87
            >
88
              {dateTime.toFormat(`EEEE dd LLLL`)}
89
            </time>
90
            <time
91
              className="match__teaser__datetime match__teaser__datetime--time"
92
              dateTime={dateTime.toFormat(`H:mm`)}
93
            >
94
              {dateTime.toFormat(`H:mm`)}
95
            </time>
96
          </div>
97
        )}
98
      </header>
99
      <main>
100
        <div
101
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
102
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
103
          })}
104
        >
105
          {match.homeTeamId === null && (
106
            <img
107
              src={match.homeClub?.logo}
108
              alt={match.homeClub?.name}
109
              width={300}
110
              className="match__teaser__logo match__teaser__logo--home"
111
            />
112
          )}
113
          {(match.homeTeamId === 1 || match.homeTeamId === 2) && (
114
            <StaticImage src="../images/logo-flat.png" alt="KCVV ELEWIJT" width={300} />
115
          )}
116
          {match.homeClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
117
        </div>
118
119
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
120
        {matchPlayed && (
121
          <div className="match__teaser__vs match__teaser__vs--score">
122
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
123
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
124
          </div>
125
        )}
126
127
        <div
128
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
129
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
130
          })}
131
        >
132
          <div>
133
            {match.awayClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
134
            {` `}
135
            {match.awayTeamId === null || (match.awayTeamId === 1 ? `A` : `B`)}
136
          </div>
137
          {match.awayTeamId === null && (
138
            <img
139
              src={match.awayClub?.logo}
140
              alt={match.awayClub?.name}
141
              className="match__teaser__logo match__teaser__logo--away"
142
            />
143
          )}
144
          {(match.awayTeamId === 1 || match.awayTeamId === 2) && (
145
            <StaticImage src="../images/logo-flat.png" alt="KCVV ELEWIJT" width={300} />
146
          )}
147
        </div>
148
      </main>
149
      {/* {includeRankings && match.competitionType === `Competitie` && (
150
        <MiniRanking
151
          teamId={match.homeTeamId || match.awayTeamId}
152
          homeTeam={match.homeClub?.name}
153
          awayTeam={match.awayClub?.name}
154
        />
155
      )} */}
156
    </article>
157
  )
158
}
159