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

MatchTeaser.tsx ➔ getData   C

Complexity

Conditions 10

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Complexity   

Complexity

Complex classes like MatchTeaser.tsx ➔ getData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import axios from "axios"
2
import classNames from "classnames"
3
import { graphql, useStaticQuery } from "gatsby"
4
import moment from "moment"
5
import "moment-timezone"
6
import "moment/locale/nl-be"
7
import React, { useEffect, useState } from "react"
8
import LazyLoad from "react-lazyload"
9
10
import { Match, MatchesQueryData } from "../Types/Match"
11
import { MatchTeaserDetailProps, MatchTeaserProps } from "../Types/MatchTeaser"
12
import { mapPsdStatus } from "../scripts/helper"
13
import "./MatchTeaser.scss"
14
import { useSiteMetaData } from "../hooks/use-site-metadata"
15
16
export const MatchTeaserDetail = ({ match, includeRankings }: MatchTeaserDetailProps) => {
17
  moment.tz.setDefault(`Europe/Brussels`)
18
  moment.locale(`nl-be`)
19
  moment.localeData(`nl-be`)
20
21
  const d = moment(match.date)
22
  const matchPlayed =
23
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
24
    false
25
26
  return (
27
    <article className="match__teaser">
28
      <header>
29
        <h3>{match.teamName.replace(`Voetbal : `, ``)}</h3>
30
        {match.status !== 0 && (
31
          <div className="match__teaser__datetime__wrapper match__teaser__datetime__wrapper--status">
32
            <time
33
              className="match__teaser__datetime match__teaser__datetime--date"
34
              dateTime={d.format(`YYYY-MM-DD - H:mm`)}
35
            >
36
              {d.format(`dddd DD MMMM - H:mm`)}
37
            </time>
38
            <span className="match__teaser__datetime match__teaser__datetime--status">
39
              {mapPsdStatus(match.status)}
40
            </span>
41
          </div>
42
        )}
43
        {(match.status === 0 || match.status === null) && (
44
          <div className="match__teaser__datetime__wrapper">
45
            <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
46
              {d.format(`dddd DD MMMM`)}
47
            </time>
48
            {` `}-{` `}
49
            <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
50
              {d.format(`H:mm`)}
51
            </time>
52
          </div>
53
        )}
54
      </header>
55
      <main>
56
        <div
57
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
58
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
59
          })}
60
        >
61
          <LazyLoad debounce={false}>
62
            <img
63
              src={match.homeClub?.logo}
64
              alt={match.homeClub?.abbreviation}
65
              className="match__teaser__logo match__teaser__logo--home"
66
            />
67
          </LazyLoad>
68
          {match.homeClub?.abbreviation || match.homeClub?.name}
69
        </div>
70
71
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
72
        {matchPlayed && (
73
          <div className="match__teaser__vs match__teaser__vs--score">
74
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
75
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
76
          </div>
77
        )}
78
79
        <div
80
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
81
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
82
          })}
83
        >
84
          <LazyLoad debounce={false}>
85
            <img
86
              src={match.awayClub?.logo}
87
              alt={match.awayClub?.abbreviation}
88
              className="match__teaser__logo match__teaser__logo--away"
89
            />
90
          </LazyLoad>
91
          {match.awayClub?.abbreviation || match.awayClub?.name}
92
        </div>
93
      </main>
94
      {/* {includeRankings && match.competitionType === `Competitie` && (
95
        <MiniRanking
96
          teamId={match.homeTeamId || match.awayTeamId}
97
          homeTeam={match.homeClub?.name}
98
          awayTeam={match.awayClub?.name}
99
        />
100
      )} */}
101
    </article>
102
  )
103
}
104
105
export const MatchTeaser = ({ teamId, action, includeRankings }: MatchTeaserProps) => {
106
  if (action !== `prev` && action !== `next`) {
107
    throw new Error(`Invalid action provided`)
108
  }
109
110
  const [data, setData] = useState<Match[]>([])
111
112
  const { kcvvPsdApi } = useSiteMetaData()
113
114
  useEffect(() => {
115
    async function getData() {
116
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
117
        params: { include: teamId },
118
      })
119
      setData(response.data)
120
    }
121
    getData()
122
  }, [action, kcvvPsdApi, teamId])
123
124
  if (data.length > 0) {
125
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
126
  } else {
127
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
128
  }
129
}
130
131
MatchTeaser.defaultProps = { includeRankings: false }
132
MatchTeaserDetail.defaultProps = { includeRankings: false }
133