|
| 1 | +'use strict' |
| 2 | +const React = require('react') |
| 3 | +const ipfsAPI = require('ipfs-api') |
| 4 | + |
| 5 | +const ipfs = ipfsAPI('localhost', '5001') |
| 6 | +const stringToUse = 'hello world from webpacked IPFS' |
| 7 | + |
| 8 | +class App extends React.Component { |
| 9 | + constructor (props) { |
| 10 | + super(props) |
| 11 | + this.state = { |
| 12 | + id: null, |
| 13 | + version: null, |
| 14 | + protocol_version: null, |
| 15 | + added_file_hash: null, |
| 16 | + added_file_contents: null |
| 17 | + } |
| 18 | + } |
| 19 | + componentDidMount () { |
| 20 | + ipfs.id((err, res) => { |
| 21 | + if (err) throw err |
| 22 | + this.setState({ |
| 23 | + id: res.id, |
| 24 | + version: res.agentVersion, |
| 25 | + protocol_version: res.protocolVersion |
| 26 | + }) |
| 27 | + }) |
| 28 | + ipfs.add([new Buffer(stringToUse)], (err, res) => { |
| 29 | + if (err) throw err |
| 30 | + const hash = res[0].hash |
| 31 | + this.setState({added_file_hash: hash}) |
| 32 | + ipfs.cat(hash, (err, res) => { |
| 33 | + if (err) throw err |
| 34 | + let data = '' |
| 35 | + res.on('data', (d) => { |
| 36 | + data = data + d |
| 37 | + }) |
| 38 | + res.on('end', () => { |
| 39 | + this.setState({added_file_contents: data}) |
| 40 | + }) |
| 41 | + }) |
| 42 | + }) |
| 43 | + } |
| 44 | + render () { |
| 45 | + return <div style={{textAlign: 'center'}}> |
| 46 | + <h1>Everything is working!</h1> |
| 47 | + <p>Your ID is <strong>{this.state.id}</strong></p> |
| 48 | + <p>Your IPFS version is <strong>{this.state.version}</strong></p> |
| 49 | + <p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p> |
| 50 | + <div> |
| 51 | + <div> |
| 52 | + Added a file! <br /> |
| 53 | + {this.state.added_file_hash} |
| 54 | + </div> |
| 55 | + <div> |
| 56 | + Contents of this file: <br /> |
| 57 | + {this.state.added_file_contents} |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + } |
| 62 | +} |
| 63 | +module.exports = App |
0 commit comments