var React = require('react')
var GetIPFS = require('getipfs.jsx')
var Icon = require('icon.jsx')
var Link = require('react-router').Link
var { Error, Loading, Saving, Success } = require('status-components.jsx')
module.exports = function (boardsAPI) {
return React.createClass({
getInitialState () {
return { }
},
componentDidMount () {
boardsAPI.use(boards => {
boards.init()
boards.getEventEmitter().on('init', err => {
if (!err && this.isMounted()) {
this.init(boards)
}
})
if (this.isMounted() && boards.isInit) {
this.init(boards)
}
})
},
componentWillReceiveProps (props) {
if (this.props.params.posthash !== props.params.posthash) {
boardsAPI.use(this.init)
}
},
init (boards) {
this.setState({ api: boards, userid: boards.getMyID(), downloaded: false, title: undefined, text: undefined })
if (this.props.params.posthash) this.downloadPost(boards)
},
downloadPost (boards) {
this.setState({ loading: true })
boards.downloadPost(this.props.params.posthash, (err, hash, date, post) => {
if (err) {
this.setState({ error: err, loading: false, downloaded: false })
} else {
console.log(post)
this.setState({ loading: false, title: post.title, text: post.text, downloaded: true })
}
})
},
handleChange (event) {
var obj = {}
obj[event.target.id] = event.target.value
this.setState(obj)
},
skip () {
this.setState({ loading: false, updating: false, error: false, success: false })
},
refresh () {
boardsAPI.use(b => this.downloadPost(b))
},
save () {
this.setState({ updating: true })
var post = {
text: this.state.text
}
if (this.state.title && this.state.title.length > 0) {
post.title = this.state.title
}
if (this.props.params.posthash) {
// TODO: maybe check if downloaded? But then what if the user skipped the prev version download?
post.previous = this.props.params.posthash
}
boardsAPI.use(boards => {
boards.createPost(post, this.props.params.boardname, (err, hash) => {
this.setState({ error: err, updating: false })
if (!err) {
var url = '/edit/board/' + this.props.params.boardname + '/post/' + hash
this.props.history.push(url)
}
})
})
},
delete () {
this.setState({ deleting: true })
boardsAPI.use(boards => {
boards.deletePost(this.props.params.posthash, this.props.params.boardname, err => {
if (!err) console.log('Post deleted')
this.setState({ deleting: false, error: err, success: true })
})
})
},
additionalButtons () {
if (this.state.api && this.props.params.posthash) {
var url = '/@' + this.state.api.getMyID() + '/' + this.props.params.boardname + '/' + this.props.params.posthash
return
View
} else {
return
}
},
render () {
if (this.state.api) {
if (this.state.error) {
return Pressing the Skip button will not abort the Delete operation. Pressing the Skip button will not abort the publish operation. Note: due to a bug in go-ipfs, you may need to wait up to a minute for changes to appear.
This App uses IPFS to store your Posts. When you are offline, other users or servers that viewed your text will serve it to others.
Warning: due to a bug in go-ipfs, it may take up to a minute for your changes to be visibile. Your Post will not appear or appear unchanged during this time.