diff --git a/app/js/components/Admin.js b/app/js/components/Admin.js index 0900bf1..24a70c5 100644 --- a/app/js/components/Admin.js +++ b/app/js/components/Admin.js @@ -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 (

Admin Panel

+ {error && {error}} + {busy && Working...}

Add a contributor

this.addContributor(e)}> diff --git a/app/js/services/Meritocracy.js b/app/js/services/Meritocracy.js index 198ce82..ab659ce 100644 --- a/app/js/services/Meritocracy.js +++ b/app/js/services/Meritocracy.js @@ -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); + } + }); +}