import React, { Component } from "react";
import { withTranslation, Trans} from "react-i18next";
import { Badge, Tooltip, Paper, Button, ListItemButton, Typography, Grid, Select, MenuItem, FormControl, FormHelperText, ListItemText, ListItemAvatar, IconButton} from "@mui/material";
import { Link } from 'react-router-dom'
import { DataGrid } from '@mui/x-data-grid';
import currencyDict from '../../static/assets/currencies.json';
import MediaQuery from 'react-responsive'
import Image from 'material-ui-image'
import getFlags from './getFlags'
import PaymentText from './PaymentText'
class BookPage extends Component {
constructor(props) {
super(props);
this.state = {
orders: new Array({id:0,}),
loading: true,
pageSize: 6,
};
this.getOrderDetails(this.props.type, this.props.currency)
}
getOrderDetails(type, currency) {
fetch('/api/book' + '?currency=' + currency + "&type=" + type)
.then((response) => response.json())
.then((data) => this.setState({
orders: data,
not_found: data.not_found,
loading: false,
}));
}
handleRowClick=(e)=>{
this.props.history.push('/order/' + e);
}
handleTypeChange=(e)=>{
this.setState({
loading: true,
});
this.props.setAppState({bookType: e.target.value})
this.getOrderDetails(e.target.value,this.props.currency);
}
handleCurrencyChange=(e)=>{
var currency = e.target.value;
this.setState({loading: true})
this.props.setAppState({
bookCurrency: currency,
bookCurrencyCode: this.getCurrencyCode(currency),
})
this.getOrderDetails(this.props.type, currency);
}
getCurrencyCode(val){
const { t } = this.props;
if (val){
return val == 0 ? t('ANY_currency') : currencyDict[val.toString()]
}else{
return t('ANY_currency')
}
}
// pretty numbers
pn(x) {
if(x == null){
return 'null'
}else{
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
}
// Colors for the status badges
statusBadgeColor(status){
if(status=='Active'){return("success")}
if(status=='Seen recently'){return("warning")}
if(status=='Inactive'){return('error')}
}
amountToString = (amount,has_range,min_amount,max_amount) => {
if (has_range){
return this.pn(parseFloat(Number(min_amount).toPrecision(2)))+'-'+this.pn(parseFloat(Number(max_amount).toPrecision(2)))
}else{
return this.pn(parseFloat(Number(amount).toPrecision(3)))
}
}
bookListTableDesktop=()=>{
const { t } = this.props;
return (
({id: order.id,
avatar: window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png',
robot: order.maker_nick,
robot_status: order.maker_status,
type: order.type ? t("Seller"): t("Buyer"),
amount: order.amount,
has_range: order.has_range,
min_amount: order.min_amount,
max_amount: order.max_amount,
currency: this.getCurrencyCode(order.currency),
payment_method: order.payment_method,
price: order.price,
premium: order.premium,
})
)}
loading={this.state.loading}
columns={[
// { field: 'id', headerName: 'ID', width: 40 },
{ field: 'robot', headerName: t("Robot"), width: 240,
renderCell: (params) => {return (
);
} },
{ field: 'type', headerName: t("Is"), width: 60 },
{ field: 'amount', headerName: t("Amount"), type: 'number', width: 90,
renderCell: (params) => {return (
{this.amountToString(params.row.amount,params.row.has_range, params.row.min_amount, params.row.max_amount)}
)}},
{ field: 'currency', headerName: t("Currency"), width: 100,
renderCell: (params) => {return (
{params.row.currency+" "}{getFlags(params.row.currency)}
)
}},
{ field: 'payment_method', headerName: t("Payment Method"), width: 180 ,
renderCell: (params) => {return (
)} },
{ field: 'price', headerName: t("Price"), type: 'number', width: 140,
renderCell: (params) => {return (
{this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }
)} },
{ field: 'premium', headerName: t("Premium"), type: 'number', width: 100,
renderCell: (params) => {return (
{parseFloat(parseFloat(params.row.premium).toFixed(4))+"%" }
)} },
]}
pageSize={this.state.loading ? 0 : this.state.pageSize}
rowsPerPageOptions={[6,20,50]}
onPageSizeChange={(newPageSize) => this.setState({pageSize:newPageSize})}
onRowClick={(params) => this.handleRowClick(params.row.id)} // Whole row is clickable, but the mouse only looks clickly in some places.
/>
);
}
bookListTablePhone=()=>{
const { t } = this.props;
return (
({id: order.id,
avatar: window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png',
robot: order.maker_nick,
robot_status: order.maker_status,
type: order.type ? t("Seller"): t("Buyer"),
amount: order.amount,
has_range: order.has_range,
min_amount: order.min_amount,
max_amount: order.max_amount,
currency: this.getCurrencyCode(order.currency),
payment_method: order.payment_method,
price: order.price,
premium: order.premium,
})
)}
columns={[
// { field: 'id', headerName: 'ID', width: 40 },
{ field: 'robot', headerName: t("Robot"), width: 64,
renderCell: (params) => {return (
);
} },
{ field: 'type', headerName: t("Is"), width: 60, hide:'true'},
{ field: 'amount', headerName: t("Amount"), type: 'number', width: 84,
renderCell: (params) => {return (
{this.amountToString(params.row.amount,params.row.has_range, params.row.min_amount, params.row.max_amount)}
)} },
{ field: 'currency', headerName: t("Currency"), width: 85,
renderCell: (params) => {return (
//
{params.row.currency+" "}{getFlags(params.row.currency)}
//
)} },
{ field: 'payment_method', headerName: t("Payment Method"), width: 180, hide:'true'},
{ field: 'payment_icons', headerName: t("Pay"), width: 75 ,
renderCell: (params) => {return (
)} },
{ field: 'price', headerName: t("Price"), type: 'number', width: 140, hide:'true',
renderCell: (params) => {return (
{this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }
)} },
{ field: 'premium', headerName: t("Premium"), type: 'number', width: 85,
renderCell: (params) => {return (
{parseFloat(parseFloat(params.row.premium).toFixed(4))+"%" }
)} },
]}
pageSize={this.state.loading ? 0 : this.state.pageSize}
rowsPerPageOptions={[6,20,50]}
onPageSizeChange={(newPageSize) => this.setState({pageSize:newPageSize})}
onRowClick={(params) => this.handleRowClick(params.row.id)} // Whole row is clickable, but the mouse only looks clickly in some places.
/>
);
}
render() {
const { t } = this.props;
return (
{/*
ORDER BOOK
*/}
{t("I want to")}
{t("ANY_type")}
{t("BUY")}
{t("SELL")}
{this.props.type == 0 ? t("and receive") : (this.props.type == 1 ? t("and pay with") : t("and use") )}
{getFlags('ANY')}{" "+t("ANY_currency")}
{
Object.entries(currencyDict)
.map( ([key, value]) => {getFlags(value)}{" "+value}
)
}
{ this.state.not_found ? "" :
{this.props.type == 0 ?
t("You are SELLING BTC for {{currencyCode}}",{currencyCode:this.props.currencyCode})
:
(this.props.type == 1 ?
t("You are BUYING BTC for {{currencyCode}}",{currencyCode:this.props.currencyCode})
:
t("You are looking at all")
)
}
}
{ this.state.not_found ?
(
{this.props.type == 0 ?
t("No orders found to sell BTC for {{currencyCode}}",{currencyCode:this.props.currencyCode})
:
t("No orders found to buy BTC for {{currencyCode}}",{currencyCode:this.props.currencyCode})
}
{t("Make Order")}
{t("Be the first one to create an order")}
)
:
{/* Desktop Book */}
{/* Smartphone Book */}
}
{t("Back")}
);
};
}
export default withTranslation()(BookPage);