import React, { Component } from 'react';
import { withTranslation } from 'react-i18next';
import {
Tooltip,
Stack,
Paper,
Button,
ToggleButtonGroup,
ToggleButton,
ListItemButton,
Typography,
Grid,
Select,
MenuItem,
FormControl,
FormHelperText,
ListItemText,
ListItemAvatar,
IconButton,
ButtonGroup,
} 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 FlagWithProps from './FlagWithProps';
import { pn, amountToString } from '../utils/prettyNumbers';
import PaymentText from './PaymentText';
import DepthChart from './Charts/DepthChart';
import RobotAvatar from './Robots/RobotAvatar';
import { apiClient } from '../services/api/index';
// Icons
import { BarChart, FormatListBulleted, Refresh } from '@mui/icons-material';
class BookPage extends Component {
constructor(props) {
super(props);
this.state = {
pageSize: 6,
view: 'list',
};
}
componentDidMount() {
this.getOrderDetails(2, 0);
}
getOrderDetails(type, currency) {
this.props.setAppState({ bookLoading: true });
apiClient.get('/api/book/').then((data) =>
this.props.setAppState({
bookNotFound: data.not_found,
bookLoading: false,
bookOrders: data,
}),
);
}
handleRowClick = (e) => {
this.props.history.push('/order/' + e);
};
handleCurrencyChange = (e) => {
const currency = e.target.value;
this.props.setAppState({
currency,
bookCurrencyCode: this.getCurrencyCode(currency),
});
};
getCurrencyCode(val) {
const { t } = this.props;
if (val) {
return val == 0 ? t('ANY_currency') : currencyDict[val.toString()];
} else {
return t('ANY_currency');
}
}
// Colors for the status badges
statusBadgeColor(status) {
if (status === 'Active') {
return 'success';
}
if (status === 'Seen recently') {
return 'warning';
}
if (status === 'Inactive') {
return 'error';
}
}
dataGridLocaleText = () => {
const { t } = this.props;
return {
MuiTablePagination: { labelRowsPerPage: t('Orders per page:') },
noRowsLabel: t('No rows'),
noResultsOverlayLabel: t('No results found.'),
errorOverlayDefaultLabel: t('An error occurred.'),
toolbarColumns: t('Columns'),
toolbarColumnsLabel: t('Select columns'),
columnsPanelTextFieldLabel: t('Find column'),
columnsPanelTextFieldPlaceholder: t('Column title'),
columnsPanelDragIconLabel: t('Reorder column'),
columnsPanelShowAllButton: t('Show all'),
columnsPanelHideAllButton: t('Hide all'),
filterPanelAddFilter: t('Add filter'),
filterPanelDeleteIconLabel: t('Delete'),
filterPanelLinkOperator: t('Logic operator'),
filterPanelOperators: t('Operator'), // TODO v6: rename to filterPanelOperator
filterPanelOperatorAnd: t('And'),
filterPanelOperatorOr: t('Or'),
filterPanelColumns: t('Columns'),
filterPanelInputLabel: t('Value'),
filterPanelInputPlaceholder: t('Filter value'),
filterOperatorContains: t('contains'),
filterOperatorEquals: t('equals'),
filterOperatorStartsWith: t('starts with'),
filterOperatorEndsWith: t('ends with'),
filterOperatorIs: t('is'),
filterOperatorNot: t('is not'),
filterOperatorAfter: t('is after'),
filterOperatorOnOrAfter: t('is on or after'),
filterOperatorBefore: t('is before'),
filterOperatorOnOrBefore: t('is on or before'),
filterOperatorIsEmpty: t('is empty'),
filterOperatorIsNotEmpty: t('is not empty'),
filterOperatorIsAnyOf: t('is any of'),
filterValueAny: t('any'),
filterValueTrue: t('true'),
filterValueFalse: t('false'),
columnMenuLabel: t('Menu'),
columnMenuShowColumns: t('Show columns'),
columnMenuFilter: t('Filter'),
columnMenuHideColumn: t('Hide'),
columnMenuUnsort: t('Unsort'),
columnMenuSortAsc: t('Sort by ASC'),
columnMenuSortDesc: t('Sort by DESC'),
columnHeaderFiltersLabel: t('Show filters'),
columnHeaderSortIconLabel: t('Sort'),
booleanCellTrueLabel: t('yes'),
booleanCellFalseLabel: t('no'),
};
};
bookListTableDesktop = () => {
const { t } = this.props;
return (
(order.type == this.props.type || this.props.type == null) &&
(order.currency == this.props.currency || this.props.currency == 0),
)}
loading={this.props.bookLoading}
columns={[
// { field: 'id', headerName: 'ID', width: 40 },
{
field: 'maker_nick',
headerName: t('Robot'),
width: 240,
renderCell: (params) => {
return (
);
},
},
{
field: 'type',
headerName: t('Is'),
width: 60,
renderCell: (params) => (params.row.type ? t('Seller') : t('Buyer')),
},
{
field: 'amount',
headerName: t('Amount'),
type: 'number',
width: 90,
renderCell: (params) => {
return (
{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) => {
const currencyCode = this.getCurrencyCode(params.row.currency);
return (
{currencyCode + ' '}
);
},
},
{
field: 'payment_method',
headerName: t('Payment Method'),
width: 180,
renderCell: (params) => {
return (
);
},
},
{
field: 'price',
headerName: t('Price'),
type: 'number',
width: 140,
renderCell: (params) => {
const currencyCode = this.getCurrencyCode(params.row.currency);
return (
{pn(params.row.price) + ' ' + currencyCode + '/BTC'}
);
},
},
{
field: 'premium',
headerName: t('Premium'),
type: 'number',
width: 100,
renderCell: (params) => {
return (
{parseFloat(parseFloat(params.row.premium).toFixed(4)) + '%'}
);
},
},
]}
components={{
NoRowsOverlay: () => (
{this.NoOrdersFound()}
),
NoResultsOverlay: () => (
{t('Filter has no results')}
),
}}
pageSize={this.props.bookLoading ? 0 : this.state.pageSize}
rowsPerPageOptions={[0, 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 (
(order.type == this.props.type || this.props.type == null) &&
(order.currency == this.props.currency || this.props.currency == 0),
)}
columns={[
// { field: 'id', headerName: 'ID', width: 40 },
{
field: 'maker_nick',
headerName: t('Robot'),
width: 64,
renderCell: (params) => {
return (
);
},
},
{
field: 'amount',
headerName: t('Amount'),
type: 'number',
width: 84,
renderCell: (params) => {
return (
{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) => {
const currencyCode = this.getCurrencyCode(params.row.currency);
return (
{currencyCode + ' '}
);
},
},
{ 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 (
{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)) + '%'}
);
},
},
]}
components={{
NoRowsOverlay: () => (
{this.NoOrdersFound()}
),
NoResultsOverlay: () => (
{t('Local filter returns no result')}
),
}}
pageSize={this.props.bookLoading ? 0 : this.state.pageSize}
rowsPerPageOptions={[0, 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.
/>
);
};
handleTypeChange = (mouseEvent, val) => {
this.props.setAppState({ type: val });
};
handleClickView = () => {
this.setState({ view: this.state.view == 'depth' ? 'list' : 'depth' });
};
NoOrdersFound = () => {
const { t } = this.props;
return (
{this.props.type == 0
? t('No orders found to sell BTC for {{currencyCode}}', {
currencyCode: this.props.bookCurrencyCode,
})
: t('No orders found to buy BTC for {{currencyCode}}', {
currencyCode: this.props.bookCurrencyCode,
})}
{t('Be the first one to create an order')}
);
};
mainView = () => {
if (this.props.bookNotFound) {
return this.NoOrdersFound();
}
const components =
this.state.view == 'depth'
? [
,
,
]
: [this.bookListTableDesktop(), this.bookListTablePhone()];
return (
<>
{/* Desktop */}
{components[0]}
{/* Smartphone */}
{components[1]}
>
);
};
getTitle = () => {
const { t } = this.props;
if (this.state.view == 'list') {
if (this.props.type == 0) {
return t('You are SELLING BTC for {{currencyCode}}', {
currencyCode: this.props.bookCurrencyCode,
});
} else if (this.props.type == 1) {
return t('You are BUYING BTC for {{currencyCode}}', {
currencyCode: this.props.bookCurrencyCode,
});
} else {
return t('You are looking at all');
}
} else if (this.state.view == 'depth') {
return t('Depth chart');
}
};
render() {
const { t } = this.props;
return (
this.setState({ loading: true }) & this.getOrderDetails(2, 0)}
>
{t('I want to')}
{t('Buy')}
{t('Sell')}
{this.props.type == 0
? t('and receive')
: this.props.type == 1
? t('and pay with')
: t('and use')}
{this.props.bookNotFound ? (
<>>
) : (
{this.getTitle()}
)}
{this.mainView()}
{!this.props.bookNotFound ? (
<>
>
) : null}
);
}
}
export default withTranslation()(BookPage);