embark/dapps/templates/demo/app/components/storage.js

274 lines
8.8 KiB
JavaScript
Raw Normal View History

2018-04-24 20:21:33 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
import React, {Fragment} from 'react';
import {Alert, Form, FormGroup, Input, FormText, Button} from 'reactstrap';
2018-07-30 11:49:03 +00:00
2018-04-24 20:21:33 +00:00
class Storage extends React.Component {
2018-07-30 11:49:03 +00:00
constructor(props) {
super(props);
this.state = {
textToSave: 'hello world!',
generatedHash: '',
loadText: '',
storedText: '',
fileToUpload: null,
fileHash: '',
imageToDownload: '',
url: '',
logs: [],
storageError: '',
2018-07-30 16:06:20 +00:00
valueRegister: '',
valueResolver: ''
2018-07-30 11:49:03 +00:00
};
}
handleChange(e, name) {
this.state[name] = e.target.value;
this.setState(this.state);
}
2018-08-15 17:12:52 +00:00
checkEnter(e, func) {
if (e.key !== 'Enter') {
return;
}
e.preventDefault();
func.apply(this, [e]);
}
2018-07-30 11:49:03 +00:00
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,
2018-04-27 13:04:34 +00:00
storageError: ''
2018-07-30 11:49:03 +00:00
});
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) {
2018-07-30 11:49:03 +00:00
let _url = EmbarkJS.Storage.getUrl(this.state.imageToDownload);
this.setState({url: _url});
this.addToLog("EmbarkJS.Storage.getUrl('" + this.state.imageToDownload + "')");
}
ipnsRegister(e) {
e.preventDefault();
2018-07-30 16:06:20 +00:00
this.setState({ registering: true, responseRegister: false });
2018-08-22 09:11:57 +00:00
this.addToLog("EmbarkJS.Storage.register(this.state.ipfsHash).then(function(hash) { })");
EmbarkJS.Storage.register(this.state.valueRegister, (err, name) => {
2018-07-30 16:06:20 +00:00
let responseRegister;
let isRegisterError = false;
2018-07-30 11:49:03 +00:00
if (err) {
2018-07-30 16:06:20 +00:00
isRegisterError = true;
responseRegister = "Name Register Error: " + (err.message || err);
2018-07-30 16:06:20 +00:00
} else {
responseRegister = name;
2018-07-30 11:49:03 +00:00
}
this.setState({
2018-07-30 16:06:20 +00:00
registering: false,
2018-07-30 16:22:44 +00:00
responseRegister,
isRegisterError
2018-07-30 11:49:03 +00:00
});
});
}
ipnsResolve(e) {
e.preventDefault();
2018-07-30 16:06:20 +00:00
this.setState({ resolving: true, responseResolver: false });
2018-08-22 09:11:57 +00:00
this.addToLog("EmbarkJS.Storage.resolve(this.state.ipnsName, function(err, path) { })");
EmbarkJS.Storage.resolve(this.state.valueResolver, (err, path) => {
2018-07-30 16:06:20 +00:00
let responseResolver;
let isResolverError = false;
2018-07-30 11:49:03 +00:00
if (err) {
2018-07-30 16:06:20 +00:00
isResolverError = true;
responseResolver = "Name Resolve Error: " + (err.message || err);
2018-07-30 16:06:20 +00:00
} else {
responseResolver = path;
2018-07-30 11:49:03 +00:00
}
this.setState({
2018-07-30 16:06:20 +00:00
resolving: false,
2018-07-30 16:22:44 +00:00
responseResolver,
isResolverError
2018-07-30 11:49:03 +00:00
});
});
}
2018-08-24 08:07:05 +00:00
isIpfs(){
2018-08-24 13:17:37 +00:00
return EmbarkJS.Storage.currentProviderName === 'ipfs';
2018-08-24 08:07:05 +00:00
}
2018-07-30 11:49:03 +00:00
render() {
return <React.Fragment>
2019-08-26 15:12:42 +00:00
{!this.props.enabled &&
<React.Fragment>
2019-08-26 15:12:42 +00:00
<Alert color="warning">The node you are using does not support IPFS. If
you haven't explicitly disabled IPFS in <code>config/storage.js</code> then
please ensure <a href="https://github.com/ipfs/js-ipfs-api#cors"
target="_blank">CORS</a> is setup for the IPFS node.</Alert>
</React.Fragment>}
{this.state.storageError !== '' &&
<Alert color="danger">{this.state.storageError}</Alert>}
2018-07-30 11:49:03 +00:00
<h3>Save text to storage</h3>
<Form onKeyDown={(e) => this.checkEnter(e, this.setText)}>
<FormGroup className="inline-input-btn">
<Input
2018-07-30 11:49:03 +00:00
type="text"
defaultValue={this.state.textToSave}
onChange={e => this.handleChange(e, 'textToSave')}/>
<Button color="primary" onClick={(e) => this.setText(e)}>Save Text</Button>
{this.state.generatedHash && <FormText>Generated Hash: <span className="textHash font-weight-bold">{this.state.generatedHash}</span></FormText>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
<h3>Load text from storage given an hash</h3>
<Form onKeyDown={(e) => this.checkEnter(e, this.loadHash)}>
<FormGroup className="inline-input-btn">
<Input
2018-07-30 11:49:03 +00:00
type="text"
value={this.state.loadText}
onChange={e => this.handleChange(e, 'loadText')}/>
<Button color="primary" onClick={(e) => this.loadHash(e)}>Load</Button>
{this.state.storedText && <FormText>Result: <span className="textHash font-weight-bold">{this.state.storedText}</span></FormText>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
<h3>Upload file to storage</h3>
<Form>
2018-07-30 11:49:03 +00:00
<FormGroup>
<Input
2018-07-30 11:49:03 +00:00
type="file"
onChange={(e) => this.handleFileUpload(e)}/>
<Button color="primary" onClick={(e) => this.uploadFile(e)} className="mt-2">Upload</Button>
{this.state.fileHash && <FormText>Generated hash: <span className="fileHash">{this.state.fileHash}</span></FormText>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
<h3>Get file or image from storage</h3>
<Form onKeyDown={(e) => this.checkEnter(e, this.loadFile)}>
<FormGroup className="inline-input-btn">
<Input
2018-07-30 11:49:03 +00:00
type="text"
value={this.state.imageToDownload}
onChange={e => this.handleChange(e, 'imageToDownload')}/>
<Button color="primary" onClick={(e) => this.loadFile(e)}>Download</Button>
{this.state.url && <Fragment>
<FormText>
File available at: <span><a href={this.state.url} target="_blank">{this.state.url}</a></span>
</FormText>
<FormText><img alt="file image" src={this.state.url}/></FormText>
</Fragment>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
{!this.isIpfs() && <Alert color="warning">The 2 functions below are only available with IPFS</Alert>}
2018-08-23 08:59:02 +00:00
2018-07-30 11:49:03 +00:00
<h3>Register to IPNS</h3>
<Form onKeyDown={(e) => this.checkEnter(e, this.ipnsRegister)}>
<FormGroup className="inline-input-btn">
<Input
2018-07-30 11:49:03 +00:00
type="text"
2018-07-30 16:06:20 +00:00
value={this.state.valueRegister}
onChange={e => this.handleChange(e, 'valueRegister')}/>
<Button color="primary" onClick={(e) => this.ipnsRegister(e)}>
2018-07-30 16:06:20 +00:00
{this.state.registering ? 'Registering...' : 'Register' }
2018-07-30 11:49:03 +00:00
</Button>
<FormText>It will take around 1 minute</FormText>
2018-07-30 16:06:20 +00:00
{this.state.responseRegister &&
<Alert className="alert-result" color={this.state.isRegisterError ? 'danger' : 'success'}>
2018-07-30 16:06:20 +00:00
<span className="value">{this.state.responseRegister}</span>
</Alert>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
<h3>Resolve name</h3>
<Form onKeyDown={(e) => this.checkEnter(e, this.ipnsResolve)}>
<FormGroup className="inline-input-btn">
<Input
2018-07-30 11:49:03 +00:00
type="text"
2018-07-30 16:06:20 +00:00
value={this.state.valueResolver}
onChange={e => this.handleChange(e, 'valueResolver')}/>
<Button color="primary" onClick={(e) => this.ipnsResolve(e)}>
2018-07-30 16:06:20 +00:00
{this.state.resolving ? 'Resolving...' : 'Resolve' }
2018-07-30 11:49:03 +00:00
</Button>
<FormText>It will take around 1 minute</FormText>
2018-07-30 16:06:20 +00:00
{this.state.responseResolver &&
<Alert className="alert-result" color={this.state.isResolverError ? 'danger' : 'success'}>
2018-07-30 16:06:20 +00:00
<span className="value">{this.state.responseResolver}</span>
</Alert>}
2018-07-30 11:49:03 +00:00
</FormGroup>
</Form>
<p>Javascript calls being made: </p>
<div className="logs">
<p>EmbarkJS.Storage.setProvider('ipfs',{'{'}server: 'localhost', port: '5001'{'}'})</p>
{
this.state.logs.map((item, i) => <p key={i}>{item}</p>)
}
</div>
</React.Fragment>;
}
2018-04-24 20:21:33 +00:00
}
2018-07-30 11:49:03 +00:00
export default Storage;