2019-08-20 18:35:58 +00:00
|
|
|
/* global web3 */
|
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import Ranking from '../embarkArtifacts/contracts/Ranking';
|
|
|
|
|
|
|
|
import Phoenix from 'phoenix';
|
|
|
|
import withObservables from '@nozbe/with-observables'
|
|
|
|
|
2019-08-20 20:17:23 +00:00
|
|
|
const { scan, map } = require('rxjs/operators');
|
2019-08-20 18:35:58 +00:00
|
|
|
import { of } from 'rxjs';
|
|
|
|
|
2019-08-21 18:51:13 +00:00
|
|
|
const phoenix = new Phoenix(web3.currentProvider);
|
2019-08-20 18:35:58 +00:00
|
|
|
|
2019-08-20 20:17:23 +00:00
|
|
|
const RankItem = ({items, onUpvote, onDownvote}) => {
|
2019-08-21 18:51:13 +00:00
|
|
|
return items.map((item, i) => <div key={i} className="item">
|
|
|
|
<b>{i+1}</b> - {item.addr}<br />
|
|
|
|
Upvotes: {item.upvotes} - Downvotes: {item.downvotes}<br />
|
|
|
|
<button onClick={onUpvote(item.addr)}>Upvote</button> | <button onClick={onDownvote(item.addr)}>Downvote</button>
|
2019-08-20 20:17:23 +00:00
|
|
|
</div>);
|
|
|
|
};
|
2019-08-20 18:35:58 +00:00
|
|
|
|
|
|
|
|
2019-08-20 20:17:23 +00:00
|
|
|
const enhance = withObservables(['items'], ({ items }) => ({items: items || of({ /* default empty object */ })}));
|
2019-08-20 18:35:58 +00:00
|
|
|
const EnhancedRankItem = enhance(RankItem)
|
|
|
|
|
|
|
|
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
|
|
|
//(async () => {
|
|
|
|
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
|
|
|
//})();
|
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>;
|
|
|
|
return <EnhancedRankItem items={observables.items} onUpvote={this.upvote} onDownvote={this.downvote} />;
|
|
|
|
}
|
2019-08-20 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App />, document.getElementById('content'));
|
|
|
|
|