Passed
Push — master ( 0c90fa...d64b6a )
by Kevin Van
01:43 queued 12s
created

MatchesScheurkalender.tsx ➔ getDataA   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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