Json.js
1.05 KB
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
import React, { Component } from 'react'
const formatJson = json =>
Object.keys(json)
.map(key => ({ key, val: json[key]}))
.filter(el => el.key !== '_')
export default class Json extends Component {
constructor({gun}) {
super()
this.gun = gun.get('json');
this.state = { newField: '', json: [] }
}
componentWillMount() {
this.gun.on(json => this.setState({ json: formatJson(json) }))
}
edit = key => e => {
e.preventDefault()
this.gun.path(key).put(e.target.value)
}
add = e => {
e.preventDefault()
this.gun.path(this.state.newField).put('value')
this.setState({newField: ''})
}
render() {
return <div>
<ul>
{this.state.json.map(({ key, val }) =>
<li key={key}><b>{key}:</b> <input value={val} onChange={this.edit(key)} /></li>
)}
</ul>
<form onSubmit={this.add}>
<input value={this.state.newField} onChange={e => this.setState({ newField: e.target.value})} />
<button onClick={this.add}>Add Field</button>
</form>
</div>
}
}