forked from VulcanJS/Vulcan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostsDailyList.jsx
118 lines (101 loc) · 3.7 KB
/
PostsDailyList.jsx
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
import React, { PropTypes, Component } from 'react';
import moment from 'moment';
import { FormattedMessage } from 'react-intl';
import Posts from 'meteor/nova:posts';
import { withCurrentUser, withList, getSetting, Components, getRawComponent, registerComponent } from 'meteor/nova:core';
class PostsDailyList extends Component{
constructor(props) {
super(props);
this.loadMoreDays = this.loadMoreDays.bind(this);
this.state = {
days: props.days,
after: props.terms.after,
daysLoaded: props.days,
afterLoaded: props.terms.after,
before: props.terms.before,
loading: true,
};
}
// intercept prop change and only show more days once data is done loading
componentWillReceiveProps(nextProps) {
if (nextProps.networkStatus === 2) {
this.setState({loading: true});
} else {
this.setState((prevState, props) => ({
loading: false,
daysLoaded: prevState.days,
afterLoaded: prevState.after,
}));
}
}
// return date objects for all the dates in a range
getDateRange(after, before) {
const mAfter = moment(after, 'YYYY-MM-DD');
const mBefore = moment(before, 'YYYY-MM-DD');
const daysCount = mBefore.diff(mAfter, 'days') + 1;
const range = _.range(daysCount).map(
i => moment(before, 'YYYY-MM-DD').subtract(i, 'days').startOf('day')
);
return range;
}
getDatePosts(posts, date) {
return _.filter(posts, post => moment(new Date(post.postedAt)).startOf('day').isSame(date, 'day'));
}
// variant 1: reload everything each time (works with polling)
loadMoreDays(e) {
e.preventDefault();
const numberOfDays = getSetting('numberOfDays', 5);
const loadMoreAfter = moment(this.state.after, 'YYYY-MM-DD').subtract(numberOfDays, 'days').format('YYYY-MM-DD');
this.props.loadMore({
...this.props.terms,
after: loadMoreAfter,
});
this.setState({
days: this.state.days + this.props.increment,
after: loadMoreAfter,
});
}
// variant 2: only load new data (need to disable polling)
loadMoreDaysInc(e) {
e.preventDefault();
const numberOfDays = getSetting('numberOfDays', 5);
const loadMoreAfter = moment(this.state.after, 'YYYY-MM-DD').subtract(numberOfDays, 'days').format('YYYY-MM-DD');
const loadMoreBefore = moment(this.state.after, 'YYYY-MM-DD').subtract(1, 'days').format('YYYY-MM-DD');
this.props.loadMoreInc({
...this.props.terms,
before: loadMoreBefore,
after: loadMoreAfter,
});
this.setState({
days: this.state.days + this.props.increment,
after: loadMoreAfter,
});
}
render() {
const posts = this.props.results;
const dates = this.getDateRange(this.state.afterLoaded, this.state.before);
return (
<div className="posts-daily">
<Components.PostsListHeader />
{dates.map((date, index) => <Components.PostsDay key={index} number={index} date={date} posts={this.getDatePosts(posts, date)} networkStatus={this.props.networkStatus} currentUser={this.props.currentUser} />)}
{this.state.loading? <Components.PostsLoading /> : <a className="posts-load-more posts-load-more-days" onClick={this.loadMoreDays}><FormattedMessage id="posts.load_more_days"/></a>}
</div>
)
}
}
PostsDailyList.propTypes = {
currentUser: React.PropTypes.object,
days: React.PropTypes.number,
increment: React.PropTypes.number
};
PostsDailyList.defaultProps = {
days: getSetting('numberOfDays', 5),
increment: getSetting('numberOfDays', 5)
};
const options = {
collection: Posts,
queryName: 'postsDailyListQuery',
fragmentName: 'PostsList',
limit: 0,
};
registerComponent('PostsDailyList', PostsDailyList, withCurrentUser, [withList, options]);