|
1
|
|
|
import React, { Fragment, 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, mapPsdStatusIcon, capitalizeFirstLetter, groupByDate } from "../scripts/helper" |
|
11
|
|
|
|
|
12
|
|
|
import "./Calendar.scss" |
|
13
|
|
|
import Icon from "./Icon" |
|
14
|
|
|
import Spinner from "./Spinner" |
|
15
|
|
|
|
|
16
|
|
|
const CalendarEvent: FunctionComponent<CalendarEventProps> = ({ event }: CalendarEventProps) => { |
|
17
|
|
|
const startDate = Moment.tz(event.start, `Europe/Brussels`) |
|
18
|
|
|
const endDate = Moment.tz(event.end, `Europe/Brussels`) |
|
19
|
|
|
|
|
20
|
|
|
return ( |
|
21
|
|
|
<article className="calendar__event"> |
|
22
|
|
|
<div className="calendar__date"> |
|
23
|
|
|
<Icon icon="fa-clock-o" /> {startDate.format(`HH:mm`)} - {endDate.format(`HH:mm`)} |
|
24
|
|
|
</div> |
|
25
|
|
|
<div className={`calendar__type calendar__type--${event.type}`}>{capitalizeFirstLetter(event.type)}</div> |
|
26
|
|
|
<div> |
|
27
|
|
|
{event.cancelled && ( |
|
28
|
|
|
<span className="calendar__status calendar__status--cancelled"> |
|
29
|
|
|
<Icon icon={`fa-times`} alt={`Afgelast`} /> |
|
30
|
|
|
</span> |
|
31
|
|
|
)} |
|
32
|
|
|
{` `} |
|
33
|
|
|
<span className="calendar__title">{event.title}</span> |
|
34
|
|
|
</div> |
|
35
|
|
|
{event.description && <p dangerouslySetInnerHTML={{ __html: event.description }} />} |
|
36
|
|
|
</article> |
|
37
|
|
|
) |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
const Calendar: FunctionComponent = () => { |
|
41
|
|
|
const [data, setData] = useState<CalendarEvent[]>([]) |
|
42
|
|
|
|
|
43
|
|
|
const { |
|
44
|
|
|
site: { |
|
45
|
|
|
siteMetadata: { kcvvPsdApi }, |
|
46
|
|
|
}, |
|
47
|
|
|
}: CalendarQueryData = useStaticQuery(graphql` |
|
48
|
|
|
{ |
|
49
|
|
|
site { |
|
50
|
|
|
siteMetadata { |
|
51
|
|
|
kcvvPsdApi |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
`) |
|
56
|
|
|
|
|
57
|
|
|
Moment.locale(`nl-BE`) |
|
58
|
|
|
|
|
59
|
|
|
useEffect(() => { |
|
60
|
|
|
async function getData() { |
|
61
|
|
|
const response = await axios.get(`${kcvvPsdApi}/events/next`) |
|
62
|
|
|
setData(response.data) |
|
63
|
|
|
} |
|
64
|
|
|
getData() |
|
65
|
|
|
}, []) |
|
66
|
|
|
|
|
67
|
|
|
const groupedEvents = groupByDate(data) |
|
68
|
|
|
|
|
69
|
|
|
return ( |
|
70
|
|
|
<div className={`events__wrapper`}> |
|
71
|
|
|
{data.length > 0 || <Spinner />} |
|
72
|
|
|
|
|
73
|
|
|
{groupedEvents.map((group, i) => { |
|
74
|
|
|
const date = Moment.tz(group.date, `Europe/Brussels`) |
|
75
|
|
|
return ( |
|
76
|
|
|
<div key={i} className="events__date_group"> |
|
77
|
|
|
<h2>{capitalizeFirstLetter(date.format(`dddd D MMM YYYY`))}</h2> |
|
78
|
|
|
<div className={`events__date`}> |
|
79
|
|
|
{group.objects.map((event: CalendarEvent, j: number) => ( |
|
80
|
|
|
<CalendarEvent event={event} key={j} /> |
|
81
|
|
|
))} |
|
82
|
|
|
</div> |
|
83
|
|
|
</div> |
|
84
|
|
|
) |
|
85
|
|
|
})} |
|
86
|
|
|
</div> |
|
87
|
|
|
) |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
export default Calendar |
|
91
|
|
|
|