1
|
|
|
import { DateTime, Settings } from "luxon" |
2
|
|
|
import React from "react" |
3
|
|
|
import { useEffect, useState } from "react" |
4
|
|
|
import { useSiteMetaData } from "../hooks/use-site-metadata" |
5
|
|
|
import { Match, MatchesProps, MatchesRowProps } from "../Types/Match" |
6
|
|
|
import { Spinner } from "./Spinner" |
7
|
|
|
import "./Matches.scss" |
8
|
|
|
import classnames from "classnames" |
9
|
|
|
import LazyLoad from "react-lazy-load" |
10
|
|
|
import { mapPsdStatus, mapPsdStatusShort, request } from "../scripts/helper" |
11
|
|
|
import { Link } from "gatsby" |
12
|
|
|
import Icon from "./Icon" |
13
|
|
|
|
14
|
|
|
const Matches = ({ teamId }: MatchesProps) => { |
15
|
|
|
const { kcvvPsdApi } = useSiteMetaData() |
16
|
|
|
const [data, setData] = useState<Match[]>([]) |
17
|
|
|
|
18
|
|
|
useEffect(() => { |
19
|
|
|
async function getData() { |
20
|
|
|
const response = await request.get(`${kcvvPsdApi}/matches/${teamId}`) |
21
|
|
|
setData(response.data) |
22
|
|
|
} |
23
|
|
|
getData() |
24
|
|
|
}, [kcvvPsdApi, teamId]) |
25
|
|
|
|
26
|
|
|
return ( |
27
|
|
|
<div className="matches__wrapper"> |
28
|
|
|
{data.length > 0 || <Spinner />} |
29
|
|
|
{data |
30
|
|
|
.sort((a, b) => a.timestamp - b.timestamp) |
31
|
|
|
.map((match, i) => ( |
32
|
|
|
<MatchesRow match={match} key={i} /> |
33
|
|
|
))} |
34
|
|
|
</div> |
35
|
|
|
) |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
const MatchesRow = ({ match }: MatchesRowProps) => { |
39
|
|
|
Settings.defaultZone = `Europe/Brussels` |
40
|
|
|
Settings.defaultLocale = `nl-be` |
41
|
|
|
|
42
|
|
|
const matchDateTime = DateTime.fromFormat(match.date, `yyyy-MM-dd HH:mm`) |
43
|
|
|
const matchDate = matchDateTime.toFormat(`EEE d MMM`) |
44
|
|
|
const matchTime = matchDateTime.toFormat(`HH:mm`) |
45
|
|
|
const matchPlayed = |
46
|
|
|
((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) || |
47
|
|
|
false |
48
|
|
|
|
49
|
|
|
return ( |
50
|
|
|
<div className="matches__row"> |
51
|
|
|
<article className="matches__match"> |
52
|
|
|
<div className="matches__competition__wrapper"> |
53
|
|
|
<span className="matches__competition__type">{match.competitionType}</span> |
54
|
|
|
</div> |
55
|
|
|
<div className="matches__date__wrapper"> |
56
|
|
|
<span className="matches__date__date">{matchDate}</span> |
57
|
|
|
<span className="matches__date__time">{matchTime}</span> |
58
|
|
|
</div> |
59
|
|
|
<div |
60
|
|
|
className={classnames(`matches__team__wrapper`, `matches__team__wrapper--home`, { |
61
|
|
|
"matches__team__wrapper--elewijt": match.homeTeamId, |
62
|
|
|
})} |
63
|
|
|
> |
64
|
|
|
<div className="matches__team__name"> |
65
|
|
|
{match.homeClub?.abbreviation || match.homeClub?.name} |
66
|
|
|
{` `} |
67
|
|
|
<span className="matches__team__home_indicator" title="Uitwedstrijd"> |
68
|
|
|
(U) |
69
|
|
|
</span> |
70
|
|
|
</div> |
71
|
|
|
<div className="matches__team__logo"> |
72
|
|
|
<LazyLoad width={44} height={44}> |
73
|
|
|
<img |
74
|
|
|
src={match.homeClub?.logo} |
75
|
|
|
alt={match.homeClub?.abbreviation} |
76
|
|
|
className="match__teaser__logo match__teaser__logo--home" |
77
|
|
|
/> |
78
|
|
|
</LazyLoad> |
79
|
|
|
</div> |
80
|
|
|
</div> |
81
|
|
|
<div className="matches__score__wrapper"> |
82
|
|
|
{match.status !== 0 && ( |
83
|
|
|
<span className="matches__score matches__score--status" title={mapPsdStatus(match.status) || ``}> |
84
|
|
|
{mapPsdStatusShort(match.status)} |
85
|
|
|
</span> |
86
|
|
|
)} |
87
|
|
|
{(match.status === 0 || match.status === null) && !matchPlayed && ( |
88
|
|
|
<span className="matches__score matches__score--vs">VS</span> |
89
|
|
|
)} |
90
|
|
|
{matchPlayed && ( |
91
|
|
|
<span className="matches__score matches__score--score"> |
92
|
|
|
<span>{match.goalsHomeTeam}</span> <span>-</span> <span>{match.goalsAwayTeam}</span> |
93
|
|
|
</span> |
94
|
|
|
)} |
95
|
|
|
</div> |
96
|
|
|
<div |
97
|
|
|
className={classnames(`matches__team__wrapper`, `matches__team__wrapper--away`, { |
98
|
|
|
"matches__team__wrapper--elewijt": match.awayTeamId, |
99
|
|
|
})} |
100
|
|
|
> |
101
|
|
|
<div className="matches__team__name"> |
102
|
|
|
{match.awayClub?.abbreviation || match.awayClub?.name} |
103
|
|
|
{` `} |
104
|
|
|
<span className="matches__team__home_indicator" title="Thuiswedstrijd"> |
105
|
|
|
(T) |
106
|
|
|
</span> |
107
|
|
|
</div> |
108
|
|
|
<div className="matches__team__logo"> |
109
|
|
|
<LazyLoad width={44} height={44}> |
110
|
|
|
<img |
111
|
|
|
src={match.awayClub?.logo} |
112
|
|
|
alt={match.awayClub?.abbreviation} |
113
|
|
|
className="match__teaser__logo match__teaser__logo--away" |
114
|
|
|
/> |
115
|
|
|
</LazyLoad> |
116
|
|
|
</div> |
117
|
|
|
</div> |
118
|
|
|
<div className="matches__info__wrapper"> |
119
|
|
|
<Link to={`/game/${match.id}`} className="matches__calendar__link"> |
120
|
|
|
<Icon icon="fa-info-circle" /> Verslag |
121
|
|
|
</Link> |
122
|
|
|
</div> |
123
|
|
|
</article> |
124
|
|
|
</div> |
125
|
|
|
) |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
export default Matches |
129
|
|
|
|