fix: update react-example1
This commit is contained in:
parent
34fba9d812
commit
9fa62bec38
|
@ -0,0 +1 @@
|
||||||
|
SKIP_PREFLIGHT_CHECK=true
|
|
@ -0,0 +1,12 @@
|
||||||
|
const {
|
||||||
|
override,
|
||||||
|
addWebpackAlias,
|
||||||
|
} = require("customize-cra");
|
||||||
|
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = override(
|
||||||
|
addWebpackAlias({
|
||||||
|
react: path.resolve('./node_modules/react')
|
||||||
|
})
|
||||||
|
)
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "observables",
|
"name": "react-example-1",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -10,12 +10,16 @@
|
||||||
"rxjs": "^6.5.2",
|
"rxjs": "^6.5.2",
|
||||||
"web3": "^1.2.1"
|
"web3": "^1.2.1"
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"customize-cra": "^0.9.1",
|
||||||
|
"react-app-rewired": "^2.1.5"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "react-app-rewired start",
|
||||||
"deploy": "node deploy.js",
|
"build": "react-app-rewired build",
|
||||||
"build": "react-scripts build",
|
"test": "react-app-rewired test",
|
||||||
"test": "react-scripts test",
|
"eject": "react-scripts eject",
|
||||||
"eject": "react-scripts eject"
|
"deploy": "node deploy.js"
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": "react-app"
|
"extends": "react-app"
|
||||||
|
|
|
@ -1,34 +1,37 @@
|
||||||
import React from "react";
|
import React, {Component} from "react";
|
||||||
import Subspace from "@embarklabs/subspace";
|
import {$average, $max, $min, $latest} from "@embarklabs/subspace";
|
||||||
import { $average, $max, $min, $latest } from "@embarklabs/subspace";
|
import {map} from "rxjs/operators";
|
||||||
import { map } from 'rxjs/operators';
|
|
||||||
import ProductComponent from "./ProductComponent";
|
import ProductComponent from "./ProductComponent";
|
||||||
import web3 from './web3';
|
import ContractInstance from "./ContractDeployer";
|
||||||
|
import {abi, address} from "./contract.json";
|
||||||
import {abi, address} from './contract.json'
|
import {withSubspace, SubspaceProvider} from "@embarklabs/subspace-react";
|
||||||
|
import Web3 from "web3";
|
||||||
|
|
||||||
let Product;
|
let Product;
|
||||||
|
|
||||||
class App extends React.Component {
|
class FormComponent extends Component {
|
||||||
state = {
|
state = {
|
||||||
userTitle: "",
|
userTitle: "",
|
||||||
userRating: 0,
|
userRating: 0,
|
||||||
|
|
||||||
minRating: 0,
|
minRating: 0,
|
||||||
maxRating: 0,
|
maxRating: 0,
|
||||||
averageRating: 0
|
averageRating: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
const subspace = new Subspace(web3.currentProvider);
|
// Product = subspace.contract(ContractInstance()); // would also work
|
||||||
await subspace.init();
|
Product = this.props.subspace.contract({abi, address});
|
||||||
|
|
||||||
// Product = subspace.contract(ContractInstance); // would also work
|
const rating$ = Product.events.Rating.track()
|
||||||
Product = subspace.contract({abi, address});
|
.map("rating")
|
||||||
|
.pipe(map(x => parseInt(x)));
|
||||||
const rating$ = Product.events.Rating.track().map("rating").pipe(map(x => parseInt(x)));
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
title: Product.methods.products(0).track().map('title'),
|
title: Product.methods
|
||||||
|
.products(0)
|
||||||
|
.track()
|
||||||
|
.map("title"),
|
||||||
averageRating: rating$.pipe($average()),
|
averageRating: rating$.pipe($average()),
|
||||||
minRating: rating$.pipe($min()),
|
minRating: rating$.pipe($min()),
|
||||||
maxRating: rating$.pipe($max()),
|
maxRating: rating$.pipe($max()),
|
||||||
|
@ -38,31 +41,47 @@ class App extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
rateProduct = async () => {
|
rateProduct = async () => {
|
||||||
let accounts = await web3.eth.getAccounts();
|
let accounts = await this.props.subspace.web3.eth.getAccounts();
|
||||||
await Product.methods.rateProduct(0, this.state.userRating).send({from: accounts[0]});
|
await Product.methods.rateProduct(0, this.state.userRating).send({from: accounts[0]});
|
||||||
};
|
};
|
||||||
|
|
||||||
updateTitle = async () => {
|
updateTitle = async () => {
|
||||||
let accounts = await web3.eth.getAccounts();
|
let accounts = await this.props.subspace.web3.eth.getAccounts();
|
||||||
await Product.methods.editProduct(0, this.state.userTitle).send({from: accounts[0]});
|
await Product.methods.editProduct(0, this.state.userTitle).send({from: accounts[0]});
|
||||||
};
|
};
|
||||||
|
|
||||||
sendFunds = async () => {
|
sendFunds = async () => {
|
||||||
let accounts = await web3.eth.getAccounts();
|
let accounts = await this.props.subspace.web3.eth.getAccounts();
|
||||||
await web3.eth.sendTransaction({value: this.state.contractFunds, to: Product.options.address, from: accounts[0]});
|
await this.props.subspace.web3.eth.sendTransaction({
|
||||||
|
value: this.state.contractFunds,
|
||||||
|
to: Product.options.address,
|
||||||
|
from: accounts[0]
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<button onClick={this.rateProduct}>Rate Product</button>
|
<button onClick={this.rateProduct}>Rate Product</button>
|
||||||
<input type="number" value={this.state.userRating} onChange={(evt) => this.setState({userRating: parseInt(evt.target.value)}) } />
|
<input
|
||||||
|
type="number"
|
||||||
|
value={this.state.userRating}
|
||||||
|
onChange={evt => this.setState({userRating: parseInt(evt.target.value)})}
|
||||||
|
/>
|
||||||
|
|
||||||
<button onClick={this.updateTitle}>Update Title</button>
|
<button onClick={this.updateTitle}>Update Title</button>
|
||||||
<input type="string" value={this.state.userTitle} onChange={(evt) => this.setState({userTitle: evt.target.value}) } />
|
<input
|
||||||
|
type="string"
|
||||||
|
value={this.state.userTitle}
|
||||||
|
onChange={evt => this.setState({userTitle: evt.target.value})}
|
||||||
|
/>
|
||||||
|
|
||||||
<button onClick={this.sendFunds}>Send Funds</button>
|
<button onClick={this.sendFunds}>Send Funds</button>
|
||||||
<input type="string" value={this.state.contractFunds} onChange={(evt) => this.setState({contractFunds: parseInt(evt.target.value)}) } />
|
<input
|
||||||
|
type="string"
|
||||||
|
value={this.state.contractFunds}
|
||||||
|
onChange={evt => this.setState({contractFunds: parseInt(evt.target.value)})}
|
||||||
|
/>
|
||||||
|
|
||||||
<ProductComponent
|
<ProductComponent
|
||||||
title={this.state.title}
|
title={this.state.title}
|
||||||
|
@ -77,4 +96,15 @@ class App extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Form = withSubspace(FormComponent);
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const web3 = new Web3("ws://localhost:8545");
|
||||||
|
return (
|
||||||
|
<SubspaceProvider web3={web3}>
|
||||||
|
<Form />
|
||||||
|
</SubspaceProvider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
import {abi, address} from './contract.json'
|
||||||
|
|
||||||
|
const getInstance = async web3 => {
|
||||||
|
if(!web3.eth.defaultAccount){
|
||||||
|
const accounts = await web3.eth.getAccounts();
|
||||||
|
web3.eth.defaultAccount = accounts[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
const MyContract = new web3.eth.Contract(abi, {from: web3.eth.default, gas: "800000"});
|
||||||
|
|
||||||
|
MyContract.options.address = address;
|
||||||
|
MyContract.options.from = web3.eth.defaultAccount;
|
||||||
|
|
||||||
|
return MyContract;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default getInstance;
|
|
@ -1,16 +0,0 @@
|
||||||
import web3 from './web3';
|
|
||||||
import {abi, address} from './contract.json'
|
|
||||||
|
|
||||||
const MyContract = new web3.eth.Contract(abi, {from: web3.eth.default, gas: "800000"});
|
|
||||||
MyContract.options.address = address;
|
|
||||||
|
|
||||||
MyContract.getInstance = async () => {
|
|
||||||
if (!web3.eth.defaultAccount) {
|
|
||||||
const accounts = await web3.eth.getAccounts();
|
|
||||||
web3.eth.defaultAccount = accounts[0];
|
|
||||||
}
|
|
||||||
MyContract.options.from = web3.eth.defaultAccount;
|
|
||||||
return MyContract;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default MyContract;
|
|
|
@ -1,5 +1,5 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { observe } from "@embarklabs/subspace/react";
|
import { observe } from "@embarklabs/subspace-react";
|
||||||
|
|
||||||
const ProductComponent = ({ maxRating, minRating, averageRating, title, balance, last5Ratings }) => {
|
const ProductComponent = ({ maxRating, minRating, averageRating, title, balance, last5Ratings }) => {
|
||||||
// Handle initial state when no data is available
|
// Handle initial state when no data is available
|
||||||
|
@ -7,9 +7,6 @@ const ProductComponent = ({ maxRating, minRating, averageRating, title, balance,
|
||||||
// return <p>No data</p>;
|
// return <p>No data</p>;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
console.dir("last5Ratings")
|
|
||||||
console.dir(last5Ratings)
|
|
||||||
|
|
||||||
return <ul>
|
return <ul>
|
||||||
<li><b>title: </b> {title}</li>
|
<li><b>title: </b> {title}</li>
|
||||||
<li><b>minimum rating: </b> {minRating}</li>
|
<li><b>minimum rating: </b> {minRating}</li>
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
import Web3 from 'web3';
|
|
||||||
|
|
||||||
const web3 = new Web3("ws://localhost:8545");
|
|
||||||
|
|
||||||
export default web3;
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue