subspace/examples/embark-dapp/app/App.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-08-20 18:35:58 +00:00
/* global web3 */
import React from 'react';
import EmbarkJS from 'Embark/EmbarkJS';
import Phoenix 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-08-21 18:51:13 +00:00
const phoenix = new Phoenix(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;
}
await phoenix.init()
observables.items = phoenix
.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;