mirror of
https://github.com/embarklabs/subspace.git
synced 2025-01-23 12:59:25 +00:00
feat: multiple items
This commit is contained in:
parent
633346d478
commit
4049688511
@ -7,22 +7,22 @@ import Ranking from '../embarkArtifacts/contracts/Ranking';
|
||||
import Phoenix from 'phoenix';
|
||||
import withObservables from '@nozbe/with-observables'
|
||||
|
||||
const {scan} = require('rxjs/operators');
|
||||
const { scan, map } = require('rxjs/operators');
|
||||
import { of } from 'rxjs';
|
||||
|
||||
|
||||
const phoenix = new Phoenix(web3);
|
||||
|
||||
const RankItem = ({votes, onUpvote, onDownvote}) => (
|
||||
<div>
|
||||
<b>1</b> - 0x00000000000000000000000000001<br />
|
||||
Upvotes: {votes.upvotes} - Downvotes: {votes.downvotes}<br />
|
||||
<button onClick={onUpvote}>Upvote</button> | <button onClick={onDownvote}>Downvote</button>
|
||||
</div>
|
||||
);
|
||||
const RankItem = ({items, onUpvote, onDownvote}) => {
|
||||
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>
|
||||
</div>);
|
||||
};
|
||||
|
||||
|
||||
const enhance = withObservables(['votes'], ({ votes }) => ({votes: votes || of({ /* default empty object */ })}));
|
||||
const enhance = withObservables(['items'], ({ items }) => ({items: items || of({ /* default empty object */ })}));
|
||||
const EnhancedRankItem = enhance(RankItem)
|
||||
|
||||
const observables = {};
|
||||
@ -40,34 +40,43 @@ class App extends React.Component {
|
||||
}
|
||||
|
||||
phoenix.init(() => {
|
||||
observables.votes = phoenix.trackEvent(Ranking, 'Rating', {filter: {}, fromBlock: 1}).pipe(
|
||||
scan((acc, curr) => {
|
||||
if(curr.rating >= 3){
|
||||
acc.upvotes++;
|
||||
} else {
|
||||
acc.downvotes++;
|
||||
}
|
||||
return acc;
|
||||
}, {upvotes: 0, downvotes: 0})
|
||||
);
|
||||
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]}
|
||||
})
|
||||
})
|
||||
);
|
||||
|
||||
this.setState({ready: true});
|
||||
this.setState({ready: true});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
upvote = () => {
|
||||
Ranking.methods.rate("0x0000000000000000000000000000000000000001", 5).send();
|
||||
upvote = address => () => {
|
||||
Ranking.methods.rate(address, 5).send();
|
||||
}
|
||||
|
||||
downvote = () => {
|
||||
Ranking.methods.rate("0x0000000000000000000000000000000000000001", 1).send();
|
||||
downvote = address => () => {
|
||||
Ranking.methods.rate(address, 1).send();
|
||||
}
|
||||
|
||||
render() {
|
||||
const {ready} = this.state;
|
||||
if(!ready) return <span>Loading...</span>;
|
||||
return <EnhancedRankItem votes={observables.votes} onUpvote={this.upvote} onDownvote={this.downvote} />;
|
||||
return <EnhancedRankItem items={observables.items} onUpvote={this.upvote} onDownvote={this.downvote} />;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<style type="text/css">
|
||||
div.item {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="content"></div>
|
||||
|
@ -57,7 +57,13 @@ module.exports = {
|
||||
// filteredFields: [],
|
||||
|
||||
contracts: {
|
||||
|
||||
"Ranking": {
|
||||
onDeploy: async (dependencies) => {
|
||||
await dependencies.contracts.Ranking.methods.rate('0xcafecafecafecafecafecafecafecafecafecafe', 4).send({from: dependencies.web3.eth.defaultAccount}),
|
||||
await dependencies.contracts.Ranking.methods.rate('0x0001020304050607080900010203040506070809', 1).send({from: dependencies.web3.eth.defaultAccount}),
|
||||
await dependencies.contracts.Ranking.methods.rate('0x0000000100000001000000010000000100000001', 3).send({from: dependencies.web3.eth.defaultAccount})
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user