embark-area-51/lib/modules/webserver/backend/contracts/view.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-08-02 19:17:40 +00:00
/*global Web3*/
import React from 'react';
import ReactDOM from 'react-dom';
import ContractUI from './components/contract-ui';
2018-04-18 19:06:41 +00:00
const contractName = location.search.replace(/\?/, '');
let contractDefinition;
let host;
fetch("/embark/console", {
2018-08-02 19:17:40 +00:00
method: "POST",
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: "cmd=web3.currentProvider.host"
})
.then(response => response.text())
.then(text => {
2018-04-18 19:06:41 +00:00
host = text;
return fetch("/embark/contract/" + contractName);
2018-08-02 19:17:40 +00:00
})
.then(response => response.json())
.then(_contractDefinition => {
2018-04-18 19:06:41 +00:00
contractDefinition = _contractDefinition;
return fetch("/embark/files/contracts?filename=" + contractDefinition.originalFilename);
2018-08-02 19:17:40 +00:00
})
.then(response => response.text())
.then(contractSource => {
2018-04-18 19:06:41 +00:00
const web3 = new Web3(host);
window.web3 = web3;
let contractObj = new web3.eth.Contract(contractDefinition.abiDefinition);
2018-08-02 19:17:40 +00:00
contractObj.options.data = "0x" + contractDefinition.code;
contractObj.options.address = contractDefinition.deployedAddress;
2018-04-18 19:06:41 +00:00
window[contractDefinition.className] = contractObj;
ReactDOM.render(
2018-08-02 19:17:40 +00:00
<ContractUI name={contractDefinition.className} definition={contractDefinition} contract={contractObj}
source={contractSource}/>,
document.getElementById('contracts-area')
2018-04-18 19:06:41 +00:00
);
2018-08-02 19:17:40 +00:00
})
.catch(function(err) {
console.log('%cError while rendering UI', 'font-weight: bold');
console.error(err);
ReactDOM.render(
2018-08-02 19:17:40 +00:00
<div>
<h1 className="h2 mb-3">Error rendering the UI</h1>
<p className="h4 text-muted font-weight-normal mb-7">UI for &quot;&lbrace;contractName&rbrace;&quot; cannot be
generated</p>
</div>,
document.getElementById('contracts-area')
);
2018-08-02 19:17:40 +00:00
});