2022-01-02 00:19:18 +00:00
|
|
|
import React, { Component } from "react";
|
2022-01-14 21:40:54 +00:00
|
|
|
import { Paper, Button , CircularProgress, ListItemButton, Typography, Grid, Select, MenuItem, FormControl, FormHelperText, List, ListItem, ListItemText, Avatar, RouterLink, ListItemAvatar} from "@mui/material";
|
2022-01-08 11:51:55 +00:00
|
|
|
import { Link } from 'react-router-dom'
|
2022-01-14 21:40:54 +00:00
|
|
|
import { DataGrid } from '@mui/x-data-grid';
|
2022-01-15 15:12:26 +00:00
|
|
|
import getFlags from './getFlags'
|
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 = {
|
2022-01-14 21:40:54 +00:00
|
|
|
orders: new Array({id:0,}),
|
2022-01-09 21:24:48 +00:00
|
|
|
currency: 0,
|
2022-01-09 21:54:13 +00:00
|
|
|
type: 1,
|
2022-01-10 10:13:54 +00:00
|
|
|
currencies_dict: {"0":"ANY"},
|
|
|
|
loading: true,
|
2022-01-03 22:52:46 +00:00
|
|
|
};
|
2022-01-07 23:48:23 +00:00
|
|
|
this.getCurrencyDict()
|
2022-01-14 21:40:54 +00:00
|
|
|
this.getOrderDetails(this.state.type, this.state.currency)
|
2022-01-03 22:52:46 +00:00
|
|
|
this.state.currencyCode = this.getCurrencyCode(this.state.currency)
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:40:54 +00:00
|
|
|
getOrderDetails(type, currency) {
|
2022-01-09 14:29:10 +00:00
|
|
|
fetch('/api/book' + '?currency=' + currency + "&type=" + type)
|
2022-01-03 22:52:46 +00:00
|
|
|
.then((response) => response.json())
|
2022-01-14 21:40:54 +00:00
|
|
|
.then((data) => console.log(data) &
|
2022-01-05 13:39:58 +00:00
|
|
|
this.setState({
|
|
|
|
orders: data,
|
|
|
|
not_found: data.not_found,
|
2022-01-10 10:13:54 +00:00
|
|
|
loading: false,
|
2022-01-05 13:39:58 +00:00
|
|
|
}));
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 21:40:54 +00:00
|
|
|
handleRowClick=(e)=>{
|
2022-01-04 16:27:15 +00:00
|
|
|
console.log(e)
|
|
|
|
this.props.history.push('/order/' + e);
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleTypeChange=(e)=>{
|
|
|
|
this.setState({
|
2022-01-10 10:13:54 +00:00
|
|
|
type: e.target.value,
|
|
|
|
loading: true,
|
2022-01-03 22:52:46 +00:00
|
|
|
});
|
2022-01-09 14:29:10 +00:00
|
|
|
this.getOrderDetails(e.target.value,this.state.currency);
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
handleCurrencyChange=(e)=>{
|
|
|
|
this.setState({
|
|
|
|
currency: e.target.value,
|
|
|
|
currencyCode: this.getCurrencyCode(e.target.value),
|
2022-01-10 10:13:54 +00:00
|
|
|
loading: true,
|
2022-01-03 22:52:46 +00:00
|
|
|
})
|
2022-01-09 14:29:10 +00:00
|
|
|
this.getOrderDetails(this.state.type, e.target.value);
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
2022-01-08 11:51:55 +00:00
|
|
|
|
|
|
|
getCurrencyDict() {
|
2022-01-09 14:29:10 +00:00
|
|
|
fetch('/static/assets/currencies.json')
|
2022-01-08 11:51:55 +00:00
|
|
|
.then((response) => response.json())
|
|
|
|
.then((data) =>
|
|
|
|
this.setState({
|
|
|
|
currencies_dict: data
|
|
|
|
}));
|
|
|
|
}
|
2022-01-09 14:29:10 +00:00
|
|
|
|
2022-01-03 22:52:46 +00:00
|
|
|
getCurrencyCode(val){
|
2022-01-07 23:48:23 +00:00
|
|
|
return this.state.currencies_dict[val.toString()]
|
2022-01-03 22:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pretty numbers
|
|
|
|
pn(x) {
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:40:54 +00:00
|
|
|
bookListTable=()=>{
|
|
|
|
return (
|
|
|
|
<div style={{ height: 475, width: '100%' }}>
|
|
|
|
<DataGrid
|
|
|
|
rows={
|
|
|
|
this.state.orders.map((order) =>
|
|
|
|
({id: order.id,
|
|
|
|
avatar: window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png',
|
|
|
|
robosat: order.maker_nick,
|
|
|
|
type: order.type ? "Sell": "Buy",
|
|
|
|
amount: parseFloat(parseFloat(order.amount).toFixed(4)),
|
|
|
|
currency: this.getCurrencyCode(order.currency),
|
|
|
|
payment_method: order.payment_method,
|
|
|
|
price: order.price,
|
|
|
|
premium: order.premium,
|
|
|
|
})
|
|
|
|
)}
|
|
|
|
|
|
|
|
columns={[
|
|
|
|
// { field: 'id', headerName: 'ID', width: 40 },
|
|
|
|
{ field: 'robosat', headerName: 'RoboSat', width: 240,
|
|
|
|
renderCell: (params) => {return (
|
|
|
|
<ListItemButton style={{ cursor: "pointer" }}>
|
|
|
|
<ListItemAvatar>
|
|
|
|
<Avatar alt={params.row.robosat} src={params.row.avatar} />
|
|
|
|
</ListItemAvatar>
|
|
|
|
<ListItemText primary={params.row.robosat}/>
|
|
|
|
</ListItemButton>
|
|
|
|
);
|
|
|
|
} },
|
|
|
|
{ field: 'type', headerName: 'Type', width: 60 },
|
|
|
|
{ field: 'amount', headerName: 'Amount', type: 'number', width: 80 },
|
2022-01-15 15:12:26 +00:00
|
|
|
{ field: 'currency', headerName: 'Currency', width: 100,
|
|
|
|
renderCell: (params) => {return (
|
|
|
|
<div style={{ cursor: "pointer" }}>{params.row.currency + " " + getFlags(params.row.currency)}</div>
|
|
|
|
)} },
|
2022-01-14 21:40:54 +00:00
|
|
|
{ field: 'payment_method', headerName: 'Payment Method', width: 180 },
|
|
|
|
{ field: 'price', headerName: 'Price', type: 'number', width: 140,
|
|
|
|
renderCell: (params) => {return (
|
|
|
|
<div style={{ cursor: "pointer" }}>{this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }</div>
|
|
|
|
)} },
|
|
|
|
{ field: 'premium', headerName: 'Premium', type: 'number', width: 100,
|
|
|
|
renderCell: (params) => {return (
|
|
|
|
<div style={{ cursor: "pointer" }}>{parseFloat(parseFloat(params.row.premium).toFixed(4))+"%" }</div>
|
|
|
|
)} },
|
|
|
|
]}
|
|
|
|
|
|
|
|
pageSize={7}
|
|
|
|
onRowClick={(params) => this.handleRowClick(params.row.id)} // Whole row is clickable, but the mouse only looks clickly in some places.
|
|
|
|
rowsPerPageOptions={[7]}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2022-01-05 13:39:58 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 22:52:46 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Grid className='orderBook' container spacing={1}>
|
|
|
|
<Grid item xs={12} align="center">
|
2022-01-10 01:12:58 +00:00
|
|
|
<Typography component="h2" variant="h2">
|
2022-01-03 22:52:46 +00:00
|
|
|
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}
|
2022-01-09 21:24:48 +00:00
|
|
|
> <MenuItem value={2}>ANY</MenuItem>
|
2022-01-03 22:52:46 +00:00
|
|
|
<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}
|
2022-01-18 15:23:57 +00:00
|
|
|
> <MenuItem value={0}>🌍 ANY</MenuItem>
|
2022-01-07 23:48:23 +00:00
|
|
|
{
|
|
|
|
Object.entries(this.state.currencies_dict)
|
2022-01-18 15:23:57 +00:00
|
|
|
.map( ([key, value]) => <MenuItem value={parseInt(key)}>{getFlags(value) + " " + value}</MenuItem> )
|
2022-01-07 23:48:23 +00:00
|
|
|
}
|
2022-01-03 22:52:46 +00:00
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</Grid>
|
2022-01-05 13:39:58 +00:00
|
|
|
{ this.state.not_found ? "" :
|
2022-01-03 22:52:46 +00:00
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h5" variant="h5">
|
2022-01-10 10:36:37 +00:00
|
|
|
You are {this.state.type == 0 ? <b> selling </b> : (this.state.type == 1 ? <b> buying </b> :" looking at all ")} BTC for {this.state.currencyCode}
|
2022-01-03 22:52:46 +00:00
|
|
|
</Typography>
|
|
|
|
</Grid>
|
2022-01-05 13:39:58 +00:00
|
|
|
}
|
2022-01-10 10:13:54 +00:00
|
|
|
{/* If loading, show circular progressbar */}
|
|
|
|
{this.state.loading ?
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<CircularProgress />
|
|
|
|
</Grid> : ""}
|
2022-01-05 13:39:58 +00:00
|
|
|
|
|
|
|
{ this.state.not_found ?
|
|
|
|
(<Grid item xs={12} align="center">
|
|
|
|
<Grid item xs={12} align="center">
|
|
|
|
<Typography component="h5" variant="h5">
|
|
|
|
No orders found to {this.state.type == 0 ? ' sell ' :' buy ' } BTC for {this.state.currencyCode}
|
|
|
|
</Typography>
|
|
|
|
</Grid>
|
|
|
|
<Grid item>
|
2022-01-08 11:51:55 +00:00
|
|
|
<Button variant="contained" color='primary' to='/make/' component={Link}>Make Order</Button>
|
2022-01-05 13:39:58 +00:00
|
|
|
</Grid>
|
2022-01-08 11:51:55 +00:00
|
|
|
<Typography component="body1" variant="body1">
|
|
|
|
Be the first one to create an order
|
|
|
|
</Typography>
|
2022-01-05 13:39:58 +00:00
|
|
|
</Grid>)
|
2022-01-09 22:17:15 +00:00
|
|
|
:
|
2022-01-10 10:49:21 +00:00
|
|
|
<Grid item xs={12} align="center">
|
2022-01-14 21:40:54 +00:00
|
|
|
<Paper elevation={0} style={{width: 910, maxHeight: 500, overflow: 'auto'}}>
|
|
|
|
|
|
|
|
{this.state.loading ? null : this.bookListTable()}
|
|
|
|
|
2022-01-10 10:49:21 +00:00
|
|
|
</Paper>
|
|
|
|
</Grid>
|
2022-01-05 13:39:58 +00:00
|
|
|
}
|
2022-01-03 22:52:46 +00:00
|
|
|
<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
|
|
|
}
|