1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-02-03 16:24:20 +01:00
ipfs-boards/webapp/components/postlist.jsx

85 lines
2.6 KiB
React
Raw Normal View History

2015-11-20 20:21:54 +01:00
var React = require('react')
var sortedIndex = require('lodash.sortedindex')
2015-11-23 13:00:49 +01:00
var Icon = require('icon.jsx')
var Post = require('post.jsx')
2015-11-20 20:21:54 +01:00
module.exports = React.createClass({
2015-12-18 22:22:26 +01:00
getInitialState () {
2015-12-19 17:43:55 +01:00
return { posts: [] }
},
2015-12-18 22:22:26 +01:00
sortFn (a, b) {
return (b.date || 0) - (a.date || 0)
},
2015-12-18 22:22:26 +01:00
componentWillReceiveProps (props) {
2015-12-21 16:12:19 +01:00
if (props.api && (this.props.board !== props.board || this.props.admin !== props.admin)) {
2015-12-18 22:22:26 +01:00
this.init(props.api, props)
}
},
init (boards, props) {
2015-12-17 22:19:21 +01:00
var onPost = (hash, date, post) => {
if (!this.isMounted()) return true
2015-12-21 16:27:08 +01:00
var now = parseInt((new Date()).getTime() / 1000, 10)
var posts = this.state.posts
2015-12-17 22:19:21 +01:00
if (date === undefined || date <= 0) {
posts.push(hash)
} else /* if (date <= now) */ {
var i = sortedIndex(posts, post, (p) => now - date || now)
posts.splice(i, 0, hash)
} /* else {
console.log('Post discarded cause date in the future:', hash)
}*/
2015-12-21 16:44:34 +01:00
// Don't drop posts in the future due to date sync problems!
this.setState({ posts })
}
2015-12-19 17:43:55 +01:00
props = props || this.props
2015-12-18 22:22:26 +01:00
boards.getEventEmitter().on('post in ' + props.board + (props.admin ? '@' + props.admin : ''), onPost)
2015-12-19 17:43:55 +01:00
this.setState({ api: boards, limited: boards.limited })
if (boards.isInit) {
boards.getPostsInBoard(props.admin, props.board)
} else {
boards.getEventEmitter().on('init', (err, limited) => {
if (!err) {
boards.getPostsInBoard(props.admin, props.board)
} else {
this.setState({ limited })
}
})
}
},
2015-12-18 22:22:26 +01:00
componentDidMount () {
var boards = this.props.api
if (boards) {
2015-12-19 17:43:55 +01:00
if (boards.isInit || boards.limited) {
this.init(boards)
} else {
2015-12-19 17:43:55 +01:00
boards.getEventEmitter().on('init', (err, limited) => {
if ((!err || limited) && this.isMounted()) this.init(boards)
})
}
2015-11-20 20:21:54 +01:00
}
},
2015-12-18 22:22:26 +01:00
getPosts () {
if (this.state.posts.length > 0 || this.state.api) {
2015-12-17 22:19:21 +01:00
return this.state.posts.map(hash => {
return <Post key={hash} board={this.props.board} admin={this.props.admin} hash={hash} api={this.props.api} />
})
} else {
return <div className="center-block text-center">
<Icon name="refresh" className="fa-3x center-block light fa-spin" />
</div>
}
},
2015-12-18 22:22:26 +01:00
render () {
2015-12-19 17:43:55 +01:00
if (this.state.limited) {
return <div className="text-center">
<h5 className="light"><Icon name="ban" /></h5>
<p>Posts in a board can't be shown in limited mode. Sorry!</p>
</div>
} else {
return <div className="postList">
{this.getPosts()}
</div>
2015-12-19 17:43:55 +01:00
}
}
})