Passed
Push — hotfix/2.2.1 ( 7ca5ce )
by Kevin Van
05:15
created

MatchTeaser.tsx ➔ getData   B

Complexity

Conditions 6

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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