Passed
Push — release/20222023-design-rework ( 2d9f50 )
by Kevin Van
09:40 queued 03:39
created

src/components/ScheurkalenderMatches.tsx   A

Complexity

Total Complexity 9
Complexity/F 4.5

Size

Lines of Code 150
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 122
mnd 7
bc 7
fnc 2
dl 0
loc 150
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 moment from "moment"
10
import "moment-timezone"
11
import "moment/locale/nl-be"
12
import React from "react"
13
import { useEffect, useState } from "react"
14
15
export const ScheurkalenderMatches = () => {
16
  const [dataA, setDataA] = useState<Match[]>([])
17
  const [dataB, setDataB] = useState<Match[]>([])
18
19
  const { kcvvPsdApi } = useSiteMetaData()
20
21
  useEffect(() => {
22
    async function getDataA() {
23
      const response = await request.get(`${kcvvPsdApi}/matches/1`)
24
      setDataA(response.data)
25
    }
26
    async function getDataB() {
27
      const response = await request.get(`${kcvvPsdApi}/matches/2`)
28
      setDataB(response.data)
29
    }
30
    getDataA()
31
    getDataB()
32
  }, [kcvvPsdApi])
33
34
  const data = dataA.concat(dataB)
35
36
  return (
37
    <div className={`scheurkalender__wrapper`}>
38
      {data.length > 0 || <Spinner />}
39
      {data
40
        .filter((match: Match) => match.competitionType === `Competitie`)
41
        .sort((a: Match, b: Match) => a.timestamp - b.timestamp)
42
        .map((match: Match) => (
43
          <MatchTeaserDetail match={match} key={match.id} />
44
        ))}
45
    </div>
46
  )
47
}
48
49
export const MatchTeaserDetail = ({ match }: MatchTeaserDetailProps) => {
50
  moment.tz.setDefault(`Europe/Brussels`)
51
  moment.locale(`nl-be`)
52
  moment.localeData(`nl-be`)
53
54
  const d = moment(match.date)
55
  const matchPlayed =
56
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
57
    false
58
59
  return (
60
    <article className="match__teaser">
61
      <header>
62
        <div className="match__teaser__series">
63
          {(match.homeTeamId === 1 || match.awayTeamId === 1) && `1e Provinciale Vl. Brabant`}
64
          {(match.homeTeamId === 2 || match.awayTeamId === 2) && `4e Provinciale C`}
65
        </div>
66
        {match.status !== 0 && (
67
          <div className="match__teaser__datetime__wrapper match__teaser__datetime__wrapper--status">
68
            <time
69
              className="match__teaser__datetime match__teaser__datetime--date"
70
              dateTime={d.format(`YYYY-MM-DD - H:mm`)}
71
            >
72
              {d.format(`dddd DD MMMM - H:mm`)}
73
            </time>
74
            <span className="match__teaser__datetime match__teaser__datetime--status">
75
              {mapPsdStatus(match.status)}
76
            </span>
77
          </div>
78
        )}
79
        {(match.status === 0 || match.status === null) && (
80
          <div className="match__teaser__datetime__wrapper">
81
            <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
82
              {d.format(`dddd DD MMMM`)}
83
            </time>
84
            <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
85
              {d.format(`H:mm`)}
86
            </time>
87
          </div>
88
        )}
89
      </header>
90
      <main>
91
        <div
92
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
93
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
94
          })}
95
        >
96
          {match.homeTeamId === null && (
97
            <img
98
              src={match.homeClub?.logo}
99
              alt={match.homeClub?.name}
100
              width={300}
101
              className="match__teaser__logo match__teaser__logo--home"
102
            />
103
          )}
104
          {(match.homeTeamId === 1 || match.homeTeamId === 2) && (
105
            <StaticImage src="../images/logo-flat.png" alt="KCVV ELEWIJT" width={300} />
106
          )}
107
          {match.homeClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
108
        </div>
109
110
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
111
        {matchPlayed && (
112
          <div className="match__teaser__vs match__teaser__vs--score">
113
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
114
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
115
          </div>
116
        )}
117
118
        <div
119
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
120
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
121
          })}
122
        >
123
          <div>
124
            {match.awayClub?.name.replace(`Kcvv`, `KCVV`).replace(`K c v v`, `KCVV`)}
125
            {` `}
126
            {match.awayTeamId === null || (match.awayTeamId === 1 ? `A` : `B`)}
127
          </div>
128
          {match.awayTeamId === null && (
129
            <img
130
              src={match.awayClub?.logo}
131
              alt={match.awayClub?.name}
132
              className="match__teaser__logo match__teaser__logo--away"
133
            />
134
          )}
135
          {(match.awayTeamId === 1 || match.awayTeamId === 2) && (
136
            <StaticImage src="../images/logo-flat.png" alt="KCVV ELEWIJT" width={300} />
137
          )}
138
        </div>
139
      </main>
140
      {/* {includeRankings && match.competitionType === `Competitie` && (
141
        <MiniRanking
142
          teamId={match.homeTeamId || match.awayTeamId}
143
          homeTeam={match.homeClub?.name}
144
          awayTeam={match.awayClub?.name}
145
        />
146
      )} */}
147
    </article>
148
  )
149
}
150