forked from coderplex-org/coderplex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
159 lines (151 loc) · 4.88 KB
/
events.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React from 'react';
import fetch from 'isomorphic-unfetch';
import { Flex, Box } from 'grid-emotion';
import styled from 'react-emotion';
import { space } from 'styled-system';
import Layout from '../components/common/layout';
import BannerSection from '../components/common/banner';
import { Container, SubTitle, Button } from '../utils/base.styles';
import { baseEventsURL, futureEventsURL, pastEventsURL, imagePlaceholderURL } from '../utils/urls';
import EventCard from '../components/events/event-card';
const EventsSection = styled.section`
${space};
background: #fff;
position: relative;
& .loadmore_div {
text-align: center;
margin-top: 2rem;
margin-bottom: 0.8rem;
}
& .event_type_title {
color: #374355;
font-weight: bold;
}
`;
export default class Events extends React.Component {
state = {
pastEvents: [],
pastEventsLoadLimit: 2,
futureEvents: [],
futureEventsLoadLimit: 1,
fetchError: null,
loading: true,
};
async componentDidMount() {
try {
let pastEvents;
let futureEvents;
const pastEventsResponse = await fetch(`${baseEventsURL}${pastEventsURL}`);
if (pastEventsResponse.ok) {
pastEvents = await pastEventsResponse.json();
} else {
throw new Error('Failed to Retrieve past events');
}
const futureEventsResponse = await fetch(`${baseEventsURL}${futureEventsURL}`);
if (futureEventsResponse.ok) {
futureEvents = await futureEventsResponse.json();
} else {
throw new Error('Failed to retieve future events');
}
await this.setState({
pastEvents,
futureEvents,
fetchError: null,
loading: false,
});
} catch (err) {
console.log(err);
await this.setState({
pastEvents: null,
futureEvents: null,
fetchError: err.message,
loading: false,
});
}
}
renderEvents(events, loadLimit) {
if (this.state.loading) {
return (
<SubTitle inverted color="#222">
Loading..
</SubTitle>
);
} else if (events.length === 0) {
return (
<SubTitle inverted color="#222">
No upcoming events yet, check back later
</SubTitle>
);
} else if (events === null) {
return (
<SubTitle inverted color="#222">
Oops! somethings went wrong while fetching the events
</SubTitle>
);
}
return (
<div>
{events.slice(0, loadLimit).map(event => {
const regexForImageSrc = /<img.*?src="([^">]*\/([^">]*?))".*?>/g;
const imageSrc = regexForImageSrc.exec(event.description);
return (
<EventCard
key={event.id}
image={imageSrc ? imageSrc[1] : imagePlaceholderURL}
name={event.name}
location={event.venue ? event.venue.name : 'Online'}
online={!event.venue}
time={event.time}
attendees={event.yes_rsvp_count}
tense={event.status}
link={event.link}
/>
);
})}
</div>
);
}
renderLoadMoreButton(eventsTotalLength, loadLimit, isPastEvent) {
return loadLimit >= eventsTotalLength ? null : (
<div className="loadmore_div" mb={[5, 5]}>
<Button inverted medium onClick={event => this.loadMore(event, isPastEvent)}>
Load more
</Button>
</div>
);
}
loadMore(isPastEvent) {
return isPastEvent
? this.setState({ pastEventsLoadLimit: this.state.pastEventsLoadLimit + 5 })
: this.setState({ futureEventsLoadLimit: this.state.futureEventsLoadLimit + 5 });
}
render() {
return (
<Layout>
<BannerSection title="Online & Offline Events" subTitle="Because you cannot change the world alone" />
<EventsSection py={[2, 2]} px={[2, 1]}>
<Container>
<Flex pb={[2, 2]} direction="column" align="center" justify="center">
<Box width={[1, 0.75]}>
<h3 className="event_type_title" inverted color="#222">
Upcoming Events
</h3>
{this.renderEvents(this.state.futureEvents, this.state.futureEventsLoadLimit)}
{this.renderLoadMoreButton(this.state.futureEvents.length, this.state.futureEventsLoadLimit, false)}
</Box>
</Flex>
<Flex direction="column" align="center" justify="center">
<Box width={[1, 0.75]}>
<h3 className="event_type_title" inverted color="#222">
Recent Events
</h3>
{this.renderEvents(this.state.pastEvents, this.state.pastEventsLoadLimit)}
{this.renderLoadMoreButton(this.state.pastEvents.length, this.state.pastEventsLoadLimit, true)}
</Box>
</Flex>
</Container>
</EventsSection>
</Layout>
);
}
}