This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathapp.js
83 lines (72 loc) · 1.87 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict'
const React = require('react')
const IPFS = require('ipfs')
const stringToUse = 'hello world from webpacked IPFS'
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
id: null,
version: null,
protocol_version: null,
added_file_hash: null,
added_file_contents: null
}
}
componentDidMount () {
const self = this
let node
create()
function create () {
// Create the IPFS node instance
node = new IPFS({ repo: String(Math.random() + Date.now()) })
node.once('ready', () => {
console.log('IPFS node is ready')
ops()
})
}
function ops () {
node.id((err, res) => {
if (err) {
throw err
}
self.setState({
id: res.id,
version: res.agentVersion,
protocol_version: res.protocolVersion
})
})
node.files.add([Buffer.from(stringToUse)], (err, filesAdded) => {
if (err) { throw err }
const hash = filesAdded[0].hash
self.setState({added_file_hash: hash})
node.files.cat(hash, (err, data) => {
if (err) { throw err }
self.setState({added_file_contents: data})
})
})
}
}
render () {
return (
<div style={{textAlign: 'center'}}>
<h1>Everything is working!</h1>
<p>Your ID is <strong>{this.state.id}</strong></p>
<p>Your IPFS version is <strong>{this.state.version}</strong></p>
<p>Your IPFS protocol version is <strong>{this.state.protocol_version}</strong></p>
<hr />
<div>
Added a file! <br />
{this.state.added_file_hash}
</div>
<br />
<br />
<p>
Contents of this file: <br />
{this.state.added_file_contents}
</p>
</div>
)
}
}
module.exports = App