robosats/frontend/src/components/BookPage.js

368 lines
15 KiB
JavaScript
Raw Normal View History

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";
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';
import currencyDict from '../../static/assets/currencies.json';
2022-01-27 14:40:14 +00:00
import MediaQuery from 'react-responsive'
2022-02-01 19:43:33 +00:00
import Image from 'material-ui-image'
2022-01-15 15:12:26 +00:00
import getFlags from './getFlags'
import PaymentText from './PaymentText'
class BookPage extends Component {
constructor(props) {
super(props);
this.state = {
2022-01-14 21:40:54 +00:00
orders: new Array({id:0,}),
loading: true,
2022-03-06 21:10:57 +00:00
pageSize: 6,
};
this.getOrderDetails(this.props.type, this.props.currency)
}
2022-01-14 21:40:54 +00:00
getOrderDetails(type, currency) {
fetch('/api/book' + '?currency=' + currency + "&type=" + type)
.then((response) => response.json())
.then((data) => this.setState({
2022-01-05 13:39:58 +00:00
orders: data,
not_found: data.not_found,
loading: false,
2022-01-05 13:39:58 +00:00
}));
}
2022-01-14 21:40:54 +00:00
handleRowClick=(e)=>{
2022-01-04 16:27:15 +00:00
this.props.history.push('/order/' + e);
}
handleTypeChange=(e)=>{
this.setState({
loading: true,
});
2022-04-01 14:52:44 +00:00
this.props.setAppState({bookType: e.target.value})
this.getOrderDetails(e.target.value,this.props.currency);
}
handleCurrencyChange=(e)=>{
2022-04-01 14:52:44 +00:00
var currency = e.target.value;
this.setState({loading: true})
this.props.setAppState({
bookCurrency: currency,
bookCurrencyCode: this.getCurrencyCode(currency),
})
2022-04-01 14:52:44 +00:00
this.getOrderDetails(this.props.type, currency);
}
getCurrencyCode(val){
const { t } = this.props;
if (val){
return val == 0 ? t('ANY_currency') : currencyDict[val.toString()]
2022-04-01 14:52:44 +00:00
}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(".");
}
}
2022-01-27 14:40:14 +00:00
2022-02-03 18:06:30 +00:00
// 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)))
}
}
2022-02-03 18:06:30 +00:00
2022-01-27 14:40:14 +00:00
bookListTableDesktop=()=>{
const { t } = this.props;
2022-01-14 21:40:54 +00:00
return (
2022-03-06 21:10:57 +00:00
<div style={{ height: 422, width: '100%' }}>
2022-01-14 21:40:54 +00:00
<DataGrid
rows={
this.state.orders.map((order) =>
({id: order.id,
avatar: window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png',
2022-02-03 18:06:30 +00:00
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,
2022-01-14 21:40:54 +00:00
currency: this.getCurrencyCode(order.currency),
payment_method: order.payment_method,
price: order.price,
premium: order.premium,
})
)}
loading={this.state.loading}
2022-01-14 21:40:54 +00:00
columns={[
// { field: 'id', headerName: 'ID', width: 40 },
{ field: 'robot', headerName: t("Robot"), width: 240,
2022-01-14 21:40:54 +00:00
renderCell: (params) => {return (
<ListItemButton style={{ cursor: "pointer" }}>
<ListItemAvatar>
<Tooltip placement="right" enterTouchDelay="0" title={t(params.row.robot_status)}>
2022-02-03 18:06:30 +00:00
<Badge variant="dot" overlap="circular" badgeContent="" color={this.statusBadgeColor(params.row.robot_status)}>
<div style={{ width: 45, height: 45 }}>
<Image className='bookAvatar'
disableError='true'
disableSpinner='true'
color='null'
alt={params.row.robot}
src={params.row.avatar}
/>
</div>
</Badge>
</Tooltip>
2022-01-14 21:40:54 +00:00
</ListItemAvatar>
2022-02-03 18:06:30 +00:00
<ListItemText primary={params.row.robot}/>
2022-01-14 21:40:54 +00:00
</ListItemButton>
);
} },
{ field: 'type', headerName: t("Is"), width: 60 },
{ field: 'amount', headerName: t("Amount"), type: 'number', width: 90,
renderCell: (params) => {return (
<div style={{ cursor: "pointer" }}>{this.amountToString(params.row.amount,params.row.has_range, params.row.min_amount, params.row.max_amount)}</div>
)}},
{ field: 'currency', headerName: t("Currency"), width: 100,
2022-01-15 15:12:26 +00:00
renderCell: (params) => {return (
2022-02-19 18:44:02 +00:00
<div style={{ cursor: "pointer", display:'flex',alignItems:'center', flexWrap:'wrap'}}>{params.row.currency+" "}{getFlags(params.row.currency)}</div>)
}},
{ field: 'payment_method', headerName: t("Payment Method"), width: 180 ,
renderCell: (params) => {return (
<div style={{ cursor: "pointer" }}><PaymentText othersText={t("Others")} verbose={true} size={20} text={params.row.payment_method}/></div>
)} },
{ field: 'price', headerName: t("Price"), type: 'number', width: 140,
2022-01-14 21:40:54 +00:00
renderCell: (params) => {return (
<div style={{ cursor: "pointer" }}>{this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }</div>
)} },
{ field: 'premium', headerName: t("Premium"), type: 'number', width: 100,
2022-01-14 21:40:54 +00:00
renderCell: (params) => {return (
<div style={{ cursor: "pointer" }}>{parseFloat(parseFloat(params.row.premium).toFixed(4))+"%" }</div>
)} },
]}
pageSize={this.state.loading ? 0 : this.state.pageSize}
2022-03-06 21:10:57 +00:00
rowsPerPageOptions={[6,20,50]}
onPageSizeChange={(newPageSize) => this.setState({pageSize:newPageSize})}
2022-01-14 21:40:54 +00:00
onRowClick={(params) => this.handleRowClick(params.row.id)} // Whole row is clickable, but the mouse only looks clickly in some places.
/>
</div>
);
2022-01-05 13:39:58 +00:00
}
2022-01-27 14:40:14 +00:00
bookListTablePhone=()=>{
const { t } = this.props;
2022-01-27 14:40:14 +00:00
return (
<div style={{ height: 422, width: '100%' }}>
2022-01-27 14:40:14 +00:00
<DataGrid
loading={this.state.loading}
2022-01-27 14:40:14 +00:00
rows={
this.state.orders.map((order) =>
({id: order.id,
avatar: window.location.origin +'/static/assets/avatars/' + order.maker_nick + '.png',
2022-02-03 18:06:30 +00:00
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,
2022-01-27 14:40:14 +00:00
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,
2022-01-27 14:40:14 +00:00
renderCell: (params) => {return (
<Tooltip placement="right" enterTouchDelay="0" title={params.row.robot+" ("+t(params.row.robot_status)+")"}>
2022-02-03 18:06:30 +00:00
<Badge variant="dot" overlap="circular" badgeContent="" color={this.statusBadgeColor(params.row.robot_status)}>
<div style={{ width: 45, height: 45 }}>
2022-02-01 19:43:33 +00:00
<Image className='bookAvatar'
disableError='true'
disableSpinner='true'
color='null'
2022-02-03 18:06:30 +00:00
alt={params.row.robot}
2022-02-01 19:43:33 +00:00
src={params.row.avatar}
/>
</div>
2022-02-03 18:06:30 +00:00
</Badge>
</Tooltip>
2022-01-27 14:40:14 +00:00
);
} },
{ field: 'type', headerName: t("Is"), width: 60, hide:'true'},
{ field: 'amount', headerName: t("Amount"), type: 'number', width: 84,
2022-02-03 18:06:30 +00:00
renderCell: (params) => {return (
<Tooltip placement="right" enterTouchDelay="0" title={t(params.row.type)}>
<div style={{ cursor: "pointer" }}>{this.amountToString(params.row.amount,params.row.has_range, params.row.min_amount, params.row.max_amount)}</div>
2022-02-03 18:06:30 +00:00
</Tooltip>
)} },
{ field: 'currency', headerName: t("Currency"), width: 85,
2022-01-27 14:40:14 +00:00
renderCell: (params) => {return (
// <Tooltip placement="left" enterTouchDelay="0" title={params.row.payment_method}>
2022-02-19 18:44:02 +00:00
<div style={{ cursor: "pointer", display:'flex',alignItems:'center', flexWrap:'wrap'}}>{params.row.currency+" "}{getFlags(params.row.currency)}</div>
// </Tooltip>
2022-01-27 14:40:14 +00:00
)} },
{ field: 'payment_method', headerName: t("Payment Method"), width: 180, hide:'true'},
{ field: 'payment_icons', headerName: t("Pay"), width: 75 ,
renderCell: (params) => {return (
<div style={{position:'relative', left:'-4px', cursor: "pointer", align:"center"}}><PaymentText othersText={t("Others")} size={16} text={params.row.payment_method}/></div>
)} },
{ field: 'price', headerName: t("Price"), type: 'number', width: 140, hide:'true',
2022-01-27 14:40:14 +00:00
renderCell: (params) => {return (
<div style={{ cursor: "pointer" }}>{this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }</div>
)} },
{ field: 'premium', headerName: t("Premium"), type: 'number', width: 85,
2022-01-27 14:40:14 +00:00
renderCell: (params) => {return (
<Tooltip placement="left" enterTouchDelay="0" title={this.pn(params.row.price) + " " +params.row.currency+ "/BTC" }>
2022-01-27 14:40:14 +00:00
<div style={{ cursor: "pointer" }}>{parseFloat(parseFloat(params.row.premium).toFixed(4))+"%" }</div>
</Tooltip>
2022-01-27 14:40:14 +00:00
)} },
]}
pageSize={this.state.loading ? 0 : this.state.pageSize}
2022-03-06 21:10:57 +00:00
rowsPerPageOptions={[6,20,50]}
onPageSizeChange={(newPageSize) => this.setState({pageSize:newPageSize})}
2022-01-27 14:40:14 +00:00
onRowClick={(params) => this.handleRowClick(params.row.id)} // Whole row is clickable, but the mouse only looks clickly in some places.
2022-03-06 21:10:57 +00:00
2022-01-27 14:40:14 +00:00
/>
</div>
);
}
render() {
const { t } = this.props;
return (
2022-01-31 03:04:54 +00:00
<Grid className='orderBook' container spacing={1} sx={{minWidth:400}}>
{/* <Grid item xs={12} align="center">
<Typography component="h4" variant="h4">ORDER BOOK</Typography>
</Grid> */}
<Grid item xs={6} align="right">
<FormControl align="center">
<FormHelperText align="center">
{t("I want to")}
</FormHelperText>
<Select
Spanish translation (#96) * Translate tp Spanish (#93) * update_es.json * Update es.json Translate eng to esp from line 222 to end Co-authored-by: mamifiero <99733022+mamifiero@users.noreply.github.com> * Fit book and maker UI for variable length text * Correction and style of the Spanish translation (#95) * Correction and style of the Spanish translation The following terms have been unified and adjusted only in Spanish, but can be done in English base too: * Satoshis, satoshis, sats = Sats (in honour of RoboSats) * Robot (always first capital letter in honour of RoboSats) * Lightning, lightning, LN, Lightning Network = Lightningç * Telegram, TG = Telegram * Trade, Swap = Trade (Intercambio) * Trade counterpart, Peer, Trading peer = Peer (Compañero) * Hold invoice, Bond & Fidelity Bond = Bond (Fianza) * Trade collateral, Collateral, Trade escrow, Escrow = Collateral (Colateral) * Burner contact method, Burner email = Burner contact method (Método de contacto de usar y tirar) * Hidden service (deprecated), Onion services, .onion site = Onion site (sitio cebolla) https://tb-manual.torproject.org/es/onion-services/ * Tor Browser (Navegador Tor) https://tb-manual.torproject.org/es/about/ * Craiglist is not common on spanish countries, so now inserted WallaPop * Update es.json Co-authored-by: decentralized.b <58108487+decentralizedb@users.noreply.github.com> Co-authored-by: mamifiero <99733022+mamifiero@users.noreply.github.com> Co-authored-by: ibertario <68381662+ibertario@users.noreply.github.com>
2022-04-15 16:15:57 +00:00
sx={{width:130}}
autoWidth={true}
label={t("Select Order Type")}
required="true"
value={this.props.type}
inputProps={{
style: {textAlign:"center"}
}}
onChange={this.handleTypeChange}
> <MenuItem value={2}>{t("ANY_type")}</MenuItem>
<MenuItem value={1}>{t("BUY")}</MenuItem>
<MenuItem value={0}>{t("SELL")}</MenuItem>
</Select>
</FormControl>
</Grid>
<Grid item xs={6} align="left">
<FormControl align="center">
<FormHelperText align="center">
{this.props.type == 0 ? t("and receive") : (this.props.type == 1 ? t("and pay with") : t("and use") )}
</FormHelperText>
<Select
//autoWidth={true}
Spanish translation (#96) * Translate tp Spanish (#93) * update_es.json * Update es.json Translate eng to esp from line 222 to end Co-authored-by: mamifiero <99733022+mamifiero@users.noreply.github.com> * Fit book and maker UI for variable length text * Correction and style of the Spanish translation (#95) * Correction and style of the Spanish translation The following terms have been unified and adjusted only in Spanish, but can be done in English base too: * Satoshis, satoshis, sats = Sats (in honour of RoboSats) * Robot (always first capital letter in honour of RoboSats) * Lightning, lightning, LN, Lightning Network = Lightningç * Telegram, TG = Telegram * Trade, Swap = Trade (Intercambio) * Trade counterpart, Peer, Trading peer = Peer (Compañero) * Hold invoice, Bond & Fidelity Bond = Bond (Fianza) * Trade collateral, Collateral, Trade escrow, Escrow = Collateral (Colateral) * Burner contact method, Burner email = Burner contact method (Método de contacto de usar y tirar) * Hidden service (deprecated), Onion services, .onion site = Onion site (sitio cebolla) https://tb-manual.torproject.org/es/onion-services/ * Tor Browser (Navegador Tor) https://tb-manual.torproject.org/es/about/ * Craiglist is not common on spanish countries, so now inserted WallaPop * Update es.json Co-authored-by: decentralized.b <58108487+decentralizedb@users.noreply.github.com> Co-authored-by: mamifiero <99733022+mamifiero@users.noreply.github.com> Co-authored-by: ibertario <68381662+ibertario@users.noreply.github.com>
2022-04-15 16:15:57 +00:00
sx={{width:130}}
label={t("Select Payment Currency")}
required="true"
value={this.props.currency}
inputProps={{
style: {textAlign:"center"}
}}
onChange={this.handleCurrencyChange}
2022-04-10 15:11:11 +00:00
> <MenuItem value={0}><div style={{display:'flex',alignItems:'center', flexWrap:'wrap'}}>{getFlags('ANY')}{" "+t("ANY_currency")}</div></MenuItem>
{
Object.entries(currencyDict)
2022-02-19 18:44:02 +00:00
.map( ([key, value]) => <MenuItem value={parseInt(key)}><div style={{display:'flex',alignItems:'center', flexWrap:'wrap'}}>{getFlags(value)}{" "+value}</div></MenuItem> )
}
</Select>
</FormControl>
</Grid>
2022-01-05 13:39:58 +00:00
{ this.state.not_found ? "" :
<Grid item xs={12} align="center">
<Typography component="h5" variant="h5">
{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")
)
}
</Typography>
</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">
{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})
}
2022-01-05 13:39:58 +00:00
</Typography>
</Grid>
<br/>
2022-01-05 13:39:58 +00:00
<Grid item>
<Button size="large" variant="contained" color='primary' to='/make/' component={Link}>{t("Make Order")}</Button>
2022-01-05 13:39:58 +00:00
</Grid>
2022-04-01 14:52:44 +00:00
<Typography color="primary" component="body1" variant="body1">
{t("Be the first one to create an order")}
<br/>
<br/>
2022-01-08 11:51:55 +00:00
</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-27 14:40:14 +00:00
{/* Desktop Book */}
2022-03-06 21:10:57 +00:00
<MediaQuery minWidth={930}>
<Paper elevation={0} style={{width: 925, maxHeight: 500, overflow: 'auto'}}>
<this.bookListTableDesktop/>
2022-01-27 14:40:14 +00:00
</Paper>
</MediaQuery>
2022-01-14 21:40:54 +00:00
2022-01-27 14:40:14 +00:00
{/* Smartphone Book */}
2022-03-06 21:10:57 +00:00
<MediaQuery maxWidth={929}>
<Paper elevation={0} style={{width: 395, maxHeight: 450, overflow: 'auto'}}>
<this.bookListTablePhone/>
2022-01-27 14:40:14 +00:00
</Paper>
</MediaQuery>
2022-01-10 10:49:21 +00:00
</Grid>
2022-01-05 13:39:58 +00:00
}
<Grid item xs={12} align="center">
<Button color="secondary" variant="contained" to="/" component={Link}>
{t("Back")}
</Button>
</Grid>
</Grid>
);
};
}
export default withTranslation()(BookPage);