-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventSection.tsx
96 lines (93 loc) · 2.8 KB
/
EventSection.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { useState } from 'react';
import styles from '../styles/layouts/EventSection.module.scss';
import { EventGridStyle, type EventSection, type EventItem } from '../config';
import { FaChevronLeft, FaChevronRight } from 'react-icons/fa';
import Event from '../components/Event';
const getSectionLayout = (
events: [EventItem, JSX.Element][],
gridStyle: EventGridStyle
): JSX.Element[][] => {
// Determine the layout
switch (gridStyle) {
case EventGridStyle.Grid:
return [events.map((e) => e[1])];
case EventGridStyle.List: {
// Chunk Array
const chunkSize = 3;
const chunks = [];
for (let i = 0; i < events.length; i += chunkSize) {
chunks.push(events.slice(i, i + chunkSize).map((e) => e[1]));
}
// Return Style
return chunks;
}
case EventGridStyle.HomeList: {
// Filter
const now = new Date();
const filteredEvents = events
.filter(([event, _]) => event.start_date > now)
.sort((a, b) => a[0].start_date.getTime() - b[0].start_date.getTime());
// Return Style
return [filteredEvents.slice(0, 3).map((e) => e[1])];
}
default:
throw new Error('Unknown GridStyle In EventSection');
}
};
interface Props {
className?: string;
section: EventSection;
}
export default function EventSection({ className, section }: Props) {
const [currentView, setCurrentView] = useState(0);
// Map the events
const events = section.events.map((event, i): [EventItem, JSX.Element] => [
event,
<Event key={i} eventItem={event} />,
]);
// Determine style
const event_list = getSectionLayout(events, section.grid_style);
// Map events
const event_view =
event_list.length == 0 || event_list[currentView].length == 0 ? (
<p>There are currently no events present.</p>
) : (
event_list[currentView]
);
// Build ui
return (
<div className={[styles.container, className].join(' ')}>
{/* Event Container */}
<div>{event_view}</div>
{/* Possible Buttons */}
{event_list.length > 1 && (
<ul>
<li>
<FaChevronLeft
className={styles.icon}
onClick={() => setCurrentView((view) => Math.max(view - 1, 0))}
/>
</li>
{event_list.map((_, i) => (
<li key={i}>
<button
onClick={() => setCurrentView(i)}
className={currentView == i ? styles.active : ''}
></button>
</li>
))}
<li>
<FaChevronRight
className={styles.icon}
onClick={() =>
setCurrentView((view) =>
Math.min(view + 1, event_list.length - 1)
)
}
/>
</li>
</ul>
)}
</div>
);
}