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

59 lines
1.9 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')
2015-11-20 20:21:54 +01:00
module.exports = function(){
var Post = require('post.jsx')()
2015-11-20 20:21:54 +01:00
return React.createClass({
getInitialState: function(){
return { posts: [], api: false }
2015-11-20 20:21:54 +01:00
},
sortFn: function(a,b){
return (b.date || 0) - (a.date || 0)
},
init: function(boards){
this.setState({ api: true })
2015-11-20 20:21:54 +01:00
boards.getPostsInBoard(this.props.admin,this.props.board)
2015-11-26 18:21:28 +01:00
.on('post in '+this.props.board+(this.props.admin?'@'+this.props.admin:''),(post,hash) => {
2015-11-20 20:21:54 +01:00
if(!this.isMounted()) return true
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 })
})
},
componentDidMount: function(){
var boards = this.props.api
if(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.hash} board={this.props.board} admin={this.props.admin} post={post} 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-11-20 20:21:54 +01:00
render: function(){
return (
<div className="postList">
2015-11-23 13:00:49 +01:00
{this.getPosts()}
2015-11-20 20:21:54 +01:00
</div>
)
}
})
}