implement

This commit is contained in:
Iuri Matias 2019-10-02 22:15:35 -04:00
parent 2a383a7ffd
commit 3d60b23e0f
3 changed files with 23 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import React from "react";
import Subspace from "@status-im/subspace";
import { $average, $max, $min } from "@status-im/subspace";
import { $average, $max, $min, $latest } from "@status-im/subspace";
import { map } from 'rxjs/operators';
import ProductComponent from "./ProductComponent";
import web3 from './web3';
@ -32,6 +32,7 @@ class App extends React.Component {
averageRating: rating$.pipe($average()),
minRating: rating$.pipe($min()),
maxRating: rating$.pipe($max()),
last5Ratings: rating$.pipe($latest(5)),
balance: subspace.trackBalance(Product.options.address)
});
}
@ -66,6 +67,7 @@ class App extends React.Component {
minRating={this.state.minRating}
averageRating={this.state.averageRating}
balance={this.state.balance}
last5Ratings={this.state.last5Ratings}
/>
</div>
);

View File

@ -1,17 +1,21 @@
import React from "react";
import { observe } from "@status-im/subspace/react";
const ProductComponent = ({ maxRating, minRating, averageRating, title, balance }) => {
const ProductComponent = ({ maxRating, minRating, averageRating, title, balance, last5Ratings }) => {
// Handle initial state when no data is available
// if (!maxRating && !minRating && !averageRating) {
// return <p>No data</p>;
// }
console.dir("last5Ratings")
console.dir(last5Ratings)
return <ul>
<li><b>title: </b> {title}</li>
<li><b>minimum rating: </b> {minRating}</li>
<li><b>maximum rating: </b> {maxRating}</li>
<li><b>average rating: </b> {averageRating}</li>
<li><b>last 5 ratings: </b> {(last5Ratings || []).join(', ')}</li>
<li><b>balance in contract:</b> {balance}</li>
</ul>;
};

View File

@ -71,11 +71,6 @@ export function $min(cb) {
} else {
currentValue = curr;
}
console.dir("===========");
console.dir(currentValue);
console.dir(acc);
console.dir(currentValue <= acc);
console.dir("===========");
if (currentValue < acc) return currentValue;
return acc;
@ -83,6 +78,21 @@ export function $min(cb) {
);
}
export function $latest(num) {
return pipe(
scan((acc, curr) => {
let currentValue = curr;
acc.push(currentValue)
if (acc.length > num) {
acc.shift()
}
return acc;
}, [])
);
}
/*
of(10, 3, 4)
.pipe($average())