src/components/MatchTeaser.tsx   A
last analyzed

Complexity

Total Complexity 13
Complexity/F 13

Size

Lines of Code 156
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 132
mnd 12
bc 12
fnc 1
dl 0
loc 156
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 { 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 { DateTime, Settings } from "luxon"
8
import React, { useEffect, useState } from "react"
9
import LazyLoad from "react-lazy-load"
10
import MiniRanking from "./MiniRanking"
11
12
const officialCompetitionTypes = [
13
  'OFFICIAL',
14
  'COMPETITION',
15
  'Competitie'
16
]
17
18
export const MatchTeaserDetail = ({ match, includeRankings }: MatchTeaserDetailProps) => {
19
  Settings.defaultZone = `Europe/Brussels`
20
  Settings.defaultLocale = `nl-be`
21
22
  const matchDateTime = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`)
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={matchDateTime.toFormat(`yyyy-MM-dd - H:mm`)}
36
            >
37
              {matchDateTime.toFormat(`EEEE dd LLLL - 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
47
              className="match__teaser__datetime match__teaser__datetime--date"
48
              dateTime={matchDateTime.toFormat(`yyyy-MM-dd`)}
49
            >
50
              {matchDateTime.toFormat(`EEEE dd LLLL`)}
51
            </time>
52
            {` `}-{` `}
53
            <time
54
              className="match__teaser__datetime match__teaser__datetime--time"
55
              dateTime={matchDateTime.toFormat(`H:mm`)}
56
            >
57
              {matchDateTime.toFormat(`H:mm`)}
58
            </time>
59
          </div>
60
        )}
61
      </header>
62
      <main>
63
        <div
64
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
65
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
66
          })}
67
        >
68
          <LazyLoad>
69
            <img
70
              src={match.homeClub?.logo}
71
              alt={match.homeClub?.abbreviation}
72
              className="match__teaser__logo match__teaser__logo--home"
73
            />
74
          </LazyLoad>
75
          {match.homeClub?.abbreviation || match.homeClub?.name}
76
        </div>
77
78
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
79
        {matchPlayed && (
80
          <div className="match__teaser__vs match__teaser__vs--score">
81
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
82
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
83
          </div>
84
        )}
85
86
        <div
87
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
88
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
89
          })}
90
        >
91
          <LazyLoad>
92
            <img
93
              src={match.awayClub?.logo}
94
              alt={match.awayClub?.abbreviation}
95
              className="match__teaser__logo match__teaser__logo--away"
96
            />
97
          </LazyLoad>
98
          {match.awayClub?.abbreviation || match.awayClub?.name}
99
        </div>
100
      </main>
101
      {includeRankings && officialCompetitionTypes.includes(match.competitionType) && (
102
        <MiniRanking
103
          teamId={match.homeTeamId || match.awayTeamId}
104
          homeTeam={match.homeClub?.name}
105
          awayTeam={match.awayClub?.name}
106
        />
107
      )}
108
    </article>
109
  )
110
}
111
112
export const MatchTeaser = ({ teamId, action, includeRankings }: MatchTeaserProps) => {
113
  if (action !== `prev` && action !== `next`) {
114
    throw new Error(`Invalid action provided`)
115
  }
116
117
  const [data, setData] = useState<Match[]>([])
118
119
  const { kcvvPsdApi } = useSiteMetaData()
120
121
  useEffect(() => {
122
    async function getData() {
123
      const response = await request.get(`${kcvvPsdApi}/matches/${action}`, {
124
        params: { include: teamId },
125
      })
126
      setData(response.data)
127
    }
128
    getData()
129
  }, [action, kcvvPsdApi, teamId])
130
131
  if (data.length > 0) {
132
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
133
  } else {
134
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
135
  }
136
}
137
138
export const MatchTeasers = ({ teamId, includeRankings = false }: MatchTeasersProps) => (
139
  <div className="match__teasers__wrapper full-width">
140
    <div className="match__teasers__inner">
141
      <div className="match__teasers match__teasers--prev">
142
        <header className="match__teasers__header">Vorige</header>
143
        <MatchTeaser teamId={teamId} action="prev" includeRankings={includeRankings} />
144
      </div>
145
      <div className="match__teasers match__teasers--next">
146
        <header className="match__teasers__header">Volgende</header>
147
        <MatchTeaser teamId={teamId} action="next" includeRankings={includeRankings} />
148
      </div>
149
    </div>
150
  </div>
151
)
152
153
MatchTeaser.defaultProps = { includeRankings: false }
154
MatchTeasers.defaultProps = { includeRankings: false }
155
MatchTeaserDetail.defaultProps = { includeRankings: false }
156