Passed
Push — feature/full-redesign ( d9aad6...eff5f0 )
by Kevin Van
04:19
created

MatchTeaser.tsx ➔ getData   D

Complexity

Conditions 12

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 4.8
c 0
b 0
f 0
cc 12

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