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

81 lines
2.9 KiB
React
Raw Normal View History

2015-11-20 20:21:54 +01:00
var React = require('react')
var Icon = require('icon.jsx')
module.exports = function (boardsAPI) {
2015-11-20 20:21:54 +01:00
return React.createClass({
getDefaults: function () {
return { addr: 'localhost', port: 5001, api: false }
2015-11-20 20:21:54 +01:00
},
getInitialState: function () {
boardsAPI.use(boards => {
if (boards.isInit && this.isMounted()) this.setState({ api: true })
boards.getEventEmitter().on('init', err => {
if (!err && this.isMounted()) this.setState({ api: true })
})
})
var s = window.localStorage.getItem('ipfs-boards-settings')
2015-11-20 20:21:54 +01:00
var obj = this.getDefaults()
try {
obj = JSON.parse(s)
} catch (e) {
window.localStorage.removeItem('ipfs-boards-settings')
2015-11-20 20:21:54 +01:00
}
return obj || this.getDefaults()
},
save: function () {
if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) {
window.alert('Port number invalid')
2015-11-20 20:21:54 +01:00
} else {
window.localStorage.setItem('ipfs-boards-settings', JSON.stringify({
2015-11-20 20:21:54 +01:00
addr: this.state.addr,
port: parseInt(this.state.port, 10)
2015-11-20 20:21:54 +01:00
}))
window.location.reload(false)
2015-11-20 20:21:54 +01:00
}
},
setDefaults: function () {
2015-11-20 20:21:54 +01:00
this.setState(this.getDefaults())
},
onChange: function (event) {
if (event.target.id === 'nodeAddress') {
2015-11-20 20:21:54 +01:00
this.setState({ addr: event.target.value })
} else {
this.setState({ port: event.target.value })
}
},
isOK: function () {
if (this.state.api) {
return <div className="itsok light">
<h5><Icon name="check" /> It's OK</h5>
<p>You're connected to IPFS</p>
</div>
}
},
render: function () {
2015-11-20 20:21:54 +01:00
return (
<div className="settings">
<h2><Icon name="cog"/> Settings</h2>
2015-11-21 17:06:14 +01:00
<h5>Choose how the prototype connects to IPFS</h5>
<p>In the future, this won't be necessary because IPFS will run in your browser.</p>
<p>All settings are saved in your browser's localStorage.</p>
2015-11-20 20:21:54 +01:00
<div className="row">
<div className="six columns">
<label htmlFor="nodeAddress">IPFS Node</label>
<input className="u-full-width" type="text" id="nodeAddress" value={this.state.addr} onChange={this.onChange} placeholder="localhost" />
</div>
<div className="six columns">
<label htmlFor="nodePort">API Port</label>
<input className="u-full-width" type="text" id="nodePort" value={this.state.port} onChange={this.onChange} placeholder="5001" />
</div>
</div>
{this.isOK()}
2015-11-20 20:21:54 +01:00
<div className="buttons">
<button className="button button-primary" onClick={this.save}>Save</button>
<button className="button not-first" onClick={this.setDefaults}>Defaults</button>
</div>
</div>
)
}
})
}