Passed
Push — feature/full-redesign ( a86ca7...15f409 )
by Kevin Van
04:58 queued 17s
created

src/components/ScheurkalenderMatches.tsx   A

Complexity

Total Complexity 9
Complexity/F 4.5

Size

Lines of Code 164
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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