/ test_apps / test_app / app / js / index.js
index.js
  1  /*globals $, SimpleStorage, document*/
  2  
  3  import React, { Component } from 'react';
  4  import EmbarkJS from 'Embark/EmbarkJS';
  5  import SimpleStorage from 'Embark/contracts/SimpleStorage';
  6  import Test from 'Embark/contracts/Test';
  7  //import Assert from 'Embark/contracts/Assert';
  8  
  9  import SimpleStorageTest from 'Embark/contracts/SimpleStorageTest';
 10  window.SimpleStorageTest = SimpleStorageTest
 11  
 12  import ReactDOM from 'react-dom';
 13  
 14  //import $ from './_vendor/jquery.min';
 15  import $ from 'jquery';
 16  //import 'bootstrap';
 17  import 'bootstrap/dist/css/bootstrap.css';
 18  import 'bootstrap/dist/css/bootstrap-theme.css';
 19  //import 'bootstrap/dist/css/bootstrap.min.css';
 20  import 'bootstrap/dist/js/bootstrap.min.js';
 21  
 22  //import 'react-bootstrap/dist/react-bootstrap.min.js';
 23  
 24  import { Navbar, Jumbotron, Button } from 'react-bootstrap';
 25  
 26  window.EmbarkJS = EmbarkJS;
 27  window.SimpleStorage = SimpleStorage;
 28  window.Test = Test;
 29  //window.Assert = Assert;
 30  
 31  window.React = React;
 32  
 33  import './foo.css';
 34  
 35  import App from './app.js';
 36  
 37  ReactDOM.render(<App />, document.getElementById('root'));
 38  
 39  var addToLog = function(id, txt) {
 40    $(id + " .logs").append("<br>" + txt);
 41  };
 42  
 43  // ===========================
 44  // Blockchain example
 45  // ===========================
 46  $(document).ready(function() {
 47    console.log([1,2,3].map(v => v + 1));
 48  
 49    $("#blockchain button.set").click(function() {
 50      var value = parseInt($("#blockchain input.text").val(), 10);
 51  
 52      // If web3.js 1.0 is being used
 53      if (EmbarkJS.isNewWeb3()) {
 54        SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000});
 55        addToLog("#blockchain", "SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000})");
 56      } else {
 57        SimpleStorage.set(value);
 58        addToLog("#blockchain", "SimpleStorage.set(" + value + ")");
 59      }
 60  
 61    });
 62  
 63    $("#blockchain button.get").click(function() {
 64      // If web3.js 1.0 is being used
 65      if (EmbarkJS.isNewWeb3()) {
 66        SimpleStorage.methods.get().call(function(err, value) {
 67          $("#blockchain .value").html(value);
 68        });
 69        addToLog("#blockchain", "SimpleStorage.methods.get(console.log)");
 70      } else {
 71        SimpleStorage.get().then(function(value) {
 72          $("#blockchain .value").html(value.toNumber());
 73        });
 74        addToLog("#blockchain", "SimpleStorage.get()");
 75      }
 76    });
 77  
 78  });
 79  
 80  // ===========================
 81  // Storage (IPFS) example
 82  // ===========================
 83  $(document).ready(function() {
 84    // automatic set if config/storage.json has "enabled": true and "provider": "ipfs"
 85    //EmbarkJS.Storage.setProvider('ipfs',{server: 'localhost', port: '5001'});
 86  
 87    $("#storage .error").hide();
 88    //EmbarkJS.Storage.ipfsConnection.version()
 89    //  .then(function(){
 90          $("#status-storage").addClass('status-online');
 91          $("#storage-controls").show();
 92     // })
 93     // .catch(function(err) {
 94     //   if(err){
 95     //     console.log("IPFS Connection Error => " + err.message);
 96     //     $("#storage .error").show();
 97     //     $("#status-storage").addClass('status-offline');
 98     //     $("#storage-controls").hide();
 99     //   }
100     // });
101  
102    $("#storage button.setIpfsText").click(function() {
103      var value = $("#storage input.ipfsText").val();
104      EmbarkJS.Storage.saveText(value).then(function(hash) {
105        $("span.textHash").html(hash);
106        $("input.textHash").val(hash);
107        addToLog("#storage", "EmbarkJS.Storage.saveText('" + value + "').then(function(hash) { })");
108      })
109      .catch(function(err) {
110        if(err){
111          console.log("IPFS saveText Error => " + err.message);
112        }
113      });
114    });
115  
116    $("#storage button.loadIpfsHash").click(function() {
117      var value = $("#storage input.textHash").val();
118      EmbarkJS.Storage.get(value).then(function(content) {
119        $("span.ipfsText").html(content);
120        addToLog("#storage", "EmbarkJS.Storage.get('" + value + "').then(function(content) { })");
121      })
122      .catch(function(err) {
123        if(err){
124          console.log("IPFS get Error => " + err.message);
125        }
126      });
127    });
128  
129    $("#storage button.uploadFile").click(function() {
130      var input = $("#storage input[type=file]");
131      EmbarkJS.Storage.uploadFile(input).then(function(hash) {
132        $("span.fileIpfsHash").html(hash);
133        $("input.fileIpfsHash").val(hash);
134        addToLog("#storage", "EmbarkJS.Storage.uploadFile($('input[type=file]')).then(function(hash) { })");
135      })
136      .catch(function(err) {
137        if(err){
138          console.log("IPFS uploadFile Error => " + err.message);
139        }
140      });
141    });
142  
143    $("#storage button.loadIpfsFile").click(function() {
144      var hash = $("#storage input.fileIpfsHash").val();
145      var url = EmbarkJS.Storage.getUrl(hash);
146      var link = '<a href="' + url + '" target="_blank">' + url + '</a>';
147      $("span.ipfsFileUrl").html(link);
148      $(".ipfsImage").attr('src', url);
149      addToLog("#storage", "EmbarkJS.Storage.getUrl('" + hash + "')");
150    });
151  
152  });
153  
154  // ===========================
155  // Communication (Whisper) example
156  // ===========================
157  $(document).ready(function() {
158  
159    $("#communication .error").hide();
160    $("#communication .errorVersion").hide();
161    if (EmbarkJS.Messages.providerName === 'whisper') {
162      EmbarkJS.Messages.getWhisperVersion(function(err, version) {
163        if (err) {
164          $("#communication .error").show();
165          $("#communication-controls").hide();
166          $("#status-communication").addClass('status-offline');
167        } else {
168          EmbarkJS.Messages.setProvider('whisper');
169          $("#status-communication").addClass('status-online');
170        }
171      });
172    }
173  
174    $("#communication button.listenToChannel").click(function() {
175      var channel = $("#communication .listen input.channel").val();
176      $("#communication #subscribeList").append("<br> subscribed to " + channel + " now try sending a message");
177      EmbarkJS.Messages.listenTo({topic: [channel]}).then(function(message) {
178        $("#communication #messagesList").append("<br> channel: " + channel + " message: " + message);
179      });
180      addToLog("#communication", "EmbarkJS.Messages.listenTo({topic: ['" + channel + "']}).then(function(message) {})");
181    });
182  
183    $("#communication button.sendMessage").click(function() {
184      var channel = $("#communication .send input.channel").val();
185      var message = $("#communication .send input.message").val();
186      EmbarkJS.Messages.sendMessage({topic: channel, data: message});
187      addToLog("#communication", "EmbarkJS.Messages.sendMessage({topic: '" + channel + "', data: '" + message + "'})");
188    });
189  
190  });