Passed
Push — feature/player-stats ( 57cce1...aaa03b )
by Kevin Van
04:53
created

PlayerDetail.renderPlayerGamesFull   B

Complexity

Conditions 3

Size

Total Lines 93
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 93
c 0
b 0
f 0
rs 7.5563
cc 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import React, { Component } from "react"
2
import { graphql, StaticQuery } from "gatsby"
3
import { mapPositionCode } from "../scripts/helper"
4
5
import moment from "moment"
6
7
import "./player.scss"
8
import { Link } from "gatsby"
9
10
import Icon from "../components/icon"
11
12
import iconBench from "../images/i_bench_2.png"
13
import iconCardRed from "../images/i_card_red.png"
14
import iconCardYellowRed from "../images/i_card_yellow_red.png"
15
import iconCardYellow from "../images/i_card_yellow.png"
16
import iconGoal from "../images/i_goal.png"
17
import iconStart from "../images/i_start.png"
18
import iconSubIn from "../images/i_sub_in.png"
19
import iconSubOut from "../images/i_sub_out.png"
20
21
// eslint-disable-next-line
22
String.prototype.replaceAll = function (search, replacement) {
23
  var target = this
24
  return target.replace(new RegExp(search, "g"), replacement)
25
}
26
27
/**
28
 */
29
class PlayerDetail extends Component {
30
  constructor(props) {
31
    super(props)
32
33
    this.state = {
34
      data: [],
35
      loading: true,
36
    }
37
38
    const {
39
      config: {
40
        site: {
41
          siteMetadata: { kcvvPsdApi },
42
        },
43
      },
44
      player: { field_vv_id: playerId },
45
    } = this.props
46
47
    this.kcvvPsdApi = kcvvPsdApi
48
    this.playerId = playerId
49
  }
50
51
  updateData() {
52
    if (this.matchId === null) {
53
      return
54
    }
55
56
    const apiUrl = `${this.kcvvPsdApi}/stats/player/${this.playerId}`
57
58
    fetch(apiUrl)
59
      .then((response) => response.json())
60
      .then((json) => this.setState({ data: json, loading: false }))
61
  }
62
63
  componentDidMount() {
64
    this.updateData()
65
  }
66
67
  renderPlayerName = (player) => (
68
    <h1 className={"player-detail__name"}>
69
      <span className={"player-detail__name-first"}>
70
        {player.field_firstname}
71
      </span>
72
      <span className={"player-detail__name-last"}>
73
        {player.field_lastname}
74
      </span>
75
    </h1>
76
  )
77
  renderPlayerImage = (player) => (
78
    <div className={"bg-green-mask"}>
79
      <div
80
        className={"player-detail__bg-avatar"}
81
        style={
82
          player.relationships.field_image && {
83
            backgroundImage: `url(${player.relationships.field_image.localFile.childImageSharp.fixed.src})`,
84
          }
85
        }
86
      />
87
      <div className={"bg-white-end"} />
88
    </div>
89
  )
90
  renderPlayerHeader = (player) => (
91
    <header className={"player-detail__header"}>
92
      {this.renderPlayerName(player)}
93
      {this.renderPlayerImage(player)}
94
95
      <div className={"player-detail__bg-shirt-number"} aria-hidden="true">
96
        {player.field_shirtnumber || ""}
97
      </div>
98
    </header>
99
  )
100
  renderPlayerStats = (player) => {
101
    if (this.state.loading === false && this.state.data) {
102
      const {
103
        playerStatistics = [],
104
      } = this.state.data
105
106
      return (
107
        <aside className={"player-detail__statistics"}>
108
          <section className={"player-detail__statistics-item"}>
109
            <div className={"player-detail__statistics-item__number"}>
110
              {playerStatistics[0]?.gamesPlayed || "0"}
111
            </div>
112
            <div className={"player-detail__statistics-item__label"}>
113
              Wedstrijden
114
            </div>
115
          </section>
116
117
          {(player.field_position === "k" || player.field_position === "d") && (
118
            <section className={"player-detail__statistics-item"}>
119
              <div className={"player-detail__statistics-item__number"}>
120
                {playerStatistics[0]?.cleanSheets || "0"}
121
              </div>
122
              <div className={"player-detail__statistics-item__label"}>
123
                Cleansheets
124
              </div>
125
            </section>
126
          )}
127
          {player.field_position !== "k" && (
128
            <section className={"player-detail__statistics-item"}>
129
              <div className={"player-detail__statistics-item__number"}>
130
                {playerStatistics[0]?.goals || "0"}
131
              </div>
132
              <div className={"player-detail__statistics-item__label"}>
133
                Doelpunten
134
              </div>
135
            </section>
136
          )}
137
          <section className={"player-detail__statistics-item"}>
138
            <div className={"player-detail__statistics-item__number"}>
139
              {playerStatistics[0]?.yellowCards || "0"}
140
            </div>
141
            <div className={"player-detail__statistics-item__label"}>
142
              Gele kaarten
143
            </div>
144
          </section>
145
          <section className={"player-detail__statistics-item"}>
146
            <div className={"player-detail__statistics-item__number"}>
147
              {playerStatistics[0]?.redCards|| "0"}
148
            </div>
149
            <div className={"player-detail__statistics-item__label"}>
150
              Rode kaarten
151
            </div>
152
          </section>
153
        </aside>
154
      )
155
    }
156
  }
157
158
  renderPlayerStatsFull = (player) => {
159
    if (this.state.loading === false && this.state.data) {
160
      const {
161
        playerStatistics = [],
162
      } = this.state.data
163
164
      return (
165
        <article
166
          className={"player-detail__stats medium-12 large-12 cell card"}
167
        >
168
          <header className={"card__header"}>
169
            <h4>Statistieken</h4>
170
          </header>
171
          <div className={"card__content"}>
172
            <table className={"player-detail__stats__table"}>
173
              <thead>
174
                <tr>
175
                  <th>Team</th>
176
                  <th>
177
                    <span title="Wedstrijden gespeeld">P</span>
178
                  </th>
179
                  <th>
180
                    <span title="Wedstrijden gewonnen">W</span>
181
                  </th>
182
                  <th>
183
                    <span title="Wedstrijden gelijkgespeeld">D</span>
184
                  </th>
185
                  <th>
186
                    <span title="Wedstrijden verloren">L</span>
187
                  </th>
188
                  <th>
189
                    <img
190
                      src={iconCardYellow}
191
                      title="Gele kaart"
192
                      alt="Gele kaart"
193
                      className="player-detail__stats--header_icon"
194
                    />
195
                  </th>
196
                  <th>
197
                    <img
198
                      src={iconCardRed}
199
                      title="Rode kaart"
200
                      alt="Rode kaart"
201
                      className="player-detail__stats--header_icon"
202
                    />
203
                  </th>
204
                  <th>
205
                    <img
206
                      src={iconGoal}
207
                      title="Doelpunten gescoord"
208
                      alt="Rode kaart"
209
                      className="player-detail__stats--header_icon"
210
                    />
211
                  </th>
212
                  <th>
213
                    <span title="Cleansheets">C</span>
214
                  </th>
215
                  <th>
216
                    <span title="Minuten gespeeld">
217
                      <Icon icon="fa-clock-o" />
218
                    </span>
219
                  </th>
220
                </tr>
221
              </thead>
222
              <tbody>
223
                {playerStatistics.map(function (stats) {
224
                  return (
225
                    <tr>
226
                      <td>{stats.team.replace("Voetbal : ", "")}</td>
227
                      <td>{stats.gamesPlayed}</td>
228
                      <td>{stats.gamesWon}</td>
229
                      <td>{stats.gamesEqual}</td>
230
                      <td>{stats.gamesLost}</td>
231
                      <td>{stats.yellowCards}</td>
232
                      <td>{stats.redCards}</td>
233
                      <td>{stats.goals}</td>
234
                      <td>{stats.cleanSheets}</td>
235
                      <td>{stats.minutes}'</td>
236
                    </tr>
237
                  )
238
                })}
239
              </tbody>
240
            </table>
241
          </div>
242
        </article>
243
      )
244
    }
245
  }
246
  renderPlayerGamesFull = (player) => {
247
    if (this.state.loading === false && this.state.data) {
248
      const {
249
        gameReports = [],
250
      } = this.state.data
251
252
      return (
253
        <article
254
          className={"player-detail__games medium-12 large-12 cell card"}
255
        >
256
          <header className={"card__header"}>
257
            <h4>Wedstrijden</h4>
258
          </header>
259
          <div className={"card__content"}>
260
            <table className={"player-detail__games__table responsive-card-table"}>
261
              <thead>
262
                <tr>
263
                  <th>Team</th>
264
                  <th>Type</th>
265
                  <th>Datum</th>
266
                  <th><span title="Thuis/uit">H/A</span></th>
267
                  <th>Score</th>
268
                  <th>Tegenstander</th>
269
                  <th>
270
                    <img
271
                      src={iconCardYellow}
272
                      title="Gele kaart"
273
                      alt="Gele kaart"
274
                      className="player-detail__stats--header_icon"
275
                    />
276
                  </th>
277
                  <th>
278
                    <img
279
                      src={iconCardRed}
280
                      title="Rode kaart"
281
                      alt="Rode kaart"
282
                      className="player-detail__stats--header_icon"
283
                    />
284
                  </th>
285
                  <th>
286
                    <img
287
                      src={iconGoal}
288
                      title="Doelpunten gescoord"
289
                      alt="Rode kaart"
290
                      className="player-detail__stats--header_icon"
291
                    />
292
                  </th>
293
                  <th>
294
                    <span title="Minuten gespeeld">
295
                      <Icon icon="fa-clock-o" />
296
                    </span>
297
                  </th>
298
                </tr>
299
              </thead>
300
              <tbody>
301
                {gameReports.map(function (game) {
302
                  return (
303
                    <tr>
304
                      <td data-label="Team">{game.team.replace("Voetbal : ", "")}</td>
305
                      <td data-label="Type">{game.competition}</td>
306
                      <td data-label="Datum">{moment(game.date).format("DD/MM/YYYY")}</td>
307
                      <td data-label="Thuis/uit">
308
                        {game.home ? (
309
                          <span
310
                            className={"player-detail__games__home"}
311
                            title="Thuiswedstrijd"
312
                          >
313
                            H
314
                          </span>
315
                        ) : (
316
                          <span
317
                            className={"player-detail__games__away"}
318
                            title="Uitwedstrijd"
319
                          >
320
                            A
321
                          </span>
322
                        )}
323
                      </td>
324
                      <td data-label="Score">
325
                        {game.goalsHomeTeam}&nbsp;-&nbsp;{game.goalsAwayTeam}
326
                      </td>
327
                      <td data-label="Tegenstander">{game.opponent}</td>
328
                      <td data-label="Gele kaart(en)">{game.yellowCards}</td>
329
                      <td  data-label="Rode kaart(en)">{game.redCards}</td>
330
                      <td data-label="Doelpunten">{game.goals}</td>
331
                      <td data-label="Speeltijd">{game.minutesPlayed}'</td>
332
                    </tr>
333
                  )
334
                })}
335
              </tbody>
336
            </table>
337
          </div>
338
        </article>
339
      )
340
    }
341
  }
342
343
  renderPlayerBirthdate = (player) => (
344
    <div
345
      className={"player-detail__data-item player-detail__data-item--birthdate"}
346
    >
347
      <span className={"player-detail__data-item__label"}>Geboortedatum</span>
348
      <span className={"player-detail__data-item__data"}>
349
        {player.field_birth_date || "Onbekend"}
350
      </span>
351
    </div>
352
  )
353
  renderPlayerPosition = (player) => (
354
    <div
355
      className={"player-detail__data-item player-detail__data-item--position"}
356
    >
357
      <span className={"player-detail__date-item__data"}>
358
        {player.field_position && mapPositionCode(player.field_position)}
359
      </span>
360
      <span className={"player-detail__data-item__label"}>
361
        {player.relationships.node__team && (
362
          <Link to={player.relationships.node__team[0].path.alias}>
363
            {player.relationships.node__team[0].title}
364
          </Link>
365
        )}
366
      </span>
367
    </div>
368
  )
369
  renderPlayerJoinDate = (player) => {
370
    const currentlyPlaying = !player.field_date_leave
371
    return (
372
      <div
373
        className={
374
          "player-detail__data-item player-detail__data-item--joindate"
375
        }
376
      >
377
        <span className={"player-detail__data-item__label"}>
378
          {currentlyPlaying && "Speler bij KCVV sinds"}
379
          {!currentlyPlaying && "Speler tussen"}
380
        </span>
381
        <span className={"player-detail__data-item__data"}>
382
          {player.field_join_date || "Onbekend"}
383
          {!currentlyPlaying && (
384
            <>
385
              <span className={"text--regular"}> en </span>{" "}
386
              {player.field_date_leave}
387
            </>
388
          )}
389
        </span>
390
      </div>
391
    )
392
  }
393
  renderPlayerData = (player) => (
394
    <section className={"player-detail__data"}>
395
      {this.renderPlayerBirthdate(player)}
396
      {this.renderPlayerPosition(player)}
397
      {this.renderPlayerJoinDate(player)}
398
    </section>
399
  )
400
  renderPlayerBody = (player) => {
401
    const cleanBody =
402
      (player.body &&
403
        player.body.processed.replaceAll(
404
          "/sites/default/",
405
          `${process.env.GATSBY_API_DOMAIN}/sites/default/`
406
        )) ||
407
      ""
408
409
    return (
410
      <section className={"player-detail__body"}>
411
        <div dangerouslySetInnerHTML={{ __html: cleanBody }} />
412
      </section>
413
    )
414
  }
415
  render() {
416
    const { player } = this.props
417
418
    return (
419
      <article className={"player-detail"}>
420
        {this.renderPlayerHeader(player)}
421
        {this.renderPlayerStats(player)}
422
        <div className={"player-break"}></div>
423
        {this.renderPlayerData(player)}
424
        {this.renderPlayerBody(player)}
425
        {this.renderPlayerStatsFull(player)}
426
        {this.renderPlayerGamesFull(player)}
427
      </article>
428
    )
429
  }
430
}
431
432
// Retrieve endpoint of the logo's api from the site metadata (gatsby-config.js).
433
const query = graphql`
434
  query {
435
    site {
436
      siteMetadata {
437
        kcvvPsdApi
438
      }
439
    }
440
  }
441
`
442
443
export default ({ player }) => (
444
  <StaticQuery
445
    query={query}
446
    render={(data) => (
447
      <PlayerDetail
448
        // Data is the result of our query.
449
        config={data}
450
        player={player}
451
      />
452
    )}
453
  />
454
)
455