From 08e403144287fd8dc2284af89fd4cffc58135d49 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Thu, 14 Feb 2019 13:38:30 +0100 Subject: [PATCH] step 13: introduce CreatePost component --- app/js/components/App.js | 8 +++- app/js/components/CreatePost.js | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/js/components/CreatePost.js diff --git a/app/js/components/App.js b/app/js/components/App.js index 5b3162f..cd43917 100644 --- a/app/js/components/App.js +++ b/app/js/components/App.js @@ -1,8 +1,14 @@ import React, { Component } from 'react'; +import { CreatePost } from './CreatePost'; export class App extends Component { render() { - return

DReddit

+ return ( + +

DReddit

+ +
+ ) } } diff --git a/app/js/components/CreatePost.js b/app/js/components/CreatePost.js new file mode 100644 index 0000000..0687f88 --- /dev/null +++ b/app/js/components/CreatePost.js @@ -0,0 +1,72 @@ +import React, { Component } from 'react'; +import EmbarkJS from 'Embark/EmbarkJS'; +import DReddit from 'Embark/contracts/Dreddit'; + +export class CreatePost extends Component { + + constructor(props) { + super(props); + + this.state = { + topic: '', + content: '', + loading: false + }; + } + + handleChange(field, event) { + this.setState({ + [field]: event.target.value + }); + } + + async createPost(event) { + event.preventDefault(); + + this.setState({ + loading: true + }); + + const ipfsHash = await EmbarkJS.Storage.saveText(JSON.stringify({ + topic: this.state.topic, + content: this.state.content + })); + + const accounts = await web3.eth.getAccounts(); + const createPost = await DReddit.methods.createPost(web3.utils.toHex(ipfsHash)); + const estimate = await createPost.estimateGas(); + + await createPost.send({from: accounts[0], gas: estimate}); + + this.setState({ + topic: '', + content: '', + loading: false + }); + } + + render() { + return ( +
this.createPost(e)}> +
+ + this.handleChange('topic', e)}/> +
+
+ +
+ + {this.state.loading && +

Posting...

+ } +
+ ) + } +}