mirror of
https://github.com/amark/gun.git
synced 2025-06-07 06:36:46 +00:00
Pass down gun instance so things don't break
This commit is contained in:
parent
8a95deb4fd
commit
4f44660cfd
@ -4,20 +4,25 @@ import Chat from './Chat'
|
|||||||
import Json from './Json'
|
import Json from './Json'
|
||||||
|
|
||||||
class App extends Component {
|
class App extends Component {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.gun = Gun(location.origin + '/gun')
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>React Examples</h1>
|
<h1>React Examples</h1>
|
||||||
<h2>Todo</h2>
|
<h2>Todo</h2>
|
||||||
<Todos />
|
<Todos gun={this.gun} />
|
||||||
<br />
|
<br />
|
||||||
<hr />
|
<hr />
|
||||||
<h2>Chat</h2>
|
<h2>Chat</h2>
|
||||||
<Chat />
|
<Chat gun={this.gun} />
|
||||||
<br />
|
<br />
|
||||||
<hr />
|
<hr />
|
||||||
<h2>Json</h2>
|
<h2>Json</h2>
|
||||||
<Json />
|
<Json gun={this.gun} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import Gun from 'gun'
|
import Gun from 'gun'
|
||||||
|
|
||||||
const gun = Gun(location.origin + '/gun').get('chat')
|
|
||||||
const formatMsgs = msgs => Object.keys(msgs)
|
const formatMsgs = msgs => Object.keys(msgs)
|
||||||
.map(key => ({ key, ...msgs[key] }))
|
.map(key => ({ key, ...msgs[key] }))
|
||||||
.filter(m => Boolean(m.when) && m.key !== '_')
|
.filter(m => Boolean(m.when) && m.key !== '_')
|
||||||
@ -9,8 +8,9 @@ const formatMsgs = msgs => Object.keys(msgs)
|
|||||||
.map(m => ((m.whenFmt = new Date(m.when).toLocaleString().toLowerCase()), m))
|
.map(m => ((m.whenFmt = new Date(m.when).toLocaleString().toLowerCase()), m))
|
||||||
|
|
||||||
export default class Chat extends Component {
|
export default class Chat extends Component {
|
||||||
constructor() {
|
constructor({gun}) {
|
||||||
super()
|
super()
|
||||||
|
this.gun = gun.get('chat');
|
||||||
this.state = {
|
this.state = {
|
||||||
newMsg: '',
|
newMsg: '',
|
||||||
name: (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'',
|
name: (document.cookie.match(/alias\=(.*?)(\&|$|\;)/i)||[])[1]||'',
|
||||||
@ -19,7 +19,7 @@ export default class Chat extends Component {
|
|||||||
}
|
}
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
const tmpState = {}
|
const tmpState = {}
|
||||||
gun.map().val((msg, key) => {
|
this.gun.map().val((msg, key) => {
|
||||||
tmpState[key] = msg
|
tmpState[key] = msg
|
||||||
this.setState({msgs: Object.assign({}, this.state.msgs, tmpState)})
|
this.setState({msgs: Object.assign({}, this.state.msgs, tmpState)})
|
||||||
})
|
})
|
||||||
@ -32,7 +32,7 @@ export default class Chat extends Component {
|
|||||||
document.cookie = ('alias=' + who)
|
document.cookie = ('alias=' + who)
|
||||||
const when = Gun.time.is()
|
const when = Gun.time.is()
|
||||||
const key = `${when}_${Gun.text.random(4)}`
|
const key = `${when}_${Gun.text.random(4)}`
|
||||||
gun.path(key).put({
|
this.gun.path(key).put({
|
||||||
who,
|
who,
|
||||||
when,
|
when,
|
||||||
what: this.state.newMsg,
|
what: this.state.newMsg,
|
||||||
|
@ -1,30 +1,29 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import Gun from 'gun'
|
|
||||||
|
|
||||||
const gun = Gun(location.origin + '/gun').get('json')
|
|
||||||
const formatJson = json =>
|
const formatJson = json =>
|
||||||
Object.keys(json)
|
Object.keys(json)
|
||||||
.map(key => ({ key, val: json[key]}))
|
.map(key => ({ key, val: json[key]}))
|
||||||
.filter(el => el.key !== '_')
|
.filter(el => el.key !== '_')
|
||||||
|
|
||||||
export default class Json extends Component {
|
export default class Json extends Component {
|
||||||
constructor() {
|
constructor({gun}) {
|
||||||
super()
|
super()
|
||||||
|
this.gun = gun.get('json');
|
||||||
this.state = { newField: '', json: [] }
|
this.state = { newField: '', json: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
gun.on(json => this.setState({ json: formatJson(json) }))
|
this.gun.on(json => this.setState({ json: formatJson(json) }))
|
||||||
}
|
}
|
||||||
|
|
||||||
edit = key => e => {
|
edit = key => e => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
gun.path(key).put(e.target.value)
|
this.gun.path(key).put(e.target.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
add = e => {
|
add = e => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
gun.path(this.state.newField).put('value')
|
this.gun.path(this.state.newField).put('value')
|
||||||
this.setState({newField: ''})
|
this.setState({newField: ''})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,27 +2,27 @@ import './style.css'
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import Gun from 'gun'
|
import Gun from 'gun'
|
||||||
|
|
||||||
const gun = Gun(location.origin + '/gun').get('todos')
|
|
||||||
const formatTodos = todos => Object.keys(todos)
|
const formatTodos = todos => Object.keys(todos)
|
||||||
.map(key => ({ key, val: todos[key] }))
|
.map(key => ({ key, val: todos[key] }))
|
||||||
.filter(t => Boolean(t.val) && t.key !== '_')
|
.filter(t => Boolean(t.val) && t.key !== '_')
|
||||||
|
|
||||||
export default class Todos extends Component {
|
export default class Todos extends Component {
|
||||||
constructor() {
|
constructor({gun}) {
|
||||||
super()
|
super()
|
||||||
|
this.gun = gun.get('todos');
|
||||||
this.state = {newTodo: '', todos: []}
|
this.state = {newTodo: '', todos: []}
|
||||||
}
|
}
|
||||||
componentWillMount() {
|
componentWillMount() {
|
||||||
gun.on(todos => this.setState({
|
this.gun.on(todos => this.setState({
|
||||||
todos: formatTodos(todos)
|
todos: formatTodos(todos)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
add = e => {
|
add = e => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
gun.path(Gun.text.random()).put(this.state.newTodo)
|
this.gun.path(Gun.text.random()).put(this.state.newTodo)
|
||||||
this.setState({newTodo: ''})
|
this.setState({newTodo: ''})
|
||||||
}
|
}
|
||||||
del = key => gun.path(key).put(null)
|
del = key => this.gun.path(key).put(null)
|
||||||
handleChange = e => this.setState({ newTodo: e.target.value})
|
handleChange = e => this.setState({ newTodo: e.target.value})
|
||||||
render() {
|
render() {
|
||||||
return <div>
|
return <div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user