2016-12-05 16:30:31 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import moment from 'moment';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import Posts from 'meteor/nova:posts';
|
2016-12-12 15:00:56 +09:00
|
|
|
import { withList, getSetting, Components, getRawComponent, registerComponent } from 'meteor/nova:core';
|
2016-12-05 16:30:31 +09:00
|
|
|
|
|
|
|
class PostsDailyList extends Component{
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.loadMoreDays = this.loadMoreDays.bind(this);
|
2016-12-06 14:46:28 +09:00
|
|
|
this.state = {
|
|
|
|
days: props.days,
|
|
|
|
after: props.terms.after,
|
|
|
|
before: props.terms.before,
|
|
|
|
};
|
2016-12-05 16:30:31 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// for a number of days "n" return dates object for the past n days
|
|
|
|
getLastNDates(n) {
|
|
|
|
return _.range(n).map(
|
|
|
|
i => moment().subtract(i, 'days').startOf('day')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMoreDays(e) {
|
|
|
|
e.preventDefault();
|
2016-12-12 15:00:56 +09:00
|
|
|
const numberOfDays = getSetting('numberOfDays', 5);
|
2017-01-09 21:14:25 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
note: loadMoreBefore is used when doing incremental loading,
|
|
|
|
but we're not doing that anymore so we only need to change 'after'
|
|
|
|
*/
|
|
|
|
|
|
|
|
// const loadMoreBefore = moment(this.state.after, 'YYYY-MM-DD').subtract(1, 'days').format('YYYY-MM-DD');
|
2016-12-06 14:46:28 +09:00
|
|
|
const loadMoreAfter = moment(this.state.after, 'YYYY-MM-DD').subtract(numberOfDays, 'days').format('YYYY-MM-DD');
|
|
|
|
this.props.loadMore({
|
2017-01-09 21:14:25 +09:00
|
|
|
...this.props.terms,
|
|
|
|
// before: loadMoreBefore,
|
|
|
|
after: loadMoreAfter,
|
2016-12-06 14:46:28 +09:00
|
|
|
});
|
2016-12-05 16:30:31 +09:00
|
|
|
this.setState({
|
2016-12-06 14:46:28 +09:00
|
|
|
days: this.state.days + this.props.increment,
|
|
|
|
after: loadMoreAfter,
|
2016-12-05 16:30:31 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const posts = this.props.results;
|
|
|
|
|
|
|
|
const postsByDates = this.getLastNDates(this.state.days).map(date => {
|
|
|
|
return {
|
|
|
|
date,
|
|
|
|
posts: _.filter(posts, post => {
|
|
|
|
// console.log(post)
|
|
|
|
// console.log(moment(post.postedAt).startOf('day'))
|
|
|
|
// console.log(date)
|
|
|
|
return moment(new Date(post.postedAt)).startOf('day').isSame(date, 'day')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="posts-daily">
|
2016-12-06 18:06:29 +01:00
|
|
|
<Components.PostsListHeader />
|
2017-01-09 21:14:25 +09:00
|
|
|
{postsByDates.map((day, index) => <Components.PostsDay key={index} number={index} {...day} loading={true} />)}
|
2016-12-10 21:58:40 +09:00
|
|
|
<a className="posts-load-more-days" onClick={this.loadMoreDays}><FormattedMessage id="posts.load_more_days"/></a>
|
2016-12-05 16:30:31 +09:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PostsDailyList.propTypes = {
|
|
|
|
days: React.PropTypes.number,
|
|
|
|
increment: React.PropTypes.number
|
|
|
|
};
|
|
|
|
|
|
|
|
PostsDailyList.defaultProps = {
|
2016-12-12 15:00:56 +09:00
|
|
|
days: getSetting('numberOfDays', 5),
|
|
|
|
increment: getSetting('numberOfDays', 5)
|
2016-12-05 16:30:31 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
collection: Posts,
|
|
|
|
queryName: 'postsDailyListQuery',
|
2016-12-18 19:04:11 +09:00
|
|
|
fragment: getRawComponent('PostsList').fragment,
|
2016-12-10 21:58:40 +09:00
|
|
|
limit: 0,
|
2016-12-05 16:30:31 +09:00
|
|
|
};
|
|
|
|
|
2016-12-06 18:06:29 +01:00
|
|
|
registerComponent('PostsDailyList', PostsDailyList, withList(options));
|