WIP add contributor

This commit is contained in:
Jonathan Rainville 2019-04-09 16:43:09 -04:00
parent 610c1b2542
commit a3c296d3ce
No known key found for this signature in database
GPG Key ID: 5F4630B759727D9C
2 changed files with 69 additions and 10 deletions

View File

@ -1,31 +1,42 @@
/*global web3*/
import React from 'react';
import {Button, Form} from 'react-bootstrap';
import {Button, Form, Alert} from 'react-bootstrap';
import ValidatedForm from 'react-validation/build/form';
import Input from 'react-validation/build/input';
import {required, isAddress} from '../validators';
import {addContributor} from '../services/Meritocracy';
class Admin extends React.Component {
state = {
contributorName: '',
contributorAddress: ''
contributorAddress: '',
busy: false,
error: ''
};
onChange = (name, e) => {
this.setState({[name]: e.target.value});
};
addContributor = (e) => {
addContributor = async (e) => {
e.preventDefault();
console.log('Submit', this.state);
this.setState({busy: true});
try {
await addContributor(this.state.contributorName, this.state.contributorAddress);
this.setState({contributorName: '', contributorAddress: '', busy: false});
} catch (e) {
this.setState({error: e.message || e, busy: false});
}
};
render() {
const {contributorAddress, contributorName} = this.state;
const {contributorAddress, contributorName, error, busy} = this.state;
return (<div>
<h2>Admin Panel</h2>
{error && <Alert variant="danger">{error}</Alert>}
{busy && <Alert variant="primary">Working...</Alert>}
<h3>Add a contributor</h3>
<ValidatedForm onSubmit={(e) => this.addContributor(e)}>
<Form.Group controlId="formContributor">

View File

@ -1,20 +1,50 @@
/*global web3*/
import Meritocracy from 'Embark/contracts/Meritocracy';
import EmbarkJS from 'Embark/EmbarkJS';
import axios from 'axios';
const IPFS_HASH = 'QmfWJJYFBJReu2rzTDzkBKXHazE52GVWrTcVNKdcupnxNH';
const IPNS_HASH = 'QmPW4ZGXXvVYxC7Uez62m9yYZZYVHmo98c8rP6Hu1nb1Na';
const mainAccount = web3.eth.defaultAccount;
export function addContributor(name, address) {
Meritocracy.methods.addContributor(address)
return new Promise(async (resolve, reject) => {
try {
const addContributor = Meritocracy.methods.addContributor(address);
let gas = await addContributor.estimateGas({from: mainAccount});
console.log({gas});
const receipt = await addContributor.send({from: mainAccount, gas: gas + 1000});
console.log({receipt});
const list = await getContributorList();
list.push({label: name, value: address});
console.log({list});
await saveContributorList(list);
resolve();
} catch (e) {
const message = 'Error adding contributor';
console.error(message);
console.error(e);
reject(message);
}
});
}
export function getContributorList() {
return new Promise(async (resolve, reject) => {
try {
const url = EmbarkJS.Storage.getUrl(IPFS_HASH);
console.log('Url', url);
// TODO figure out how to make IPFS/IPNS work
const hash = await EmbarkJS.Storage.resolve(IPNS_HASH, (err, hash) => {
console.log('Resolved??', {err, hash});
});
console.log({hash});
const url = await EmbarkJS.Storage.getUrl(hash);
console.log({url});
const response = await axios.get(url);
console.log(response.data);
resolve(response.data.contributors);
} catch (e) {
const message = 'Error getting contributor file on IPFS';
@ -24,3 +54,21 @@ export function getContributorList() {
}
});
}
export function saveContributorList(list) {
return new Promise(async (resolve, reject) => {
try {
const url = await EmbarkJS.Storage.getUrl(IPNS_HASH);
console.log('Url', url);
console.log('saving', {contributors: list});
const response = await axios.post(url, {contributors: list});
console.log(response.data);
resolve(response.data.contributors);
} catch (e) {
const message = 'Error saving contributor file on IPFS';
console.error(message);
console.error(e);
reject(message);
}
});
}