hackathon-registration-dapp/app/js/index.js

169 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

2018-10-03 13:36:13 +00:00
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';
2018-10-03 12:45:56 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
2018-10-03 13:36:13 +00:00
import web3 from 'Embark/web3'
import SNTGiveaway from 'Embark/contracts/SNTGiveaway';
2018-10-03 18:28:33 +00:00
import SNT from 'Embark/contracts/SNT';
2018-10-05 01:55:45 +00:00
import axios from 'axios';
2018-10-07 15:51:27 +00:00
import config from './config';
2018-10-03 18:28:33 +00:00
window.SNTGiveaway = SNTGiveaway;
2018-10-03 18:28:33 +00:00
window.SNT = SNT;
2018-10-19 19:34:00 +00:00
2018-10-03 13:36:13 +00:00
class App extends React.Component {
constructor(props) {
super(props);
2018-10-04 14:09:29 +00:00
this.state = {
error: false,
errorMessage: '',
2018-10-05 01:55:45 +00:00
ready: false,
2018-10-06 18:50:15 +00:00
showENSLink: false,
intervalCheck: false,
2018-10-20 11:37:20 +00:00
showError: false,
noCode: false
2018-10-04 14:09:29 +00:00
};
2018-10-03 13:36:13 +00:00
}
2018-10-03 14:13:40 +00:00
componentDidMount(){
EmbarkJS.onReady(async (error) => {
2018-10-19 17:55:12 +00:00
const accounts = await web3.eth.getAccounts();
if(!accounts.length){
this.setState({error: true, errorMessage: "No accounts are available"});
return;
}
web3.eth.defaultAccount = accounts[0];
2018-10-03 14:13:40 +00:00
if(error){
this.setState({error: true, errorMessage: "Error loading the DAPP - Contact your nearest Status Core Developer"});
2018-10-19 19:34:00 +00:00
alert(error);
2018-10-03 14:13:40 +00:00
return;
}
2018-10-05 01:55:45 +00:00
try {
await this.start();
} catch (error) {
2018-10-19 15:32:16 +00:00
if(error.response && error.response.data){
this.setState({error: true, errorMessage: error.response.data});
} else {
this.setState({error: true, errorMessage: error.message});
}
2018-10-05 01:55:45 +00:00
return;
}
2018-10-19 22:15:16 +00:00
});
}
2018-10-06 18:50:15 +00:00
async redirectIfProcessed(code, intervalCheck){
2018-10-19 17:55:12 +00:00
const sentToAddress = await SNTGiveaway.methods.sentToAddress(web3.eth.defaultAccount).call({from: web3.eth.defaultAccount});
const usedCode = await SNTGiveaway.methods.codeUsed( '0x' + code,).call({from: web3.eth.defaultAccount});
2018-10-05 02:31:39 +00:00
if(sentToAddress && usedCode){
2018-10-06 18:50:15 +00:00
this.setState({showENSLink: true, intervalCheck});
setTimeout(() => {
2018-10-07 15:51:27 +00:00
window.location = config.ENSDappURL;
2018-10-06 18:50:15 +00:00
}, 7000);
}
}
2018-10-05 01:55:45 +00:00
async start(){
2018-10-07 15:51:27 +00:00
const code = location.search.replace("?", '');
const accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
if(!code) {
2018-10-20 11:37:20 +00:00
this.setState({noCode: true});
return;
}
2018-10-07 15:51:27 +00:00
2018-10-05 01:55:45 +00:00
await this.redirectIfProcessed(code);
2018-10-05 01:55:45 +00:00
if(!this.state.showENSLink){
2018-10-07 15:51:27 +00:00
const response = await axios.get(config.APIServer + '/isProcessing/' + code);
2018-10-05 01:55:45 +00:00
if(!response.data.result){
const record = {
code,
2018-10-07 15:51:27 +00:00
address: web3.eth.defaultAccount
2018-10-05 01:55:45 +00:00
}
2018-10-07 15:51:27 +00:00
const response = await axios.post(config.APIServer + '/requestFunds/', record);
2018-10-05 02:31:39 +00:00
if(response.data.error){
this.setState({error: true, errorMessage: response.data.message});
}
2018-10-05 01:55:45 +00:00
// TODO: avoid people spamming with localstorage
}
setInterval(async () => {
2018-10-06 18:50:15 +00:00
await this.redirectIfProcessed(code, true);
2018-10-05 01:55:45 +00:00
}, 10000);
}
this.setState({ready: true});
2018-10-03 14:13:40 +00:00
}
2018-10-06 18:50:15 +00:00
showErrorMessage(){
this.setState({showErrorMessage: !this.state.showErrorMessage});
}
2018-10-03 13:36:13 +00:00
render(){
2018-10-20 11:37:20 +00:00
const {error, errorMessage, ready, showENSLink, showErrorMessage, intervalCheck, noCode} = this.state;
2018-10-06 18:50:15 +00:00
2018-10-20 11:37:20 +00:00
let showImage = true;
2018-10-06 18:50:15 +00:00
let className = "blue";
if(error) className = "red";
if(!ready && !error) className = "blue";
if(ready && !error && showENSLink && !intervalCheck) className = "yellow";
if(ready && !error && showENSLink && intervalCheck) className = "green";
let messageTitle;
let messageBody;
switch(className){
case "blue":
2018-10-20 11:37:20 +00:00
if(noCode){
showImage = false;
messageTitle = "This is the #CryptoLife Registration DApp";
messageBody = <p>Please scan one of the QR codes provided at the entrance to the event in order to receive some ETH and SNT so you can buy an ENS username and pay for food and drinks!</p>;
} else {
messageTitle = "Transaction is being processed";
messageBody = <Fragment><p>You will shortly receive some ETH and SNT, just for attending #CryptoLife! Use it to register an ENS Name and buy food.</p><p>We hope you have a wonderful time bringing crypto closer to life with us.</p></Fragment>
}
2018-10-06 18:50:15 +00:00
break;
case "red":
2018-10-15 17:14:39 +00:00
messageTitle = "Shit, there was an error";
2018-10-06 18:50:15 +00:00
messageBody = <p>Please ask a volunteer for help in person.</p>;
break;
case "yellow":
2018-10-06 21:22:28 +00:00
messageTitle = "You've already been funded!";
2018-10-06 18:50:15 +00:00
messageBody = <p>We're redirecting you to ENS to register your own username! Ask a volunteer for help if you get lost.</p>
break;
case "green":
2018-10-06 21:22:28 +00:00
messageTitle = "Great success! Such future!";
2018-10-06 18:50:15 +00:00
messageBody = <p>We're redirecting you to ENS to register your own username! Ask a volunteer for help if you get lost.</p>
break;
}
2018-10-06 18:50:15 +00:00
return <div className={className + " container"}>
2018-10-20 11:37:20 +00:00
<div id="image" className={showImage?'':'no-show'}>
2018-10-06 18:50:15 +00:00
</div>
<div className="message-title" onClick={(e) => this.showErrorMessage()}>
{messageTitle}
</div>
<div className="message-body">
{messageBody}
</div>
{ error && showErrorMessage && <div className="errorMessage">{errorMessage}</div> }
</div>
2018-10-03 13:36:13 +00:00
}
}
2018-10-03 12:45:56 +00:00
2018-10-03 13:36:13 +00:00
ReactDOM.render(<App></App>, document.getElementById('app'));