From 71107a7432f6b38ecbbb9d4789647bee64b5acff Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Sat, 8 Jan 2022 17:23:13 -0800 Subject: [PATCH] Refresh orders --- api/views.py | 2 +- frontend/src/components/OrderPage.js | 30 +++++++++++++++-- frontend/src/components/TradeBox.js | 49 +++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/api/views.py b/api/views.py index addb763e..511891c9 100644 --- a/api/views.py +++ b/api/views.py @@ -372,7 +372,7 @@ class BookView(ListAPIView): class InfoView(ListAPIView): - def get(self): + def get(self, request): context = {} context['num_public_buy_orders'] = len(Order.objects.filter(type=Order.Types.BUY, status=Order.Status.PUB)) diff --git a/frontend/src/components/OrderPage.js b/frontend/src/components/OrderPage.js index e7196d1a..8521918f 100644 --- a/frontend/src/components/OrderPage.js +++ b/frontend/src/components/OrderPage.js @@ -67,15 +67,18 @@ export default class OrderPage extends Component { super(props); this.state = { isExplicit: false, + delay: 5000, // Refresh every 5 seconds }; this.orderId = this.props.match.params.orderId; this.getOrderDetails(); } getOrderDetails() { + this.setState(null) fetch('/api/order' + '?order_id=' + this.orderId) .then((response) => response.json()) .then((data) => { + console.log(data) & this.setState({ statusCode: data.status, statusText: data.status_message, @@ -107,6 +110,29 @@ export default class OrderPage extends Component { }); } + + // These are used to refresh the data + componentDidMount() { + this.interval = setInterval(this.tick, this.state.delay); + } + componentDidUpdate(prevProps, prevState) { + if (prevState.delay !== this.state.delay) { + clearInterval(this.interval); + this.interval = setInterval(this.tick, this.state.delay); + } + } + componentWillUnmount() { + clearInterval(this.interval); + } + tick = () => { + this.getOrderDetails(); + } + handleDelayChange = (e) => { + this.setState({ delay: Number(e.target.value) }); + } + + + // Gets currency code (3 letters) from numeric (e.g., 1 -> USD) // Improve this function so currencies are read from json getCurrencyCode(val){ @@ -162,7 +188,7 @@ export default class OrderPage extends Component { src={window.location.origin +'/static/assets/avatars/' + this.state.makerNick + '.png'} /> - + @@ -171,7 +197,7 @@ export default class OrderPage extends Component { {this.state.takerNick!='None' ? <> - + { + this.setState({ delay: Number(e.target.value) }); + } + tick = () => { + this.data = this.props.data; + } + showInvoice=()=>{ return ( @@ -71,6 +96,27 @@ export default class TradeBox extends Component { ); } + showTakerFound=()=>{ + + // Make some sound here! The maker might have been waiting for long + + return ( + + + + A taker has been found! + + + + + + Please wait for the taker to confirm his commitment by locking a bond. + + + + ); + } + showMakerWait=()=>{ return ( @@ -124,6 +170,7 @@ export default class TradeBox extends Component { {this.data.bondInvoice ? this.showInvoice() : ""} {this.data.isMaker & this.data.statusCode == 1 ? this.showMakerWait() : ""} + {this.data.isMaker & this.data.statusCode == 3 ? this.showTakerFound() : ""} {this.data.isSeller & this.data.escrowInvoice != null ? this.showEscrowInvoice() : ""}