diff --git a/app/js/components/addToken.js b/app/js/components/addToken.js index 22940df..30cdbdc 100644 --- a/app/js/components/addToken.js +++ b/app/js/components/addToken.js @@ -70,6 +70,8 @@ class AddToken extends Component { price: '' }); + this.props.loadShipsForSale(); + // TODO: show success }) .catch((err) => { diff --git a/app/js/components/ship.js b/app/js/components/ship.js index 0aa1c5c..f3a7870 100644 --- a/app/js/components/ship.js +++ b/app/js/components/ship.js @@ -40,8 +40,13 @@ class Ship extends Component { }) .then(receipt => { console.log(receipt); + + this.props.onBuy(); + // TODO: show success // TODO: hide ship + + return true; }) .catch((err) => { console.error(err); diff --git a/app/js/components/shipList.js b/app/js/components/shipList.js new file mode 100644 index 0000000..d9c7d00 --- /dev/null +++ b/app/js/components/shipList.js @@ -0,0 +1,20 @@ +import EmbarkJS from 'Embark/EmbarkJS'; +import web3 from "Embark/web3" +import React, { Component, Fragment } from 'react'; +import ReactDOM from 'react-dom'; +import SpaceshipToken from 'Embark/contracts/SpaceshipToken'; +import Ship from './ship'; + +class ShipList extends Component { + + render = () => { + const { list, title, id, wallet, onBuy } = this.props; + return
+

{title}

+ { list.map((ship, i) => ) } +
; + } +} + + +export default ShipList; diff --git a/app/js/components/shipyard.js b/app/js/components/shipyard.js deleted file mode 100644 index 618bf19..0000000 --- a/app/js/components/shipyard.js +++ /dev/null @@ -1,55 +0,0 @@ -import EmbarkJS from 'Embark/EmbarkJS'; -import web3 from "Embark/web3" -import React, { Component, Fragment } from 'react'; -import ReactDOM from 'react-dom'; -import SpaceshipToken from 'Embark/contracts/SpaceshipToken'; -import Ship from './ship'; - -class Shipyard extends Component { - - constructor(props) { - super(props); - this.state = { - shipList: [] - } - } - - componentDidMount(){ - EmbarkJS.onReady((err) => { - this._loadShipsForSale(); - }); - } - - _loadShipsForSale = async () => { - const { shipsForSaleN, shipsForSale, spaceshipPrices, spaceships } = SpaceshipToken.methods; - - const total = await shipsForSaleN().call(); - const list = []; - if(total){ - for (let i = total-1; i >= 0; i--) { - const shipForSale = await shipsForSale(i).call(); - const _info = await spaceships(shipForSale).call(); - const _price = await spaceshipPrices(shipForSale).call(); - - const ship = { - price: _price, - id: shipForSale, - ..._info - }; - list.push(ship); - } - } - this.setState({shipList: list.reverse()}); - } - - render = () => { - const { shipList } = this.state; - return
-

Tienda

- { shipList.map((ship, i) => ) } -
; - } -} - - -export default Shipyard; diff --git a/app/js/index.js b/app/js/index.js index cfaac00..cb9243a 100644 --- a/app/js/index.js +++ b/app/js/index.js @@ -2,7 +2,7 @@ import EmbarkJS from 'Embark/EmbarkJS'; import React, { Fragment, Component } from 'react'; import ReactDOM from 'react-dom'; import SpaceshipToken from 'Embark/contracts/SpaceshipToken'; -import Shipyard from './components/shipyard.js'; +import ShipList from './components/shipList.js'; import WithdrawBalance from './components/withdrawBalance.js'; import AddToken from './components/addToken.js'; import MyShips from './components/myShips.js'; @@ -13,16 +13,24 @@ class App extends Component { super(props); this.state = { isOwner: false, - hidePanel: false + hidePanel: false, + shipsForSale: [], + myShips: [] } } componentDidMount(){ EmbarkJS.onReady((err) => { this._isOwner(); + this._loadEverything(); }); } + _loadEverything(){ + this._loadShipsForSale(); + this._loadMyShips(); + } + _isOwner(){ SpaceshipToken.methods.owner() .call() @@ -32,8 +40,50 @@ class App extends Component { }); } + _loadShipsForSale = async () => { + const { shipsForSaleN, shipsForSale, spaceshipPrices, spaceships } = SpaceshipToken.methods; + + const total = await shipsForSaleN().call(); + const list = []; + if(total){ + for (let i = total-1; i >= 0; i--) { + const shipId = await shipsForSale(i).call(); + const _info = await spaceships(shipId).call(); + const _price = await spaceshipPrices(shipId).call(); + + const ship = { + price: _price, + id: shipId, + ..._info + }; + list.push(ship); + } + } + this.setState({shipsForSale: list.reverse()}); + } + + _loadMyShips = async () => { + const { balanceOf, tokenOfOwnerByIndex, spaceships } = SpaceshipToken.methods; + + const total = await balanceOf(web3.eth.defaultAccount).call(); + const list = []; + if(total){ + for (let i = total-1; i >= 0; i--) { + const myShipId = await tokenOfOwnerByIndex(web3.eth.defaultAccount, i).call(); + const _info = await spaceships(myShipId).call(); + + const ship = { + id: myShipId, + ..._info + }; + list.push(ship); + } + } + this.setState({myShips: list.reverse()}); + } + render(){ - const { isOwner, hidePanel } = this.state; + const { isOwner, hidePanel, shipsForSale, myShips } = this.state; return ( @@ -41,11 +91,11 @@ class App extends Component {
this.setState({'hidePanel': true})}>cerrar - +
: '' } - - + + this._loadEverything()} />
); } }