Passed
Pull Request — develop (#758)
by Kevin Van
08:53 queued 04:59
created

Matches.tsx ➔ getData   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 axios from "axios"
2
import moment from "moment"
3
import "moment-timezone"
4
import "moment/locale/nl-be"
5
import React from "react"
6
import { useEffect, useState } from "react"
7
import { useSiteMetaData } from "../hooks/use-site-metadata"
8
import { Match, MatchesProps, MatchesRowProps } from "../Types/Match"
9
import { Spinner } from "./Spinner"
10
import "./Matches.scss"
11
import classnames from "classnames"
12
import LazyLoad from "react-lazyload"
13
import { mapPsdStatus, mapPsdStatusShort } from "../scripts/helper"
14
import { Link } from "gatsby"
15
import Icon from "./Icon"
16
17
const Matches = ({ teamId }: MatchesProps) => {
18
  const { kcvvPsdApi } = useSiteMetaData()
19
  const [data, setData] = useState<Match[]>([])
20
21
  useEffect(() => {
22
    async function getData() {
23
      const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`)
24
      setData(response.data)
25
    }
26
    getData()
27
  }, [kcvvPsdApi, teamId])
28
29
  return (
30
    <div className="matches__wrapper">
31
      {data.length > 0 || <Spinner />}
32
      {data
33
        .sort((a, b) => a.timestamp - b.timestamp)
34
        .map((match, i) => (
35
          <MatchesRow match={match} key={i} />
36
        ))}
37
    </div>
38
  )
39
}
40
41
const MatchesRow = ({ match }: MatchesRowProps) => {
42
  moment.tz.setDefault(`Europe/Brussels`)
43
  moment.locale(`nl-be`)
44
  moment.localeData(`nl-be`)
45
46
  const d = moment(match.date)
47
  const date = d.format(`ddd D MMM`)
48
  const time = d.format(`HH:mm`)
49
  const matchPlayed =
50
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
51
    false
52
53
  return (
54
    <div className="matches__row">
55
      <article className="matches__match">
56
        <div className="matches__competition__wrapper">
57
          <span className="matches__competition__type">{match.competitionType}</span>
58
        </div>
59
        <div className="matches__date__wrapper">
60
          <span className="matches__date__date">{date}</span>
61
          <span className="matches__date__time">{time}</span>
62
        </div>
63
        <div
64
          className={classnames(`matches__team__wrapper`, `matches__team__wrapper--home`, {
65
            "matches__team__wrapper--elewijt": match.homeTeamId,
66
          })}
67
        >
68
          <div className="matches__team__name">
69
            {match.homeClub?.abbreviation || match.homeClub?.name}
70
            {` `}
71
            <span className="matches__team__home_indicator" title="Uitwedstrijd">
72
              (U)
73
            </span>
74
          </div>
75
          <div className="matches__team__logo">
76
            <LazyLoad debounce={false}>
77
              <img
78
                src={match.homeClub?.logo}
79
                alt={match.homeClub?.abbreviation}
80
                className="match__teaser__logo match__teaser__logo--home"
81
              />
82
            </LazyLoad>
83
          </div>
84
        </div>
85
        <div className="matches__score__wrapper">
86
          {match.status !== 0 && (
87
            <span className="matches__score matches__score--status" title={mapPsdStatus(match.status) || ``}>
88
              {mapPsdStatusShort(match.status)}
89
            </span>
90
          )}
91
          {(match.status === 0 || match.status === null) && !matchPlayed && (
92
            <span className="matches__score matches__score--vs">VS</span>
93
          )}
94
          {matchPlayed && (
95
            <>
96
              <span className="matches__score matches__score--score">
97
                <span>{match.goalsHomeTeam}</span> <span>-</span> <span>{match.goalsAwayTeam}</span>
98
                {/* {match.goalsHomeTeam} - {match.goalsAwayTeam} */}
99
              </span>
100
              {/* className={classnames(`matches__score_result`, {
101
                  "matches__score_result--draw": match.goalsHomeTeam === match.goalsAwayTeam,
102
                  "matches__score_result--loss":
103
                    sideTeam === `home`
104
                      ? match.goalsAwayTeam > match.goalsHomeTeam
105
                      : match.goalsHomeTeam > match.goalsAwayTeam,
106
                  "matches__score_result--win":
107
                    sideTeam === `home`
108
                      ? match.goalsHomeTeam > match.goalsAwayTeam
109
                      : match.goalsAwayTeam > match.goalsHomeTeam,
110
                })}
111
              > */}
112
              {/* {(sideTeam === `home`
113
                ? match.goalsAwayTeam > match.goalsHomeTeam
114
                : match.goalsHomeTeam > match.goalsAwayTeam) && (
115
                <span className="matches__score_result matches__score_result--loss">Verlies</span>
116
              )}
117
              {(sideTeam === `home`
118
                ? match.goalsHomeTeam > match.goalsAwayTeam
119
                : match.goalsAwayTeam > match.goalsHomeTeam) && (
120
                <span className="matches__score_result matches__score_result--win">Winst</span>
121
              )}
122
              {match.goalsAwayTeam === match.goalsHomeTeam && (
123
                <span className="matches__score_result matches__score_result--draw">Gelijk</span>
124
              )} */}
125
              {/* </span> */}
126
            </>
127
          )}
128
        </div>
129
        <div
130
          className={classnames(`matches__team__wrapper`, `matches__team__wrapper--away`, {
131
            "matches__team__wrapper--elewijt": match.awayTeamId,
132
          })}
133
        >
134
          <div className="matches__team__name">
135
            {match.awayClub?.abbreviation || match.awayClub?.name}
136
            {` `}
137
            <span className="matches__team__home_indicator" title="Thuiswedstrijd">
138
              (T)
139
            </span>
140
          </div>
141
          <div className="matches__team__logo">
142
            <LazyLoad debounce={false}>
143
              <img
144
                src={match.awayClub?.logo}
145
                alt={match.awayClub?.abbreviation}
146
                className="match__teaser__logo match__teaser__logo--away"
147
              />
148
            </LazyLoad>
149
          </div>
150
        </div>
151
        <div className="matches__info__wrapper">
152
          <Link to={`/game/${match.id}`} className="matches__calendar__link">
153
            <Icon icon="fa-info-circle" /> Verslag
154
          </Link>
155
        </div>
156
      </article>
157
      {/* <header className="matches__calendar__title">
158
        <h3>
159
          <span className="matches__calendar__date">{date}</span>
160
          <span className="matches__calendar__separator">|</span>
161
          <span className="matches__calendar__type">{match.competitionType}</span>
162
        </h3>
163
      </header> */}
164
      {/* <main className="matches__calendar__main">
165
        <div
166
          className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, {
167
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
168
          })}
169
        >
170
          {match.homeClub?.abbreviation || match.homeClub?.name}
171
          <LazyLoad debounce={false}>
172
            <img
173
              src={match.homeClub?.logo}
174
              alt={match.homeClub?.name}
175
              className="matches__calendar__logo matches__calendar__logo--home"
176
            />
177
          </LazyLoad>
178
        </div>
179
180
        <div className="matches__calendar__score">
181
          {match.status !== 0 && (
182
            <span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span>
183
          )}
184
          {(match.status === 0 || match.status === null) && !matchPlayed && <span>{time}</span>}
185
          {matchPlayed && (
186
            <span>
187
              {match.goalsHomeTeam} - {match.goalsAwayTeam}
188
            </span>
189
          )}
190
        </div>
191
192
        <div
193
          className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, {
194
            "matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
195
          })}
196
        >
197
          <LazyLoad debounce={false}>
198
            <img
199
              src={match.awayClub?.logo}
200
              alt={match.awayClub?.name}
201
              className="matches__calendar__logo matches__calendar__logo--away"
202
            />
203
          </LazyLoad>
204
205
          {match.awayClub?.abbreviation || match.awayClub?.name}
206
        </div>
207
208
        <Link to={`/game/${match.id}`} className="matches__calendar__link">
209
          <Icon icon="fa-info-circle" />
210
        </Link>
211
      </main> */}
212
    </div>
213
  )
214
}
215
216
export default Matches
217