1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-26 15:04:19 +01:00
ipfs-boards/webapp/pages/commentpage.jsx

84 lines
3.3 KiB
React
Raw Normal View History

2015-11-26 19:35:34 +01:00
var React = require('react')
var Link = require('react-router').Link
var Icon = require('icon.jsx')
var UserID = require('userID.jsx')
var GetIPFS = require('getipfs.jsx')
var Comment = require('comment.jsx').Comment
2015-11-26 19:35:34 +01:00
module.exports = function (boardsAPI) {
2015-11-26 19:35:34 +01:00
return React.createClass({
getInitialState: function () {
2015-11-26 19:35:34 +01:00
return { parent: false, api: false }
},
componentDidMount: function () {
2015-11-26 19:35:34 +01:00
boardsAPI.use(boards => {
2015-12-23 13:19:23 +01:00
boards.getEventEmitter().on('init', (err, limited) => {
if ((!err || limited) && this.isMounted()) {
2015-11-26 19:35:34 +01:00
this.init(boards)
}
})
2015-12-23 13:19:23 +01:00
if (boards.isInit || boards.limited) {
2015-11-26 19:35:34 +01:00
this.init(boards)
}
})
},
componentWillReceiveProps: function (nextProps) {
2015-12-23 11:47:31 +01:00
if (this.props.params !== nextProps.params) {
2015-12-23 13:19:23 +01:00
boardsAPI.use(boards => this.init(boards, nextProps))
2015-12-23 11:47:31 +01:00
}
},
downloadComment: function (boards, props) {
boards.downloadComment(props.params.commenthash, props.params.userid, props.params.boardname, (err, comment) => {
if (err) {
this.setState({
comment: { title: 'Error', text: err.Message || err.Error }
})
2015-11-26 19:35:34 +01:00
} else {
2015-12-23 11:47:31 +01:00
if (!comment.parent && comment.text) {
// Redirect to post page
this.props.history.push('/@' + this.props.params.userid + '/' + this.props.params.boardname + '/' + this.props.params.commenthash)
} else {
this.setState({ comment })
}
2015-11-26 19:35:34 +01:00
}
})
},
2015-12-23 13:19:23 +01:00
init: function (boards, props) {
this.setState({ comment: false, boards, api: true, allowReply: boards.isInit && !boards.limited })
this.downloadComment(boards, props || this.props)
},
getContext: function () {
if (this.props.params.userid) {
if (this.props.params.boardname) {
return <div>Comment by <UserID id={this.props.params.userid} api={this.state.boards} /> in <Link to={'@' + this.props.params.userid + '/' + this.props.params.boardname}>#{this.props.params.boardname}</Link> to <Link to={'/@' + this.props.params.userid + '/' + this.props.params.boardname + '/' + this.props.params.posthash }>{this.props.params.posthash}</Link></div>
} else {
return <div>Comment by <UserID id={this.props.params.userid} api={this.state.boards} /></div>
}
2015-11-26 19:35:34 +01:00
} else return <div><h6 className="light">You are viewing a single comment</h6></div>
},
showComment: function () {
if (this.state.comment) {
2015-12-23 13:19:23 +01:00
console.log('allowReply', this.state.allowReply)
return <Comment allowReply={this.state.allowReply} comment={this.state.comment} post={this.props.params.posthash} adminID={this.props.params.userid} board={this.props.params.boardname} showParent={true} api={this.state.boards} />
2015-11-26 19:35:34 +01:00
} else {
2015-12-12 13:21:14 +01:00
return <div className="center-block text-center find-content">
2015-11-26 19:35:34 +01:00
<Icon name="refresh" className="fa-3x center-block light fa-spin" />
<h4>Finding content...</h4>
</div>
}
},
render: function () {
if (this.state.api) {
2015-11-26 19:35:34 +01:00
return <div className="comment-page">
<div className="text-center">
{this.getContext()}
</div>
{this.showComment()}
</div>
} else {
return <GetIPFS api={this.state.boards} />
}
2015-11-26 19:35:34 +01:00
}
})
}