import React, { Component } from 'react';
import { InputAdornment, LinearProgress, Link, Checkbox, Slider, Box, Tab, Tabs, SliderThumb, Tooltip, Paper, Button , Grid, Typography, TextField, Select, FormHelperText, MenuItem, FormControl, Radio, FormControlLabel, RadioGroup} from "@mui/material"
import { LocalizationProvider, TimePicker} from '@mui/lab';
import DateFnsUtils from "@date-io/date-fns";
import { Link as LinkRouter } from 'react-router-dom'
import { styled } from '@mui/material/styles';
import getFlags from './getFlags';
import AutocompletePayments from './autocompletePayments';
import LockIcon from '@mui/icons-material/Lock';
import HourglassTopIcon from '@mui/icons-material/HourglassTop';
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
// pretty numbers
function pn(x) {
if(x==null){
return(null)
}
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
export default class MakerPage extends Component {
defaultCurrency = 1;
defaultCurrencyCode = 'USD';
defaultPaymentMethod = "not specified";
defaultPremium = 0;
minTradeSats = 20000;
maxTradeSats = 800000;
maxBondlessSats = 50000;
maxRangeAmountMultiple = 4.8;
minRangeAmountMultiple = 1.6;
constructor(props) {
super(props);
this.state={
is_explicit: false,
type: 0,
currency: this.defaultCurrency,
currencyCode: this.defaultCurrencyCode,
payment_method: this.defaultPaymentMethod,
premium: 0,
satoshis: null,
currencies_dict: {"1":"USD"},
showAdvanced: false,
allowBondless: false,
publicExpiryTime: new Date(0, 0, 0, 23, 59),
enableAmountRange: false,
minAmount: null,
bondSize: 1,
limits: null,
minAmount: null,
maxAmount: null,
loadingLimits: false,
}
this.getCurrencyDict()
}
getLimits() {
this.setState({loadingLimits:true})
fetch('/api/limits/')
.then((response) => response.json())
.then((data) => this.setState({
limits:data,
loadingLimits:false,
minAmount: this.state.amount ? parseFloat((this.state.amount/2).toPrecision(2)) : parseFloat(Number(data[this.state.currency]['max_amount']*0.25).toPrecision(2)),
maxAmount: this.state.amount ? this.state.amount : parseFloat(Number(data[this.state.currency]['max_amount']*0.75).toPrecision(2)),
}));
}
a11yProps(index) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
handleTypeChange=(e)=>{
this.setState({
type: e.target.value,
});
}
handleCurrencyChange=(e)=>{
this.setState({
currency: e.target.value,
currencyCode: this.getCurrencyCode(e.target.value),
});
if(this.state.enableAmountRange){
this.setState({
minAmount: parseFloat(Number(this.state.limits[e.target.value]['max_amount']*0.25).toPrecision(2)),
maxAmount: parseFloat(Number(this.state.limits[e.target.value]['max_amount']*0.75).toPrecision(2)),
})
}
}
handleAmountChange=(e)=>{
this.setState({
amount: e.target.value,
});
}
handleMinAmountChange=(e)=>{
this.setState({
minAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)),
});
}
handleMaxAmountChange=(e)=>{
this.setState({
maxAmount: parseFloat(Number(e.target.value).toPrecision(e.target.value < 100 ? 2 : 3)),
});
}
handleRangeAmountChange = (e, newValue, activeThumb) => {
var maxAmount = this.getMaxAmount();
var minAmount = this.getMinAmount();
var lowerValue = e.target.value[0];
var upperValue = e.target.value[1];
var minRange = this.minRangeAmountMultiple;
var maxRange = this.maxRangeAmountMultiple;
if (lowerValue > maxAmount/minRange){
lowerValue = maxAmount/minRange
}
if (upperValue < minRange*minAmount){
upperValue = minRange*minAmount
}
if (lowerValue > upperValue/minRange) {
if (activeThumb === 0) {
upperValue = minRange*lowerValue
} else {
lowerValue = upperValue/minRange
}
}else if(lowerValue < upperValue/maxRange){
if (activeThumb === 0) {
upperValue = maxRange*lowerValue
} else {
lowerValue = upperValue/maxRange
}
}
this.setState({
minAmount: parseFloat(Number(lowerValue).toPrecision(lowerValue < 100 ? 2 : 3)),
maxAmount: parseFloat(Number(upperValue).toPrecision(upperValue < 100 ? 2 : 3)),
});
}
handlePaymentMethodChange=(value)=>{
this.setState({
payment_method: value,
badPaymentMethod: value.length > 35,
});
}
handlePremiumChange=(e)=>{
if(e.target.value > 999){
var bad_premium = "Must be less than 999%"
}
if(e.target.value < -100){
var bad_premium = "Must be more than -100%"
}
this.setState({
premium: e.target.value,
badPremium: bad_premium,
});
}
handleSatoshisChange=(e)=>{
if(e.target.value > this.maxTradeSats){
var bad_sats = "Must be less than " + pn(this.maxTradeSats)
}
if(e.target.value < this.minTradeSats){
var bad_sats = "Must be more than "+pn(this.minTradeSats)
}
this.setState({
satoshis: e.target.value,
badSatoshis: bad_sats,
});
}
handleClickRelative=(e)=>{
this.setState({
is_explicit: false,
});
this.handlePremiumChange();
}
handleClickExplicit=(e)=>{
this.setState({
is_explicit: true,
});
this.handleSatoshisChange();
}
handleCreateOfferButtonPressed=()=>{
this.state.amount == null ? this.setState({amount: 0}) : null;
const requestOptions = {
method: 'POST',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken')},
body: JSON.stringify({
type: this.state.type,
currency: this.state.currency,
amount: this.state.amount,
has_range: this.state.enableAmountRange,
min_amount: this.state.minAmount,
max_amount: this.state.maxAmount,
payment_method: this.state.payment_method,
is_explicit: this.state.is_explicit,
premium: this.state.is_explicit ? null: this.state.premium,
satoshis: this.state.is_explicit ? this.state.satoshis: null,
public_duration: this.state.publicDuration,
bond_size: this.state.bondSize,
bondless_taker: this.state.allowBondless,
}),
};
fetch("/api/make/",requestOptions)
.then((response) => response.json())
.then((data) => (this.setState({badRequest:data.bad_request})
& (data.id ? this.props.history.push('/order/' + data.id) :"")));
}
getCurrencyDict() {
fetch('/static/assets/currencies.json')
.then((response) => response.json())
.then((data) =>
this.setState({
currencies_dict: data
}));
}
getCurrencyCode(val){
return this.state.currencies_dict[val.toString()]
}
handleInputBondSizeChange = (event) => {
this.setState({bondSize: event.target.value === '' ? 1 : Number(event.target.value)});
};
StandardMakerOptions = () => {
return(