2022-01-02 00:19:18 +00:00
|
|
|
import React, { Component } from "react";
|
2022-01-04 10:21:45 +00:00
|
|
|
import { Paper, Button , Divider, Card, CardActionArea, CardContent, Typography, Grid, Select, MenuItem, FormControl, FormHelperText, List, ListItem, ListItemText, Avatar, Link, RouterLink, ListItemAvatar} from "@material-ui/core"
|
2022-01-02 00:19:18 +00:00
|
|
|
|
|
|
|
export default class BookPage extends Component {
|
2022-01-03 22:52:46 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
orders: new Array(),
|
|
|
|
currency: 1,
|
|
|
|
type: 1,
|
|
|
|
};
|
|
|
|
this.getOrderDetails()
|
|
|
|
this.state.currencyCode = this.getCurrencyCode(this.state.currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix needed to handle HTTP 404 error when no order is found
|
|
|
|
// Show message to be the first one to make an order
|
|
|
|
getOrderDetails() {
|
|
|
|
fetch('/api/book' + '?currency=' + this.state.currency + "&type=" + this.state.type)
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) => //console.log(data));
|
|
|
|
this.setState({orders: data}));
|
|
|
|
}
|
|
|
|
|
2022-01-04 10:21:45 +00:00
|
|
|
handleCardClick (orderId){
|
|
|
|
this.props.history.push('/order/' + orderId);
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make these two functions sequential. getOrderDetails needs setState to be finish beforehand.
|
|
|
|
handleTypeChange=(e)=>{
|
|
|
|
this.setState({
|
|
|
|
type: e.target.value,
|
|
|
|
});
|
|
|
|
this.getOrderDetails();
|
|
|
|
}
|
|
|
|
handleCurrencyChange=(e)=>{
|
|
|
|
this.setState({
|
|
|
|
currency: e.target.value,
|
|
|
|
currencyCode: this.getCurrencyCode(e.target.value),
|
|
|
|
})
|
|
|
|
this.getOrderDetails();
|
|
|
|
}
|
|
|
|
|
2022-01-04 10:21:45 +00:00
|
|
|
// Gets currency code (3 letters) from numeric (e.g., 1 -> USD)
|
2022-01-03 22:52:46 +00:00
|
|
|
// Improve this function so currencies are read from json
|
|
|
|
getCurrencyCode(val){
|
2022-01-04 10:21:45 +00:00
|
|
|
return (val == 1 ) ? "USD": ((val == 2 ) ? "EUR":"ETH")
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pretty numbers
|
|
|
|
pn(x) {
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Grid className='orderBook' container spacing={1}>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h4" variant="h4">
|
|
|
|
Order Book
|
|
|
|
</Typography>
|
|
|
|
</Grid>
|
|
|
|
|
|
|
|
<Grid item xs={6} align="right">
|
|
|
|
<FormControl >
|
|
|
|
<FormHelperText>
|
|
|
|
I want to
|
|
|
|
</FormHelperText>
|
|
|
|
<Select
|
|
|
|
label="Select Order Type"
|
|
|
|
required="true"
|
|
|
|
value={this.state.type}
|
|
|
|
inputProps={{
|
|
|
|
style: {textAlign:"center"}
|
|
|
|
}}
|
|
|
|
onChange={this.handleTypeChange}
|
|
|
|
>
|
|
|
|
<MenuItem value={1}>BUY</MenuItem>
|
|
|
|
<MenuItem value={0}>SELL</MenuItem>
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</Grid>
|
|
|
|
|
|
|
|
<Grid item xs={6} align="left">
|
|
|
|
<FormControl >
|
|
|
|
<FormHelperText>
|
|
|
|
And pay with
|
|
|
|
</FormHelperText>
|
|
|
|
<Select
|
|
|
|
label="Select Payment Currency"
|
|
|
|
required="true"
|
|
|
|
value={this.state.currency}
|
|
|
|
inputProps={{
|
|
|
|
style: {textAlign:"center"}
|
|
|
|
}}
|
|
|
|
onChange={this.handleCurrencyChange}
|
|
|
|
>
|
|
|
|
<MenuItem value={1}>USD</MenuItem>
|
|
|
|
<MenuItem value={2}>EUR</MenuItem>
|
|
|
|
<MenuItem value={3}>ETH</MenuItem>
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</Grid>
|
|
|
|
{this.state.orders.map((order) =>
|
2022-01-04 10:21:45 +00:00
|
|
|
<Grid container item sm={4}>
|
2022-01-04 13:47:37 +00:00
|
|
|
<Card elevation={6} sx={{ width: 945 }}>
|
2022-01-03 22:52:46 +00:00
|
|
|
{/* Linking to order details not working yet as expected */}
|
2022-01-04 10:21:45 +00:00
|
|
|
{/* <CardActionArea onClick={this.handleCardClick(15)} component={RouterLink} to="/order"> */}
|
|
|
|
<CardActionArea>
|
2022-01-03 22:52:46 +00:00
|
|
|
<CardContent>
|
2022-01-04 10:21:45 +00:00
|
|
|
|
|
|
|
<List dense="true">
|
|
|
|
<ListItem >
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar
|
|
|
|
alt={order.maker_nick}
|
|
|
|
src={window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png'}
|
|
|
|
/>
|
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText>
|
|
|
|
<Typography gutterBottom variant="h6">
|
|
|
|
{order.maker_nick}
|
|
|
|
</Typography>
|
|
|
|
</ListItemText>
|
|
|
|
</ListItem>
|
|
|
|
|
2022-01-03 22:52:46 +00:00
|
|
|
{/* CARD PARAGRAPH CONTENT */}
|
2022-01-04 10:21:45 +00:00
|
|
|
<ListItemText>
|
|
|
|
<Typography variant="subtitle1" color="text.secondary">
|
|
|
|
◑{order.type == 0 ? <b> Buys </b>: <b> Sells </b>}
|
|
|
|
<b>{parseFloat(parseFloat(order.amount).toFixed(4))}
|
|
|
|
{" " +this.getCurrencyCode(order.currency)}</b> <a> worth of bitcoin</a>
|
|
|
|
</Typography>
|
|
|
|
|
|
|
|
<Typography variant="subtitle1" color="text.secondary">
|
|
|
|
◑ Payment via <b>{order.payment_method}</b>
|
|
|
|
</Typography>
|
|
|
|
|
|
|
|
<Typography variant="subtitle1" color="text.secondary">
|
|
|
|
◑ Priced {order.is_explicit ?
|
|
|
|
" explicitly at " + this.pn(order.satoshis) + " Sats" : (
|
2022-01-04 13:47:37 +00:00
|
|
|
" at " +
|
|
|
|
parseFloat(parseFloat(order.premium).toFixed(4)) + "% over the market"
|
2022-01-04 10:21:45 +00:00
|
|
|
)}
|
|
|
|
</Typography>
|
|
|
|
|
|
|
|
<Typography variant="subtitle1" color="text.secondary">
|
|
|
|
◑ <b>{" 42,354 "}{this.getCurrencyCode(order.currency)}/BTC</b> (Binance API)
|
|
|
|
</Typography>
|
|
|
|
</ListItemText>
|
|
|
|
|
|
|
|
</List>
|
|
|
|
|
2022-01-03 22:52:46 +00:00
|
|
|
</CardContent>
|
|
|
|
</CardActionArea>
|
|
|
|
</Card>
|
|
|
|
</Grid>
|
|
|
|
)}
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h5" variant="h5">
|
|
|
|
You are {this.state.type == 0 ? " selling " : " buying "} BTC for {this.state.currencyCode}
|
|
|
|
</Typography>
|
|
|
|
</Grid>
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Button color="secondary" variant="contained" to="/" component={Link}>
|
|
|
|
Back
|
|
|
|
</Button>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
};
|
2022-01-02 00:19:18 +00:00
|
|
|
}
|