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

136 lines
5.1 KiB
React
Raw Normal View History

2015-12-16 18:12:25 +01:00
var React = require('react')
var GetIPFS = require('getipfs.jsx')
var Icon = require('icon.jsx')
var Link = require('react-router').Link
var { Error, Loading, Saving } = require('status-components.jsx')
2015-12-16 18:12:25 +01:00
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)
}
})
},
init (boards) {
if (this.state.init) return
this.setState({ api: boards, init: true })
this.getBoardSettings(boards)
},
getBoardSettings (boards) {
2015-12-16 23:05:48 +01:00
if (!this.props.params.boardname) return
2015-12-16 19:15:06 +01:00
this.setState({ loading: true })
2015-12-16 23:05:48 +01:00
boards.getBoardSettings(boards.getMyID(), this.props.params.boardname, (err, s) => {
2015-12-16 19:15:06 +01:00
if (err) {
this.setState({ error: err, loading: false })
2015-12-16 23:05:48 +01:00
} else if (this.state.loading) {
2015-12-16 19:15:06 +01:00
this.setState({
2015-12-16 23:05:48 +01:00
id: this.props.params.boardname,
2015-12-16 19:15:06 +01:00
name: s.fullname,
desc: s.description,
loading: false
})
}
})
2015-12-16 18:12:25 +01:00
},
handleChange (event) {
var obj = {}
obj[event.target.id] = event.target.value
this.setState(obj)
},
skip () {
this.setState({ loading: false, updating: false, error: false })
},
refresh () {
this.setState({ loading: true })
boardsAPI.use(b => this.getBoardSettings(b))
},
save () {
var boards = this.state.api
var board = {
2015-12-16 23:05:48 +01:00
id: this.state.shortname || this.props.params.boardname,
2015-12-16 18:12:25 +01:00
fullname: this.state.name,
2015-12-16 23:05:48 +01:00
description: this.state.desc
2015-12-16 18:12:25 +01:00
}
this.setState({ updating: true })
boards.createBoard(board, (err) => {
2015-12-16 19:15:06 +01:00
this.setState({ updating: false })
2015-12-16 18:12:25 +01:00
console.log('CREATE:', err)
})
},
additionalButtons () {
2015-12-16 23:05:48 +01:00
if (this.state.api && this.props.params.boardname) {
var url = '/@' + this.state.api.getMyID() + '/' + this.props.params.boardname
2015-12-16 18:12:25 +01:00
return <span>
<button onClick={this.refresh} className="button not-first">Refresh</button>
<Link to={url} className="button not-first">View</Link>
</span>
} else {
return <span></span>
}
},
render () {
if (this.state.api) {
if (this.state.error) {
return <Error error={this.state.error} >
<button className="button button-primary center-block" onClick={this.skip}>Continue</button>
</Error>
2015-12-16 18:12:25 +01:00
} else if (this.state.loading) {
return <Loading title="Fetching your current Board Settings...">
<button className="button button-primary center-block" onClick={this.skip}>Skip</button>
</Loading>
2015-12-16 18:12:25 +01:00
} else if (this.state.updating) {
return <Saving>
<p>Pressing the Skip button will not abort the publish operation.</p>
<button className="button button-primary center-block" onClick={this.skip}>Skip</button>
</Saving>
2015-12-16 18:12:25 +01:00
} else {
return (
<div className="editor">
2015-12-17 16:18:07 +01:00
<h2><Icon name="inbox" className="light" />
{this.props.params.boardname ? ' Board Settings' : ' New Board'}
</h2>
2015-12-16 18:12:25 +01:00
<p>This App uses IPFS to store your Boards. When you are offline,
other users or servers that viewed your content will serve it to
others.</p>
<p><b>Warning:</b> due to a bug in go-ipfs, it may take up to a minute
for your changes to be visibile. Your Boards will appear unchanged during
this time.</p>
<div className="row">
2015-12-16 23:05:48 +01:00
{this.props.params.boardname
? <div></div>
: <div className="six columns">
<label htmlFor="shortname">ID</label>
<input className="u-full-width" type="text" id="shortname" value={this.state.id} onChange={this.handleChange} placeholder="Choose a short ID." />
</div>}
<div className={(this.props.params.boardname ? 'twelve' : 'six') + ' columns'}>
2015-12-16 18:12:25 +01:00
<label htmlFor="name">Title</label>
2015-12-16 23:05:48 +01:00
<input className="u-full-width" type="text" id="name" value={this.state.name} onChange={this.handleChange} placeholder="Name your board" />
2015-12-16 18:12:25 +01:00
</div>
</div>
<div>
<label htmlFor="desc">Description</label>
2015-12-16 23:05:48 +01:00
<textarea className="u-full-width" id="desc" value={this.state.desc} onChange={this.handleChange} placeholder="What's this Board about? Markdown is supported :)" />
2015-12-16 18:12:25 +01:00
</div>
<div className="buttons">
<button className="button button-primary" onClick={this.save}>Publish</button>
{this.additionalButtons()}
</div>
</div>
)
}
} else return <GetIPFS api={this.state.api} />
}
})
}