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 869647b

Browse files
author
z
committed
added load more
1 parent 1185f0d commit 869647b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

pages/events.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ import { space } from 'styled-system';
66

77
import Layout from '../components/common/layout';
88
import BannerSection from '../components/common/banner';
9-
import { Container, Title, SubTitle } from '../utils/base.styles';
9+
import { Container, Title, SubTitle, Button } from '../utils/base.styles';
1010
import { baseEventsURL, futureEventsURL, pastEventsURL, imagePlaceholderURL } from '../utils/urls';
1111
import EventCard from '../components/events/event-card';
1212

1313
const EventsSection = styled.section`
1414
${space};
1515
background: #fff;
1616
position: relative;
17+
& .loadmore_div {
18+
text-align: center;
19+
}
1720
`;
1821

1922
export default class Events extends React.Component {
2023
state = {
2124
pastEvents: [],
25+
pastEventsLoadLimit: 2,
2226
futureEvents: [],
27+
futureEventsLoadLimit: 2,
2328
fetchError: null,
2429
loading: true,
2530
};
@@ -57,7 +62,7 @@ export default class Events extends React.Component {
5762
}
5863
}
5964

60-
renderEvents(events) {
65+
renderEvents(events, loadLimit) {
6166
if (this.state.loading) {
6267
return (
6368
<SubTitle inverted color="#222">
@@ -79,7 +84,7 @@ export default class Events extends React.Component {
7984
}
8085
return (
8186
<div>
82-
{events.map(event => {
87+
{events.slice(0, loadLimit).map(event => {
8388
const regexForImageSrc = /<img.*?src="([^">]*\/([^">]*?))".*?>/g;
8489
const imageSrc = regexForImageSrc.exec(event.description);
8590
return (
@@ -100,6 +105,22 @@ export default class Events extends React.Component {
100105
);
101106
}
102107

108+
renderLoadMoreButton(eventsTotalLength, loadLimit, isPastEvent) {
109+
return loadLimit >= eventsTotalLength ? null : (
110+
<div className="loadmore_div">
111+
<Button inverted medium onClick={event => this.loadMore(event, isPastEvent)}>
112+
Load more
113+
</Button>
114+
</div>
115+
);
116+
}
117+
118+
loadMore(isPastEvent) {
119+
return isPastEvent
120+
? this.setState({ pastEventsLoadLimit: this.state.pastEventsLoadLimit + 5 })
121+
: this.setState({ futureEventsLoadLimit: this.state.futureEventsLoadLimit + 5 });
122+
}
123+
103124
render() {
104125
return (
105126
<Layout>
@@ -115,15 +136,17 @@ export default class Events extends React.Component {
115136
<Title inverted color="#222">
116137
Upcoming Events
117138
</Title>
118-
{this.renderEvents(this.state.futureEvents)}
139+
{this.renderEvents(this.state.futureEvents, this.state.futureEventsLoadLimit)}
140+
{this.renderLoadMoreButton(this.state.futureEvents.length, this.state.futureEventsLoadLimit, false)}
119141
</Box>
120142
</Flex>
121143
<Flex direction="column" align="center" justify="center">
122144
<Box width={[1, 0.75]}>
123145
<Title inverted color="#222">
124146
Recent Events
125147
</Title>
126-
{this.renderEvents(this.state.pastEvents)}
148+
{this.renderEvents(this.state.pastEvents, this.state.pastEventsLoadLimit)}
149+
{this.renderLoadMoreButton(this.state.pastEvents.length, this.state.pastEventsLoadLimit, true)}
127150
</Box>
128151
</Flex>
129152
</Container>

0 commit comments

Comments
 (0)