1+ import EmbarkJS from 'Embark/EmbarkJS' ;
2+ import React from 'react' ;
3+ import { Alert , Form , FormGroup , FormControl , HelpBlock , Button } from 'react-bootstrap' ;
4+
5+ class Storage extends React . Component {
6+
7+ constructor ( props ) {
8+ super ( props ) ;
9+
10+ this . state = {
11+ textToSave : 'hello world!' ,
12+ generatedHash : '' ,
13+ loadText : '' ,
14+ storedText : '' ,
15+ fileToUpload : null ,
16+ fileHash : '' ,
17+ imageToDownload : '' ,
18+ url : '' ,
19+ logs : [ ] ,
20+ storageError : ''
21+ } ;
22+ }
23+
24+ handleChange ( e , name ) {
25+ this . state [ name ] = e . target . value ;
26+ this . setState ( this . state ) ;
27+ }
28+
29+ handleFileUpload ( e ) {
30+ this . setState ( { fileToUpload : [ e . target ] } ) ;
31+ }
32+
33+ addToLog ( txt ) {
34+ this . state . logs . push ( txt ) ;
35+ this . setState ( { logs : this . state . logs } ) ;
36+ }
37+
38+ setText ( e ) {
39+ e . preventDefault ( ) ;
40+
41+ EmbarkJS . Storage . saveText ( this . state . textToSave )
42+ . then ( ( hash ) => {
43+ this . setState ( {
44+ generatedHash : hash ,
45+ loadText : hash ,
46+ storageError : ''
47+ } ) ;
48+ this . addToLog ( "EmbarkJS.Storage.saveText('" + this . state . textToSave + "').then(function(hash) { })" ) ;
49+ } )
50+ . catch ( ( err ) => {
51+ if ( err ) {
52+ this . setState ( { storageError : err . message } ) ;
53+ console . log ( "Storage saveText Error => " + err . message ) ;
54+ }
55+ } ) ;
56+ }
57+
58+ loadHash ( e ) {
59+ e . preventDefault ( ) ;
60+
61+ EmbarkJS . Storage . get ( this . state . loadText )
62+ . then ( ( content ) => {
63+ this . setState ( { storedText : content , storageError : '' } ) ;
64+ this . addToLog ( "EmbarkJS.Storage.get('" + this . state . loadText + "').then(function(content) { })" ) ;
65+ } )
66+ . catch ( ( err ) => {
67+ if ( err ) {
68+ this . setState ( { storageError : err . message } )
69+ console . log ( "Storage get Error => " + err . message ) ;
70+ }
71+ } ) ;
72+ }
73+
74+ uploadFile ( e ) {
75+ e . preventDefault ( ) ;
76+
77+ EmbarkJS . Storage . uploadFile ( this . state . fileToUpload )
78+ . then ( ( hash ) => {
79+ this . setState ( {
80+ fileHash : hash ,
81+ imageToDownload : hash ,
82+ storageError : ''
83+ } ) ;
84+ this . addToLog ( "EmbarkJS.Storage.uploadFile(this.state.fileToUpload).then(function(hash) { })" ) ;
85+ } )
86+ . catch ( ( err ) => {
87+ if ( err ) {
88+ this . setState ( { storageError : err . message } ) ;
89+ console . log ( "Storage uploadFile Error => " + err . message ) ;
90+ }
91+ } ) ;
92+ }
93+
94+ loadFile ( e ) {
95+ let _url = EmbarkJS . Storage . getUrl ( this . state . imageToDownload ) ;
96+ this . setState ( { url : _url } )
97+ this . addToLog ( "EmbarkJS.Storage.getUrl('" + this . state . imageToDownload + "')" ) ;
98+ }
99+
100+ render ( ) {
101+ return < React . Fragment >
102+ {
103+ ! this . props . enabled ?
104+ < React . Fragment >
105+ < Alert bsStyle = "warning" > The node you are using does not support IPFS. Please ensure < a href = "https://github.com/ipfs/js-ipfs-api#cors" target = "_blank" > CORS</ a > is setup for the IPFS node.</ Alert >
106+ </ React . Fragment > : ''
107+ }
108+ {
109+ this . state . storageError !== '' ?
110+ < Alert bsStyle = "danger" > { this . state . storageError } </ Alert >
111+ : ''
112+ }
113+ < h3 > Save text to storage</ h3 >
114+ < Form inline >
115+ < FormGroup >
116+ < FormControl
117+ type = "text"
118+ defaultValue = { this . state . textToSave }
119+ onChange = { e => this . handleChange ( e , 'textToSave' ) } />
120+ < Button bsStyle = "primary" onClick = { ( e ) => this . setText ( e ) } > Save Text</ Button >
121+ < HelpBlock > generated Hash: < span className = "textHash" > { this . state . generatedHash } </ span > </ HelpBlock >
122+ </ FormGroup >
123+ </ Form >
124+
125+ < h3 > Load text from storage given an hash</ h3 >
126+ < Form inline >
127+ < FormGroup >
128+ < FormControl
129+ type = "text"
130+ value = { this . state . loadText }
131+ onChange = { e => this . handleChange ( e , 'loadText' ) } />
132+ < Button bsStyle = "primary" onClick = { ( e ) => this . loadHash ( e ) } > Load</ Button >
133+ < HelpBlock > result: < span className = "textHash" > { this . state . storedText } </ span > </ HelpBlock >
134+ </ FormGroup >
135+ </ Form >
136+
137+ < h3 > Upload file to storage</ h3 >
138+ < Form inline >
139+ < FormGroup >
140+ < FormControl
141+ type = "file"
142+ onChange = { ( e ) => this . handleFileUpload ( e ) } />
143+ < Button bsStyle = "primary" onClick = { ( e ) => this . uploadFile ( e ) } > Upload</ Button >
144+ < HelpBlock > generated hash: < span className = "fileHash" > { this . state . fileHash } </ span > </ HelpBlock >
145+ </ FormGroup >
146+ </ Form >
147+
148+ < h3 > Get file or image from storage</ h3 >
149+ < Form inline >
150+ < FormGroup >
151+ < FormControl
152+ type = "text"
153+ value = { this . state . imageToDownload }
154+ onChange = { e => this . handleChange ( e , 'imageToDownload' ) } />
155+ < Button bsStyle = "primary" onClick = { ( e ) => this . loadFile ( e ) } > Download</ Button >
156+ < HelpBlock > file available at: < span > < a href = { this . state . url } target = "_blank" > { this . state . url } </ a > </ span > </ HelpBlock >
157+ < HelpBlock > < img src = { this . state . url } /> </ HelpBlock >
158+ </ FormGroup >
159+ </ Form >
160+
161+ < p > Javascript calls being made: </ p >
162+ < div className = "logs" >
163+ < p > EmbarkJS.Storage.setProvider('ipfs',{ '{' } server: 'localhost', port: '5001'{ '}' } )</ p >
164+ {
165+ this . state . logs . map ( ( item , i ) => < p key = { i } > { item } </ p > )
166+ }
167+ </ div >
168+ </ React . Fragment > ;
169+ }
170+ }
171+
172+ export default Storage ;
0 commit comments