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