Passed
Push — feature/game-reports ( 812aeb...4b0bd0 )
by Kevin Van
04:20
created

GamePage.renderEventLine   B

Complexity

Conditions 5

Size

Total Lines 70
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 70
rs 8.006
c 0
b 0
f 0
cc 5

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
3
import { graphql } from "gatsby"
4
5
import Layout from "../layouts/index"
6
import SEO from "../components/seo"
7
import Icon from "../components/icon"
8
9
import iconBench from "../images/i_bench.png"
10
import iconCardRed from "../images/i_card_red.png"
11
import iconCardYellowRed from "../images/i_card_yellow_red.png"
12
import iconCardYellow from "../images/i_card_yellow.png"
13
import iconGoal from "../images/i_goal.png"
14
import iconStart from "../images/i_start.png"
15
import iconSubIn from "../images/i_sub_in.png"
16
import iconSubOut from "../images/i_sub_out.png"
17
18
class GamePage extends Component {
19
  constructor(props) {
20
    super(props)
21
22
    this.state = {
23
      data: [],
24
      loading: true,
25
    }
26
27
    const {
28
      data: {
29
        site: {
30
          siteMetadata: { kcvvPsdApi, refreshRate },
31
        },
32
      },
33
    } = this.props
34
35
    this.kcvvPsdApi = kcvvPsdApi
36
    this.apiRefreshRate = refreshRate
37
    this.timeout = {}
38
    this.matchId = this.props.id || null
39
  }
40
41
  updateData() {
42
    const apiUrl = `${this.kcvvPsdApi}/match/${this.matchId}`
43
44
    fetch(apiUrl)
45
      .then((response) => response.json())
46
      .then((json) => this.setState({ data: json, loading: false }))
47
  }
48
49
  componentDidMount() {
50
    this.updateData()
51
  }
52
53
  render() {
54
    if (this.state.loading === false && this.state.data) {
55
      const { general, substitutes, lineup, events } = this.state.data
56
      const homeTeamId = general.homeClub.id
57
      const awayTeamId = general.awayClub.id
58
      const ogImage = {
59
        src: general?.homeClub.logo,
60
        width: 180,
61
        height: 180,
62
      }
63
64
      return (
65
        <Layout>
66
          <SEO
67
            lang="nl-BE"
68
            title={`Matchverslag ${general?.homeClub?.name} - ${general?.awayClub?.name}`}
69
            description={`Matchverslag ${general?.homeClub?.name} - ${general?.awayClub?.name}`}
70
            path={`/game/${general?.id}`}
71
            image={ogImage}
72
          />
73
          <section className="grid-container site-content">
74
            <div className="grid-x grid-margin-x">
75
              <div className={"cell large-12 center"}>
76
                {general.date}
77
                <br />
78
                {general.status}
79
                <br />
80
                {general.homeClub.name}
81
                <img src={general.homeClub.logo} alt={general.homeClub.name} />
82
                {general.goalsHomeTeam} - {general.goalsAwayTeam}
83
                {general.awayClub.name}
84
                <img src={general.awayClub.logo} alt={general.awayClub.name} />
85
                <br />
86
                {general.competitionType}
87
              </div>
88
89
              <div className={"lineup__wrapper grid-x grid-margin-x cell large-12"}>
90
                <div className={"cell large-6 lineup__wrapper--home"}>
91
                  <h3>{general.homeClub.name}</h3>
92
                  {this.renderLineup(lineup.home, substitutes.home)}
93
                </div>
94
                <div className={"cell large-6 lineup__wrapper--away"}>
95
                  <h3>{general.awayClub.name}</h3>
96
                  {this.renderLineup(lineup.away, substitutes.away)}
97
                </div>
98
              </div>
99
100
              <div className={"cell large-12 event__wrapper"}>
101
                <h3>Events</h3>
102
                {this.renderEvents(events, homeTeamId)}
103
              </div>
104
            </div>
105
          </section>
106
        </Layout>
107
      )
108
    } else {
109
      return (
110
        <Layout>
111
          <section className="grid-container site-content">
112
            Matchverslag inladen...
113
          </section>
114
        </Layout>
115
      )
116
    }
117
  }
118
119
  renderEvents(events, homeTeamId) {
120
    return (
121
      <>
122
        {events.map((element, i) =>
123
          this.renderEventLine(i, element, homeTeamId)
124
        )}
125
      </>
126
    )
127
  }
128
129
  renderEventLine(i, element, homeTeamId) {
130
    const homeTeam = element.clubId == homeTeamId
131
    let actionIcon = null
132
    let actionText = ""
133
134
    switch (element.action) {
135
      case "geel":
136
        actionIcon = iconCardYellow
137
        actionText = "Gele kaart voor"
138
        break
139
      case "rood":
140
        actionIcon = iconCardRed
141
        actionText = "Rode kaart voor"
142
        break
143
      case "doelpunt":
144
        actionIcon = iconGoal
145
        actionText = `${element?.goalsHome} - ${element?.goalsAway} — Doelpunt gescoord door`
146
        break
147
    }
148
149
    return (
150
      <div
151
        className={`event__row ${
152
          homeTeam ? "event__row--home" : "event__row--away"
153
        } grid-x grid-margin-x`}
154
        key={i}
155
      >
156
        {homeTeam && (
157
          <span
158
            className={
159
              "event__row__item event__row__item--home lineup__item--name cell small-10 large-4"
160
            }
161
          >
162
            {actionText} {element.playerName}
163
          </span>
164
        )}
165
        {homeTeam && (
166
          <span
167
            className={
168
              "event__row__item event__row__item--home lineup__item--action cell small-1 center"
169
            }
170
            style={{ backgroundImage: `url(${actionIcon})` }}
171
          ></span>
172
        )}
173
        <span
174
          className={
175
            "event__row__item lineup__item--time cell small-1 large-2 center"
176
          }
177
        >
178
          {element.minute}'
179
        </span>
180
        {homeTeam || (
181
          <span
182
            className={
183
              "event__row__item event__row__item--away lineup__item--action cell small-1 center"
184
            }
185
            style={{ backgroundImage: `url(${actionIcon})` }}
186
          ></span>
187
        )}
188
        {homeTeam || (
189
          <span
190
            className={
191
              "event__row__item event__row__item--away lineup__item--name cell small-10 large-4"
192
            }
193
          >
194
            {actionText} {element.playerName}
195
          </span>
196
        )}
197
      </div>
198
    )
199
  }
200
  renderLineup(lineup, substitutes) {
201
    return (
202
      <>
203
        {this.renderLineupHeader()}
204
        {lineup.map((element, i) => this.renderLineupLine(i, element))}
205
        <hr />
206
        {substitutes.map((element, i) => this.renderSubLine(i, element))}
207
      </>
208
    )
209
  }
210
211
  renderLineupHeader() {
212
    return (
213
      <div className={"lineup__header grid-x grid-margin-x"}>
214
        <span
215
          className={"lineup__header__item lineup__item--status cell small-1"}
216
        ></span>
217
        <span
218
          className={"lineup__header__item lineup__item--number cell small-1"}
219
        >
220
          #
221
        </span>
222
        <span
223
          className={"lineup__header__item lineup__item--name cell small-9"}
224
        >
225
          Name
226
        </span>
227
        <span
228
          className={"lineup__header__item lineup__item--time cell small-1"}
229
        >
230
          <Icon icon="fa-clock-o" />
231
        </span>
232
      </div>
233
    )
234
  }
235
236
  renderSubLine(i, element) {
237
    return (
238
      <div
239
        className={"lineup__row lineup__row--substitute grid-x grid-margin-x"}
240
        key={i}
241
      >
242
        <span
243
          className={"lineup__row__item lineup__item--status cell small-1"}
244
          style={{
245
            backgroundImage: `url(${element.changed ? iconSubIn : iconBench})`,
246
          }}
247
        ></span>
248
        <span className={"lineup__row__item lineup__item--number cell small-1"}>
249
          {element.number}
250
        </span>
251
        <span className={"lineup__row__item lineup__item--name cell small-9"}>
252
          {element.playerName}
253
        </span>
254
        <span className={"lineup__row__item lineup__item--time cell small-1"}>
255
          {element.minutesPlayed}'
256
        </span>
257
      </div>
258
    )
259
  }
260
261
  renderLineupLine(i, element) {
262
    return (
263
      <div
264
        className={"lineup__row lineup__row--lineup grid-x grid-margin-x"}
265
        key={i}
266
      >
267
        <span
268
          className={"lineup__row__item lineup__item--status cell small-1"}
269
          style={{
270
            backgroundImage: `url(${element.changed ? iconSubOut : iconStart})`,
271
          }}
272
        ></span>
273
        <span className={"lineup__row__item lineup__item--number cell small-1"}>
274
          {element.number}
275
        </span>
276
        <span className={"lineup__row__item lineup__item--name cell small-9"}>
277
          {element.playerName} {element.captain && `(C)`}
278
        </span>
279
        <span className={"lineup__row__item lineup__item--time cell small-1"}>
280
          {element.minutesPlayed}'
281
        </span>
282
      </div>
283
    )
284
  }
285
}
286
287
export const pageQuery = graphql`
288
  query {
289
    site {
290
      siteMetadata {
291
        siteUrl
292
        kcvvPsdApi
293
        refreshRate
294
      }
295
    }
296
  }
297
`
298
299
export default GamePage
300