Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit cfa1ee8

Browse files
committed
move event fetch logic to componentDidMount for static export
1 parent c97f7f2 commit cfa1ee8

File tree

1 file changed

+69
-50
lines changed

1 file changed

+69
-50
lines changed

pages/events.js

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import fetch from 'isomorphic-unfetch';
3-
import { Card, Divider } from 'semantic-ui-react';
3+
import { Card, Divider, Dimmer, Loader } from 'semantic-ui-react';
44

55
import publicPage from '../hocs/public-page';
66
import {
@@ -11,7 +11,13 @@ import {
1111
import RowEvent from '../components/row-events';
1212

1313
class Events extends React.Component {
14-
static async getInitialProps() {
14+
state = {
15+
pastEvents: null,
16+
futureEvents: null,
17+
fetchError: null,
18+
loading: true,
19+
};
20+
async componentDidMount() {
1521
try {
1622
const pastEvents = await fetch(
1723
`${reverseProxyCORS}${pastEventsMeetupURL}`,
@@ -25,21 +31,72 @@ class Events extends React.Component {
2531
if (res.ok) return res.json();
2632
throw new Error('Failed to Retrieve Events');
2733
});
28-
return {
34+
await this.setState({
2935
pastEvents,
3036
futureEvents,
3137
fetchError: null,
32-
};
38+
loading: false,
39+
});
3340
} catch (err) {
3441
console.log(err);
35-
return {
42+
await this.setState({
3643
pastEvents: null,
3744
futureEvents: null,
3845
fetchError: err.message,
39-
};
46+
loading: false,
47+
});
4048
}
4149
}
42-
50+
renderEvents() {
51+
if (this.state.fetchError) {
52+
return <div>{this.state.fetchError}</div>;
53+
}
54+
return (
55+
<div>
56+
<section>
57+
<h2>Upcoming events</h2>
58+
<div>
59+
{this.state.futureEvents.map(event => (
60+
<Card.Group key={event.id}>
61+
<RowEvent
62+
name={event.name}
63+
yesCount={event.yes_rsvp_count}
64+
time={event.time}
65+
venue={event.venue}
66+
link={event.link}
67+
status={event.status}
68+
/>
69+
</Card.Group>
70+
))}
71+
</div>
72+
</section>
73+
<Divider />
74+
<section>
75+
<h2>Recent events</h2>
76+
<div>
77+
{this.state.pastEvents.map(event => (
78+
<Card.Group key={event.id}>
79+
<RowEvent
80+
key={event.id}
81+
name={event.name}
82+
yesCount={event.yes_rsvp_count}
83+
time={event.time}
84+
venue={event.venue}
85+
link={event.link}
86+
status={event.status}
87+
/>
88+
</Card.Group>
89+
))}
90+
</div>
91+
</section>
92+
<style jsx>{`
93+
section {
94+
margin: 50px 0;
95+
}
96+
`}</style>
97+
</div>
98+
);
99+
}
43100
render() {
44101
return (
45102
<div>
@@ -48,47 +105,12 @@ class Events extends React.Component {
48105
<h2>Because you cannot change the world alone</h2>
49106
</div>
50107
<main>
51-
{this.props.fetchError ? (
52-
<div>{this.props.fetchError}</div>
108+
{this.state.loading ? (
109+
<Dimmer active>
110+
<Loader indeterminate>Fetching Events</Loader>
111+
</Dimmer>
53112
) : (
54-
<div>
55-
<section>
56-
<h2>Upcoming events</h2>
57-
<div>
58-
{this.props.futureEvents.map(event => (
59-
<Card.Group key={event.id}>
60-
<RowEvent
61-
name={event.name}
62-
yesCount={event.yes_rsvp_count}
63-
time={event.time}
64-
venue={event.venue}
65-
link={event.link}
66-
status={event.status}
67-
/>
68-
</Card.Group>
69-
))}
70-
</div>
71-
</section>
72-
<Divider />
73-
<section>
74-
<h2>Recent events</h2>
75-
<div>
76-
{this.props.pastEvents.map(event => (
77-
<Card.Group key={event.id}>
78-
<RowEvent
79-
key={event.id}
80-
name={event.name}
81-
yesCount={event.yes_rsvp_count}
82-
time={event.time}
83-
venue={event.venue}
84-
link={event.link}
85-
status={event.status}
86-
/>
87-
</Card.Group>
88-
))}
89-
</div>
90-
</section>
91-
</div>
113+
this.renderEvents()
92114
)}
93115
</main>
94116
<style jsx>{`
@@ -115,9 +137,6 @@ class Events extends React.Component {
115137
color: #df1cb5;
116138
font-weight: 900;
117139
}
118-
section {
119-
margin: 50px 0;
120-
}
121140
`}</style>
122141
</div>
123142
);

0 commit comments

Comments
 (0)