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

srcBU/components/MatchTeaser.tsx   A

Complexity

Total Complexity 13
Complexity/F 13

Size

Lines of Code 157
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 118
mnd 12
bc 12
fnc 1
dl 0
loc 157
rs 10
bpm 12
cpm 13
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
D MatchTeaser.tsx ➔ getData 0 5 12
1
import axios from "axios"
2
import classNames from "classnames"
3
import { graphql, useStaticQuery } from "gatsby"
4
import moment from "moment-timezone"
5
import "moment-timezone/node_modules/moment/locale/nl-be"
6
import React, { Fragment, FunctionComponent, useEffect, useState } from "react"
7
import LazyLoad from "react-lazyload"
8
9
import { mapPsdStatus } from "../scripts/helper"
10
import "./MatchTeaser.scss"
11
import MiniRanking from "./MiniRanking"
12
13
export const MatchTeaserDetail: FunctionComponent<MatchTeaserDetailProps> = ({
14
  match,
15
  includeRankings = false,
16
}: 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
        <div>
31
          {match.status !== 0 && (
32
            <Fragment>
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
            </Fragment>
43
          )}
44
          {(match.status === 0 || match.status === null) && (
45
            <Fragment>
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
              <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
50
                {d.format(`H:mm`)}
51
              </time>
52
            </Fragment>
53
          )}
54
        </div>
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: FunctionComponent<MatchTeaserProps> = ({
107
  teamId,
108
  action,
109
  includeRankings = false,
110
}: MatchTeaserProps) => {
111
  if (action !== `prev` && action !== `next`) {
112
    throw new Error(`Invalid action provided`)
113
  }
114
115
  const [data, setData] = useState<Match[]>([])
116
117
  const {
118
    site: {
119
      siteMetadata: { kcvvPsdApi },
120
    },
121
  }: MatchesQueryData = useStaticQuery(graphql`
122
    {
123
      site {
124
        siteMetadata {
125
          kcvvPsdApi
126
        }
127
      }
128
    }
129
  `)
130
131
  useEffect(() => {
132
    async function getData() {
133
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
134
        params: { include: teamId },
135
      })
136
      setData(response.data)
137
    }
138
    getData()
139
  }, [])
140
141
  if (data.length > 0) {
142
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
143
  } else {
144
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
145
  }
146
}
147
148
export const MatchTeasers: FunctionComponent<MatchTeasersProps> = ({
149
  teamId,
150
  includeRankings = false,
151
}: MatchTeasersProps) => (
152
  <div className="match__teasers">
153
    <MatchTeaser teamId={teamId} action="prev" includeRankings={includeRankings} />
154
    <MatchTeaser teamId={teamId} action="next" includeRankings={includeRankings} />
155
  </div>
156
)
157