diff --git a/templates/demo/app/components/storage.js b/templates/demo/app/components/storage.js new file mode 100644 index 000000000..d800868b5 --- /dev/null +++ b/templates/demo/app/components/storage.js @@ -0,0 +1,174 @@ +import EmbarkJS from 'Embark/EmbarkJS'; +import React from 'react'; +import { Alert, Form, FormGroup, FormControl, HelpBlock, Button } from 'react-bootstrap'; + +class Storage extends React.Component { + + constructor(props) { + super(props); + + this.state = { + textToSave: 'hello world!', + generatedHash: '', + loadText: '', + storedText: '', + fileToUpload: null, + fileHash: '', + imageToDownload: '', + url: '', + logs: [] + }; + + this.setText = this.setText.bind(this); + this.loadHash = this.loadHash.bind(this); + this.uploadFile = this.uploadFile.bind(this); + this.handleFileUpload = this.handleFileUpload.bind(this); + this.loadFile = this.loadFile.bind(this); + } + + handleChange(e, name){ + this.state[name] = e.target.value; + this.setState(this.state); + } + + handleFileUpload(e){ + this.setState({ fileToUpload: [e.target] }); + } + + _addToLog(txt){ + this.state.logs.push(txt); + this.setState({logs: this.state.logs}); + } + + setText(e){ + e.preventDefault(); + + let _this = this; + + EmbarkJS.Storage.saveText(this.state.textToSave) + .then(function(hash) { + _this.setState({ + generatedHash: hash, + loadText: hash + }); + _this._addToLog("EmbarkJS.Storage.saveText('" + _this.state.textToSave + "').then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("Storage saveText Error => " + err.message); + } + }); + } + + loadHash(e){ + e.preventDefault(); + + let _this = this; + + EmbarkJS.Storage.get(this.state.loadText) + .then(function(content) { + _this.setState({storedText: content}); + _this._addToLog("EmbarkJS.Storage.get('" + _this.state.loadText + "').then(function(content) { })"); + }) + .catch(function(err) { + if(err){ + console.log("Storage get Error => " + err.message); + } + }); + } + + uploadFile(e){ + e.preventDefault(); + + let _this = this; + + EmbarkJS.Storage.uploadFile(this.state.fileToUpload) + .then(function(hash) { + _this.setState({ + fileHash: hash, + imageToDownload: hash + }); + _this._addToLog("EmbarkJS.Storage.uploadFile(this.state.fileToUpload).then(function(hash) { })"); + }) + .catch(function(err) { + if(err){ + console.log("Storage uploadFile Error => " + err.message); + } + }); + } + + loadFile(e){ + let _url = EmbarkJS.Storage.getUrl(this.state.imageToDownload); + this.setState({url: _url}) + this._addToLog("EmbarkJS.Storage.getUrl('" + this.state.imageToDownload + "')"); + } + + render(){ + return + { + !this.props.enabled ? + + The node you are using does not support IPFS. Please ensure CORS is setup for the IPFS node. + : '' + } + + Save text to storage + + + this.handleChange(e, 'textToSave')} /> + Save Text + generated Hash: {this.state.generatedHash} + + + + Load text from storage given an hash + + + this.handleChange(e, 'loadText')} /> + Load + result: {this.state.storedText} + + + + Upload file to storage + + + + Upload + generated hash: {this.state.fileHash} + + + + Get file or image from storage + + + this.handleChange(e, 'imageToDownload')} /> + Download + file available at: {this.state.url} + + + + + Javascript calls being made: + + EmbarkJS.Storage.setProvider('ipfs',{'{'}server: 'localhost', port: '5001'{'}'}) + { + this.state.logs.map((item, i) => {item}) + } + + ; + } +} + +export default Storage; \ No newline at end of file diff --git a/templates/demo/app/dapp.js b/templates/demo/app/dapp.js index d2e70b096..de6b4084f 100644 --- a/templates/demo/app/dapp.js +++ b/templates/demo/app/dapp.js @@ -5,6 +5,7 @@ import { Tabs, Tab } from 'react-bootstrap'; import EmbarkJS from 'Embark/EmbarkJS'; import Blockchain from './components/blockchain'; import Whisper from './components/whisper'; +import Storage from './components/storage'; import './dapp.css'; @@ -14,14 +15,16 @@ class App extends React.Component { super(props); this.state = { - whisperEnabled: false + whisperEnabled: false, + storageEnabled: false } } componentDidMount(){ - // TODO Verify if whisper & swarm are available + // TODO Verify if whisper & swarm/ipfs are available this.setState({ - whisperEnabled: false + whisperEnabled: false, + storageEnabled: false }); } @@ -40,8 +43,8 @@ class App extends React.Component { - - Tab 2 content + +
Javascript calls being made:
EmbarkJS.Storage.setProvider('ipfs',{'{'}server: 'localhost', port: '5001'{'}'})
{item}