2019-08-20 18:35:58 +00:00
|
|
|
/* global web3 */
|
|
|
|
import React from 'react';
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
2019-09-25 12:55:05 +00:00
|
|
|
import Subspace from 'phoenix';
|
2019-09-05 19:26:37 +00:00
|
|
|
import Ranking from '../embarkArtifacts/contracts/Ranking';
|
|
|
|
import { scan, map } from 'rxjs/operators';
|
|
|
|
import RankItem from './RankItem';
|
2019-08-20 18:35:58 +00:00
|
|
|
|
2019-09-25 12:55:05 +00:00
|
|
|
const subspace = new Subspace(web3.currentProvider);
|
2019-08-20 18:35:58 +00:00
|
|
|
|
|
|
|
const observables = {};
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
state = {
|
2019-08-20 18:42:23 +00:00
|
|
|
ready: false
|
2019-08-20 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(){
|
2019-08-21 18:51:13 +00:00
|
|
|
EmbarkJS.onReady(async (err) => {
|
|
|
|
if(err){
|
|
|
|
console.error(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-25 12:55:05 +00:00
|
|
|
await subspace.init()
|
2019-08-21 18:51:13 +00:00
|
|
|
|
2019-09-25 12:55:05 +00:00
|
|
|
observables.items = subspace
|
2019-08-21 18:51:13 +00:00
|
|
|
.trackEvent(Ranking, 'Rating', {filter: {}, fromBlock: 1})
|
|
|
|
.pipe(
|
|
|
|
scan((acc, curr) => {
|
|
|
|
const votes = {...acc[curr.addr]} || {};
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[curr.addr]: {
|
|
|
|
upvotes: (votes.upvotes || 0) + (curr.rating >= 3 ? 1 : 0),
|
|
|
|
downvotes: (votes.downvotes || 0) + (curr.rating < 3 ? 1 : 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, {}),
|
|
|
|
map(summary => {
|
|
|
|
return Object.keys(summary).map(k => {
|
|
|
|
return {addr: k, ...summary[k]}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
);
|
2019-08-20 20:17:23 +00:00
|
|
|
|
|
|
|
this.setState({ready: true});
|
2019-08-20 18:35:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-21 18:51:13 +00:00
|
|
|
upvote = address => () => {
|
|
|
|
Ranking.methods.rate(address, 5).send();
|
|
|
|
}
|
2019-08-20 18:35:58 +00:00
|
|
|
|
2019-08-21 18:51:13 +00:00
|
|
|
downvote = address => () => {
|
|
|
|
Ranking.methods.rate(address, 1).send();
|
|
|
|
}
|
2019-08-20 18:35:58 +00:00
|
|
|
|
2019-08-21 18:51:13 +00:00
|
|
|
render() {
|
|
|
|
const {ready} = this.state;
|
|
|
|
if(!ready) return <span>Loading...</span>;
|
2019-09-05 19:26:37 +00:00
|
|
|
return <RankItem items={observables.items} onUpvote={this.upvote} onDownvote={this.downvote} />;
|
2019-08-21 18:51:13 +00:00
|
|
|
}
|
2019-08-20 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 19:26:37 +00:00
|
|
|
export default App;
|