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: [], storageError: '', valueRegister: '', valueResolver: '', }; } 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(); EmbarkJS.Storage.saveText(this.state.textToSave) .then((hash) => { this.setState({ generatedHash: hash, loadText: hash, storageError: '' }); this.addToLog("EmbarkJS.Storage.saveText('" + this.state.textToSave + "').then(function(hash) { })"); }) .catch((err) => { if (err) { this.setState({storageError: err.message}); console.log("Storage saveText Error => " + err.message); } }); } loadHash(e) { e.preventDefault(); EmbarkJS.Storage.get(this.state.loadText) .then((content) => { this.setState({storedText: content, storageError: ''}); this.addToLog("EmbarkJS.Storage.get('" + this.state.loadText + "').then(function(content) { })"); }) .catch((err) => { if (err) { this.setState({storageError: err.message}); console.log("Storage get Error => " + err.message); } }); } uploadFile(e) { e.preventDefault(); EmbarkJS.Storage.uploadFile(this.state.fileToUpload) .then((hash) => { this.setState({ fileHash: hash, imageToDownload: hash, storageError: '' }); this.addToLog("EmbarkJS.Storage.uploadFile(this.state.fileToUpload).then(function(hash) { })"); }) .catch((err) => { if (err) { this.setState({storageError: err.message}); 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 + "')"); } ipnsRegister(e) { e.preventDefault(); this.setState({ registering: true, responseRegister: false }); this.addToLog("EmbarkJS.Names.register(this.state.ipfsHash).then(function(hash) { })"); EmbarkJS.Names.register(this.state.valueRegister, (err, name) => { let responseRegister; let isRegisterError = false; if (err) { isRegisterError = true; responseRegister = "Name Register Error: " + err.message } else { responseRegister = name; } this.setState({ responseRegister: responseRegister, registering: false, isRegisterError: isRegisterError }); }); } ipnsResolve(e) { e.preventDefault(); this.setState({ resolving: true, responseResolver: false }); this.addToLog("EmbarkJS.Names.resolve(this.state.ipnsName, function(err, path) { })"); EmbarkJS.Names.resolve(this.state.valueResolver, (err, path) => { let responseResolver; let isResolverError = false; if (err) { isResolverError = true; responseResolver = "Name Resolve Error: " + err.message } else { responseResolver = path; } this.setState({ responseResolver: responseResolver, resolving: false, isResolverError: isResolverError }); }); } render() { return { !this.props.enabled ? The node you are using does not support IPFS. Please ensure CORS is setup for the IPFS node. : '' } { this.state.storageError !== '' ? {this.state.storageError} : '' }

Save text to storage

this.handleChange(e, 'textToSave')}/> generated Hash: {this.state.generatedHash}

Load text from storage given an hash

this.handleChange(e, 'loadText')}/> result: {this.state.storedText}

Upload file to storage

this.handleFileUpload(e)}/> generated hash: {this.state.fileHash}

Get file or image from storage

this.handleChange(e, 'imageToDownload')}/> file available at: {this.state.url}

Register to IPNS

this.handleChange(e, 'valueRegister')}/> It will take around 1 minute {this.state.responseRegister && {this.state.responseRegister} }

Resolve name

this.handleChange(e, 'valueResolver')}/> It will take around 1 minute {this.state.responseResolver && {this.state.responseResolver} }

Javascript calls being made:

EmbarkJS.Storage.setProvider('ipfs',{'{'}server: 'localhost', port: '5001'{'}'})

{ this.state.logs.map((item, i) =>

{item}

) }
; } } export default Storage;