do remove contributor

This commit is contained in:
Jonathan Rainville 2019-04-10 16:12:47 -04:00
parent 7c308ef261
commit 8963427f64
No known key found for this signature in database
GPG Key ID: 5F4630B759727D9C
3 changed files with 48 additions and 5 deletions

View File

@ -7,7 +7,7 @@ import {required, isAddress} from '../validators';
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faTrash} from "@fortawesome/free-solid-svg-icons";
import {addContributor, getFormattedContributorList} from '../services/Meritocracy';
import {addContributor, getFormattedContributorList, removeContributor} from '../services/Meritocracy';
import './admin.scss';
@ -42,6 +42,10 @@ class Admin extends React.Component {
this.setState({busy: true, successMsg: ''});
try {
await addContributor(this.state.contributorName, this.state.contributorAddress);
const contributorList = this.state.contributorList;
contributorList.push({label: this.state.contributorName, value: this.state.contributorAddress});
this.setState({busy: false, successMsg: 'Contributor added!'});
} catch (e) {
this.setState({error: e.message || e, busy: false});
@ -53,8 +57,19 @@ class Admin extends React.Component {
this.setState({focusedContributorIndex: contributorIndex, showDeleteModal: true});
};
doRemove = () => {
this.setState({focusedContributorIndex: -1, showDeleteModal: false});
doRemove = async () => {
const idx = this.state.focusedContributorIndex;
this.setState({focusedContributorIndex: -1, showDeleteModal: false, busy: true});
try {
await removeContributor(this.state.contributorList[idx].value);
const contributorList = this.state.contributorList;
contributorList.splice(idx, 1);
this.setState({contributorList, busy: false, successMsg: 'Contributor removed!'});
} catch (e) {
this.setState({error: e.message || e, busy: false});
}
};
handleClose = () => {

View File

@ -28,6 +28,33 @@ export function addContributor(name, address) {
});
}
export function removeContributor(address) {
return new Promise(async (resolve, reject) => {
const mainAccount = web3.eth.defaultAccount;
try {
const registry = await Meritocracy.methods.getRegistry().call({from: mainAccount});
let index = registry.indexOf(address);
const list = await getContributorList();
const idx = list.findIndex(contributor => contributor.value === address);
list.splice(idx, 1);
const newHash = await saveContributorList(list);
const removeContributor = Meritocracy.methods.removeContributor(index, newHash);
let gas = await removeContributor.estimateGas({from: mainAccount});
const receipt = await removeContributor.send({from: mainAccount, gas: gas + 1000});
resolve(receipt);
} catch (e) {
const message = 'Error removing contributor';
console.error(message);
console.error(e);
reject(message);
}
});
}
export function getContributorList(hash) {
return new Promise(async (resolve, reject) => {
try {
@ -113,6 +140,7 @@ export async function getContributor(_address) {
export function saveContributorList(list) {
return new Promise(async (resolve, reject) => {
try {
contributorList = list;
const newHash = await EmbarkJS.Storage.saveText(JSON.stringify(list));
resolve(newHash);
} catch (e) {

View File

@ -230,7 +230,7 @@ contract Meritocracy {
// Locals
uint256 registryLength = registry.length - 1;
// Requirements
require(idx < registryLength); // idx needs to be smaller than registry.length - 1 OR maxContributors
require(idx <= registryLength); // idx needs to be smaller than registry.length - 1 OR maxContributors
// Body
address c = registry[idx];
// Swap & Pop!