Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import Form from 'react-bootstrap/Form'
  3. import { Container } from 'react-bootstrap';
  4. import { Button } from 'react-bootstrap';
  5. import Gun from 'gun/gun'
  6. import SEA from 'gun/sea'
  7.  
  8. const formattadas = tadas => Object.keys(tadas)
  9. .map(key => ({ key, val: tadas[key] }))
  10. .filter(t => Boolean(t.val) && t.key !== '_')
  11.  
  12. export default class Auth extends Component {
  13. constructor({ gun }) {
  14. super()
  15. this.gun = gun
  16. this.user = gun.user()
  17. this.state = {
  18. ipfsPub: '96969',
  19. ipfsPriv: '123',
  20. said: '',
  21. newTada: '',
  22. tadas: []
  23. }
  24. }
  25.  
  26. componentDidMount() {
  27. this.user.auth(this.state.ipfsPub, this.state.ipfsPriv, (ack) => { if (ack.err) console.log('FUCK! ' + ack.err) });
  28. this.gun.on('auth', () => this.thing());
  29. }
  30.  
  31. thing() {
  32. console.log('on auth triggered');
  33. this.gun.get(tadas => this.setState({ tadas: formattadas(tadas) }));
  34. }
  35.  
  36. /* handleAuth() {
  37. this.user.auth(this.state.ipfsPub, this.state.ipfsPriv, (ack) => { if (ack.err) console.log(ack.err) });
  38. let statement = this.user.get('said');
  39. this.setState({ said: statement });
  40. }
  41.  
  42. handlePub(event) {
  43. var value = event.target.value
  44. this.setState({ ipfsPub: value })
  45. }
  46.  
  47. handlePriv(event) {
  48. var value = event.target.value
  49. this.setState({ ipfsPriv: value })
  50. } */
  51.  
  52. handleChange = e => this.setState({ newTada: e.target.value })
  53.  
  54. add = e => {
  55. e.preventDefault()
  56. this.gun.get('tadas').set(this.state.newTada)
  57. this.setState({ newTada: '' })
  58. console.log("Add attempted")
  59. }
  60.  
  61. del = key => this.gun.path(key).put(null)
  62.  
  63. render() {
  64. return <>
  65. <p />
  66. <Container>
  67. <div>Gun</div>
  68. <div>
  69. <form onSubmit={this.add}>
  70. <input value={this.state.newTada} onChange={this.handleChange} />
  71. <button>Add</button>
  72. </form>
  73. <br />
  74. <ul>
  75. {this.state.tadas.map(tada => <li key={tada.key} onClick={_ => this.del(tada.key)}>{tada.val}</li>)}
  76. </ul>
  77. </div>
  78. </Container></>
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement