1
|
|
|
import React, { FunctionComponent, useEffect, useState } from "react" |
2
|
|
|
import { graphql, Link, 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, mapPsdStatusShort } from "../scripts/helper" |
11
|
|
|
|
12
|
|
|
import "./Matches.scss" |
13
|
|
|
import Icon from "./Icon" |
14
|
|
|
|
15
|
|
|
const MatchesRow: FunctionComponent<MatchesRowProps> = ({ match }: MatchesRowProps) => { |
16
|
|
|
const d = Moment.tz(match.date, `Europe/Brussels`) |
17
|
|
|
const date = d.format(`dddd D MMMM YYYY`) |
18
|
|
|
const time = d.format(`HH:mm`) |
19
|
|
|
const matchPlayed = (match.status === 0 && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) || false |
20
|
|
|
|
21
|
|
|
return ( |
22
|
|
|
<article> |
23
|
|
|
<header className="matches__calendar__title"> |
24
|
|
|
<h3> |
25
|
|
|
<span className="matches__calendar__date">{date}</span> |
26
|
|
|
<span className="matches__calendar__separator">|</span> |
27
|
|
|
<span className="matches__calendar__type">{match.competitionType}</span> |
28
|
|
|
</h3> |
29
|
|
|
</header> |
30
|
|
|
<main className="matches__calendar__main"> |
31
|
|
|
<div |
32
|
|
|
className={classNames(`matches__calendar__team`, `matches__calendar__team--home`, { |
33
|
|
|
"matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam, |
34
|
|
|
})} |
35
|
|
|
> |
36
|
|
|
{match.homeClub.abbreviation || match.homeClub.name} |
37
|
|
|
|
38
|
|
|
<LazyLoad debounce={false}> |
39
|
|
|
<img |
40
|
|
|
src={match.homeClub.logo} |
41
|
|
|
alt={match.homeClub.name} |
42
|
|
|
className="matches__calendar__logo matches__calendar__logo--home" |
43
|
|
|
/> |
44
|
|
|
</LazyLoad> |
45
|
|
|
</div> |
46
|
|
|
|
47
|
|
|
<div className="matches__calendar__score"> |
48
|
|
|
{match.status !== 0 && ( |
49
|
|
|
<span title={mapPsdStatus(match.status) || ``}>{mapPsdStatusShort(match.status)}</span> |
50
|
|
|
)} |
51
|
|
|
{match.status === 0 && !matchPlayed && <span>{time}</span>} |
52
|
|
|
{matchPlayed && ( |
53
|
|
|
<span> |
54
|
|
|
{match.goalsHomeTeam} - {match.goalsAwayTeam} |
55
|
|
|
</span> |
56
|
|
|
)} |
57
|
|
|
</div> |
58
|
|
|
|
59
|
|
|
<div |
60
|
|
|
className={classNames(`matches__calendar__team`, `matches__calendar__team--away`, { |
61
|
|
|
"matches__calendar__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam, |
62
|
|
|
})} |
63
|
|
|
> |
64
|
|
|
<LazyLoad debounce={false}> |
65
|
|
|
<img |
66
|
|
|
src={match.awayClub.logo} |
67
|
|
|
alt={match.awayClub.name} |
68
|
|
|
className="matches__calendar__logo matches__calendar__logo--away" |
69
|
|
|
/> |
70
|
|
|
</LazyLoad> |
71
|
|
|
{match.awayClub.abbreviation || match.awayClub.name} |
72
|
|
|
</div> |
73
|
|
|
|
74
|
|
|
<Link to={`/game/${match.id}`} className="matches__calendar__link"> |
75
|
|
|
<Icon icon="fa-info-circle" /> |
76
|
|
|
</Link> |
77
|
|
|
</main> |
78
|
|
|
</article> |
79
|
|
|
) |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
const Matches: FunctionComponent<MatchesProps> = ({ teamId }: MatchesProps) => { |
83
|
|
|
const [data, setData] = useState<Match[]>([]) |
84
|
|
|
|
85
|
|
|
const { |
86
|
|
|
site: { |
87
|
|
|
siteMetadata: { kcvvPsdApi }, |
88
|
|
|
}, |
89
|
|
|
}: MatchesQueryData = useStaticQuery(graphql` |
90
|
|
|
{ |
91
|
|
|
site { |
92
|
|
|
siteMetadata { |
93
|
|
|
kcvvPsdApi |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
`) |
98
|
|
|
|
99
|
|
|
Moment.locale(`nl-BE`) |
100
|
|
|
|
101
|
|
|
useEffect(() => { |
102
|
|
|
async function getData() { |
103
|
|
|
const response = await axios.get(`${kcvvPsdApi}/matches/${teamId}`) |
104
|
|
|
setData(response.data) |
105
|
|
|
} |
106
|
|
|
getData() |
107
|
|
|
}, []) |
108
|
|
|
|
109
|
|
|
return ( |
110
|
|
|
<div className={`matches__wrapper`}> |
111
|
|
|
{data |
112
|
|
|
.sort((a, b) => a.timestamp - b.timestamp) |
113
|
|
|
.map((match, i) => ( |
114
|
|
|
<MatchesRow match={match} key={i} /> |
115
|
|
|
))} |
116
|
|
|
</div> |
117
|
|
|
) |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
export default Matches |
121
|
|
|
|