2015-11-20 20:21:54 +01:00
|
|
|
var React = require('react')
|
|
|
|
var sortedIndex = require('lodash.sortedindex')
|
|
|
|
|
2015-11-22 00:10:46 +01:00
|
|
|
module.exports = function(boardsAPI){
|
|
|
|
var Post = require('post.jsx')(boardsAPI)
|
2015-11-20 20:21:54 +01:00
|
|
|
return React.createClass({
|
|
|
|
getInitialState: function(){
|
2015-11-22 00:10:46 +01:00
|
|
|
return { posts: [], api: false }
|
2015-11-20 20:21:54 +01:00
|
|
|
},
|
|
|
|
sortFn: function(a,b){
|
|
|
|
return (b.date || 0) - (a.date || 0)
|
|
|
|
},
|
2015-11-22 00:10:46 +01:00
|
|
|
init: function(boards){
|
|
|
|
this.setState({ api: true })
|
2015-11-20 20:21:54 +01:00
|
|
|
boards.getPostsInBoard(this.props.admin,this.props.board)
|
|
|
|
.on('post in '+this.props.board+'@'+this.props.admin,(post,hash) => {
|
|
|
|
if(!this.isMounted()) return true
|
2015-11-22 00:10:46 +01:00
|
|
|
var now = (new Date()).getTime()
|
2015-11-20 20:21:54 +01:00
|
|
|
var posts = this.state.posts
|
|
|
|
if(post.date === undefined || post.date <= 0){
|
|
|
|
posts.push(post)
|
|
|
|
} else if(post.date <= now){
|
|
|
|
var i = sortedIndex(posts,post,(p) => now-p.date || now)
|
|
|
|
posts.splice(i,0,post)
|
|
|
|
} else {
|
|
|
|
console.log('Post discarded cause date in the future:',post)
|
|
|
|
}
|
|
|
|
this.setState({ posts })
|
|
|
|
})
|
|
|
|
},
|
2015-11-22 00:10:46 +01:00
|
|
|
componentDidMount: function(){
|
|
|
|
boardsAPI.use(boards => {
|
|
|
|
if(boards.isInit) this.init(boards)
|
|
|
|
else boards.getEventEmitter().on('init',err => {
|
|
|
|
if(!err && this.isMounted()) this.init(boards)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
getPosts: function(){
|
|
|
|
if(this.state.posts.length > 0 || this.state.api){
|
|
|
|
return this.state.posts.map(post => {
|
|
|
|
return <Post key={post.title+post.text} post={post} />
|
|
|
|
})
|
|
|
|
} else return <div className="center-block text-center">
|
|
|
|
<Icon name="refresh" className="fa-3x center-block light fa-spin" />
|
|
|
|
</div>
|
|
|
|
},
|
2015-11-20 20:21:54 +01:00
|
|
|
render: function(){
|
|
|
|
return (
|
|
|
|
<div className="postList">
|
|
|
|
{this.state.posts.map(post => {
|
|
|
|
return <Post key={post.title+post.text} post={post} />
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|