yapi/client/containers/News/NewsTimeline/NewsTimeline.js

130 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-07-17 12:15:21 +08:00
import React, { Component } from 'react'
2017-08-09 16:08:37 +08:00
import { Timeline, Spin } from 'antd'
2017-07-17 12:15:21 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2017-08-09 16:08:37 +08:00
import {formatTime} from '../../../common.js';
import { fetchNewsData } from '../../../reducer/modules/news.js'
2017-07-19 16:15:51 +08:00
2017-08-09 16:08:37 +08:00
function timeago(timestamp) {
let minutes, hours, days, seconds, mouth, year;
const timeNow = parseInt(new Date().getTime()/1000);
seconds = timeNow - timestamp;
if(seconds > 86400*30*12){
year = parseInt(seconds / (86400*30*12));
}else{
year = 0;
}
if(seconds > 86400*30){
mouth = parseInt(seconds / (86400*30));
}else{
mouth = 0;
}
if(seconds > 86400){
days = parseInt(seconds / (86400));
}else{
days = 0;
}
if(seconds > 3600){
hours = parseInt(seconds / (3600));
}else{
hours = 0;
}
minutes = parseInt(seconds / 60);
if(year > 0){
return year + "年前";
}else if(mouth > 0 && year <= 0){
return mouth + "月前";
}else if (days > 0 && mouth <= 0) {
return days + "天前";
} else if (days <= 0 && hours > 0) {
return hours + "小时前";
} else if (hours <= 0 && minutes > 0) {
return minutes + "分钟前";
} else if (minutes <= 0 && seconds > 0) {
if(seconds < 30){
return "刚刚";
}else{
return seconds + "秒前";
}
} else {
return new Date(timestamp);
}
2017-07-19 16:15:51 +08:00
}
2017-08-09 16:08:37 +08:00
// timeago(new Date().getTime() - 40);
2017-07-19 16:15:51 +08:00
@connect(
2017-08-09 16:08:37 +08:00
state => {
return {
newsData: state.news.newsData,
curpage: state.news.curpage
}
},
{
2017-08-09 16:08:37 +08:00
fetchNewsData: fetchNewsData
2017-08-09 18:00:45 +08:00
}
)
2017-07-17 12:15:21 +08:00
class NewsTimeline extends Component {
static propTypes = {
2017-07-20 16:30:55 +08:00
newsData: PropTypes.object,
2017-08-09 16:08:37 +08:00
fetchNewsData: PropTypes.func,
2017-08-09 18:00:45 +08:00
2017-07-20 16:30:55 +08:00
setLoading: PropTypes.func,
2017-08-09 16:08:37 +08:00
loading: PropTypes.bool,
curpage: PropTypes.number
2017-07-17 12:15:21 +08:00
}
2017-08-07 19:34:34 +08:00
2017-07-17 12:15:21 +08:00
constructor(props) {
2017-07-17 18:15:55 +08:00
super(props);
2017-07-20 16:30:55 +08:00
this.state = {
2017-08-09 16:08:37 +08:00
bidden: "",
loading: false
}
2017-07-17 12:15:21 +08:00
}
2017-07-20 16:30:55 +08:00
2017-08-09 16:08:37 +08:00
getMore(){
2017-07-20 16:30:55 +08:00
const that = this;
2017-08-09 16:08:37 +08:00
this.setState({loading: true});
2017-08-11 17:49:47 +08:00
this.props.fetchNewsData(21,'project', this.props.curpage, 8).then(function () {
2017-08-09 16:08:37 +08:00
that.setState({loading: false});
if(that.props.newsData.total + 1 === that.props.curpage){
that.setState({bidden: "logbidden"})
}
})
}
componentWillMount() {
2017-08-11 17:49:47 +08:00
this.props.fetchNewsData(21,'project', this.props.curpage, 8)
2017-07-19 16:15:51 +08:00
}
2017-08-09 16:08:37 +08:00
render() {
let data = this.props.newsData?this.props.newsData.list:[];
if(data && data.length){
data = data.map(function (item, i) {
return (<Timeline.Item key = {i}>
<span className="logoTimeago">{timeago(item.add_time)}</span>
<span className="logusername">{item.username}</span>
<span className="logtype">{item.type}</span>
<span className="logtime">{formatTime(item.add_time)}</span>
<span className="logcontent">{item.content}</span>
</Timeline.Item>);
});
}else{
data = "";
}
let pending = this.state.bidden?<a className={this.state.bidden}>以上为全部内容</a>:<a className="loggetMore" onClick = {this.getMore.bind(this)}></a>;
if(this.state.loading){
pending = <Spin />
}
2017-07-17 12:15:21 +08:00
return (
<section className="news-timeline">
2017-08-09 16:08:37 +08:00
{ data?<Timeline pending={pending}>{data}</Timeline>:data }
2017-07-17 12:15:21 +08:00
</section>
)
}
}
export default NewsTimeline