robosats/frontend/src/components/TradeBox.js

460 lines
17 KiB
JavaScript
Raw Normal View History

import React, { Component } from "react";
import { Link, Paper, Rating, Button, Grid, Typography, TextField, List, ListItem, ListItemText, Divider} from "@mui/material"
2022-01-09 01:23:13 +00:00
import QRCode from "react-qr-code";
import Chat from "./Chat"
2022-01-09 14:07:05 +00:00
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;
}
}
2022-01-09 01:23:13 +00:00
}
2022-01-09 14:07:05 +00:00
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
2022-01-09 01:23:13 +00:00
2022-01-09 14:07:05 +00:00
// pretty numbers
function pn(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
2022-01-09 01:23:13 +00:00
2022-01-09 14:07:05 +00:00
export default class TradeBox extends Component {
constructor(props) {
super(props);
this.state = {
badInvoice: false,
}
2022-01-09 01:23:13 +00:00
}
2022-01-09 12:14:11 +00:00
showQRInvoice=()=>{
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="body2" variant="body2">
2022-01-10 18:47:16 +00:00
Robosats show commitment to their peers
</Typography>
</Grid>
<Grid item xs={12} align="center">
2022-01-09 14:07:05 +00:00
{this.props.data.isMaker ?
2022-01-10 18:47:16 +00:00
<Typography color="primary" component="subtitle1" variant="subtitle1">
2022-01-09 14:07:05 +00:00
<b>Lock {pn(this.props.data.bondSatoshis)} Sats to PUBLISH order </b>
</Typography>
:
2022-01-10 18:47:16 +00:00
<Typography color="primary" component="subtitle1" variant="subtitle1">
2022-01-09 14:07:05 +00:00
<b>Lock {pn(this.props.data.bondSatoshis)} Sats to TAKE the order </b>
</Typography>
}
</Grid>
<Grid item xs={12} align="center">
2022-01-09 14:07:05 +00:00
<QRCode value={this.props.data.bondInvoice} size={305}/>
</Grid>
<Grid item xs={12} align="center">
<TextField
hiddenLabel
variant="filled"
size="small"
2022-01-09 14:07:05 +00:00
defaultValue={this.props.data.bondInvoice}
disabled="true"
2022-01-09 20:05:19 +00:00
helperText="This is a hold invoice. It will not be charged if the order succeeds or expires.
2022-01-08 15:34:09 +00:00
It will be charged if the order is cancelled or you lose a dispute."
color = "secondary"
/>
</Grid>
</Grid>
);
}
2022-01-09 20:05:19 +00:00
showBondIsLocked=()=>{
return (
<Grid item xs={12} align="center">
<Typography color="primary" component="subtitle1" variant="subtitle1" align="center">
🔒 Your {this.props.data.isMaker ? 'maker' : 'taker'} bond is safely locked
</Typography>
</Grid>
);
}
2022-01-09 12:14:11 +00:00
showEscrowQRInvoice=()=>{
2022-01-08 17:19:30 +00:00
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
2022-01-10 18:47:16 +00:00
<Typography color="primary" component="subtitle1" variant="subtitle1">
2022-01-09 14:07:05 +00:00
<b>Deposit {pn(this.props.data.escrowSatoshis)} Sats as trade collateral </b>
2022-01-08 17:19:30 +00:00
</Typography>
</Grid>
<Grid item xs={12} align="center">
2022-01-09 14:07:05 +00:00
<QRCode value={this.props.data.escrowInvoice} size={305}/>
2022-01-08 17:19:30 +00:00
</Grid>
<Grid item xs={12} align="center">
<TextField
hiddenLabel
variant="filled"
size="small"
2022-01-09 14:07:05 +00:00
defaultValue={this.props.data.escrowInvoice}
2022-01-08 17:19:30 +00:00
disabled="true"
2022-01-09 20:05:19 +00:00
helperText="This is a hold LN invoice. It will be charged once the buyer confirms he sent the fiat."
2022-01-08 17:19:30 +00:00
color = "secondary"
/>
</Grid>
{this.showBondIsLocked()}
2022-01-08 17:19:30 +00:00
</Grid>
);
}
2022-01-09 01:23:13 +00:00
showTakerFound=()=>{
2022-01-09 14:07:05 +00:00
// TODO Make some sound here! The maker might have been waiting for long
2022-01-09 01:23:13 +00:00
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="subtitle1" variant="subtitle1">
<b>A taker has been found! </b>
</Typography>
</Grid>
<Divider/>
<Grid item xs={12} align="center">
2022-01-09 15:28:12 +00:00
<Typography component="body2" variant="body2">
2022-01-09 01:23:13 +00:00
Please wait for the taker to confirm his commitment by locking a bond.
</Typography>
</Grid>
{this.showBondIsLocked()}
2022-01-09 01:23:13 +00:00
</Grid>
);
}
2022-01-08 15:34:09 +00:00
showMakerWait=()=>{
return (
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="subtitle1" variant="subtitle1">
2022-01-10 18:47:16 +00:00
<b> Your order is public. Wait for a taker. </b>
2022-01-08 15:34:09 +00:00
</Typography>
</Grid>
<Grid item xs={12} align="center">
<List dense="true">
<Divider/>
<ListItem>
<Typography component="body2" variant="body2" align="left">
<p>Be patient while robots check the book.
It might take some time. This box will ring 🔊 once a robot takes your order. </p>
<p>Please note that if your premium is too high, or if your currency or payment
methods are not popular, your order might expire untaken. Your bond will
return to you (no action needed).</p>
</Typography>
</ListItem>
{/* TODO API sends data for a more confortable wait */}
<Divider/>
<ListItem>
<ListItemText primary={999} secondary="Robots looking at the book"/>
</ListItem>
<Divider/>
<ListItem>
2022-01-09 14:07:05 +00:00
<ListItemText primary={999} secondary={"Active orders for " + this.props.data.currencyCode}/>
2022-01-08 15:34:09 +00:00
</ListItem>
<Divider/>
<ListItem>
<ListItemText primary="33%" secondary="Premium percentile" />
</ListItem>
<Divider/>
2022-01-08 15:34:09 +00:00
</List>
</Grid>
{this.showBondIsLocked()}
2022-01-08 15:34:09 +00:00
</Grid>
)
}
2022-01-09 14:07:05 +00:00
handleInputInvoiceChanged=(e)=>{
this.setState({
invoice: e.target.value,
badInvoice: false,
2022-01-09 14:07:05 +00:00
});
}
2022-01-09 12:14:11 +00:00
2022-01-09 20:05:19 +00:00
// Fix this. It's clunky because it takes time. this.props.data does not refresh until next refresh of OrderPage.
2022-01-09 15:28:12 +00:00
2022-01-09 14:07:05 +00:00
handleClickSubmitInvoiceButton=()=>{
this.setState({badInvoice:false});
2022-01-09 14:07:05 +00:00
const requestOptions = {
method: 'POST',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken'),},
body: JSON.stringify({
'action':'update_invoice',
'invoice': this.state.invoice,
}),
};
fetch('/api/order/' + '?order_id=' + this.props.data.id, requestOptions)
.then((response) => response.json())
.then((data) => this.setState({badInvoice:data.bad_invoice})
& console.log(data));
2022-01-09 14:07:05 +00:00
}
2022-01-09 12:14:11 +00:00
2022-01-09 14:07:05 +00:00
showInputInvoice(){
return (
2022-01-09 12:14:11 +00:00
2022-01-09 14:07:05 +00:00
// TODO Camera option to read QR
<Grid container spacing={1}>
<Grid item xs={12} align="center">
2022-01-10 18:47:16 +00:00
<Typography color="primary" component="subtitle1" variant="subtitle1">
2022-01-09 14:07:05 +00:00
<b> Submit a LN invoice for {pn(this.props.data.invoiceAmount)} Sats </b>
</Typography>
</Grid>
<Grid item xs={12} align="left">
<Typography component="body2" variant="body2">
The taker is committed! Before letting you send {" "+ parseFloat(parseFloat(this.props.data.amount).toFixed(4))+
" "+ this.props.data.currencyCode}, we want to make sure you are able to receive the BTC. Please provide a
valid invoice for {pn(this.props.data.invoiceAmount)} Satoshis.
</Typography>
</Grid>
<Grid item xs={12} align="center">
<TextField
error={this.state.badInvoice}
helperText={this.state.badInvoice ? this.state.badInvoice : "" }
2022-01-09 14:07:05 +00:00
label={"Payout Lightning Invoice"}
required
inputProps={{
style: {textAlign:"center"}
}}
multiline
onChange={this.handleInputInvoiceChanged}
/>
</Grid>
<Grid item xs={12} align="center">
<Button variant='contained' color='primary' onClick={this.handleClickSubmitInvoiceButton}>Submit</Button>
</Grid>
{this.showBondIsLocked()}
2022-01-09 14:07:05 +00:00
</Grid>
)
}
showWaitingForEscrow(){
2022-01-09 15:28:12 +00:00
return(
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="subtitle1" variant="subtitle1">
<b>Your invoice looks good!</b>
</Typography>
</Grid>
<Grid item xs={12} align="center">
<Typography component="body2" variant="body2" align="left">
<p>We are waiting for the seller to deposit the full trade BTC amount
into the escrow.</p>
<p> Just hang on for a moment. If the seller does not deposit,
you will get your bond back automatically.</p>
</Typography>
</Grid>
{this.showBondIsLocked()}
2022-01-09 15:28:12 +00:00
</Grid>
)
}
showWaitingForBuyerInvoice(){
return(
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="subtitle1" variant="subtitle1">
<b>The trade collateral is locked! :D </b>
</Typography>
</Grid>
<Grid item xs={12} align="center">
<Typography component="body2" variant="body2" align="left">
<p> We are waiting for the buyer to post a lightning invoice. Once
he does, you will be able to directly communicate the fiat payment
details. </p>
<p> Just hang on for a moment. If the buyer does not cooperate,
you will get back the trade collateral and your bond automatically.</p>
</Typography>
</Grid>
{this.showBondIsLocked()}
2022-01-09 15:28:12 +00:00
</Grid>
)
2022-01-09 14:07:05 +00:00
}
2022-01-09 12:14:11 +00:00
2022-01-09 20:05:19 +00:00
handleClickConfirmButton=()=>{
2022-01-09 15:28:12 +00:00
const requestOptions = {
method: 'POST',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken'),},
body: JSON.stringify({
2022-01-09 20:05:19 +00:00
'action': "confirm",
2022-01-09 15:28:12 +00:00
}),
};
fetch('/api/order/' + '?order_id=' + this.props.data.id, requestOptions)
.then((response) => response.json())
.then((data) => (this.props.data = data));
}
2022-01-09 20:05:19 +00:00
handleClickOpenDisputeButton=()=>{
const requestOptions = {
method: 'POST',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken'),},
body: JSON.stringify({
'action': "dispute",
}),
};
fetch('/api/order/' + '?order_id=' + this.props.data.id, requestOptions)
.then((response) => response.json())
.then((data) => (this.props.data = data));
}
handleRatingChange=(e)=>{
const requestOptions = {
method: 'POST',
headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken'),},
body: JSON.stringify({
'action': "rate",
'rating': e.target.value,
}),
};
fetch('/api/order/' + '?order_id=' + this.props.data.id, requestOptions)
.then((response) => response.json())
.then((data) => (this.props.data = data));
}
2022-01-09 12:14:11 +00:00
2022-01-09 15:28:12 +00:00
showFiatSentButton(){
return(
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Button defaultValue="confirm" variant='contained' color='secondary' onClick={this.handleClickConfirmButton}>Confirm {this.props.data.currencyCode} sent</Button>
2022-01-09 20:05:19 +00:00
</Grid>
</Grid>
)
}
showFiatReceivedButton(){
// TODO, show alert and ask for double confirmation (Have you check you received the fiat? Confirming fiat received settles the trade.)
// Ask for double confirmation.
return(
<Grid item xs={12} align="center">
<Button defaultValue="confirm" variant='contained' color='secondary' onClick={this.handleClickConfirmButton}>Confirm {this.props.data.currencyCode} received</Button>
2022-01-09 20:05:19 +00:00
</Grid>
)
}
showOpenDisputeButton(){
// TODO, show alert about how opening a dispute might involve giving away personal data and might mean losing the bond. Ask for double confirmation.
return(
<Grid item xs={12} align="center">
<Button color="inherit" onClick={this.handleClickOpenDisputeButton}>Open Dispute</Button>
2022-01-09 20:05:19 +00:00
</Grid>
)
}
showChat(sendFiatButton, receivedFiatButton, openDisputeButton){
return(
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="subtitle1" variant="subtitle1">
<b>Chatting with {this.props.data.isMaker ? this.props.data.takerNick : this.props.data.makerNick}</b>
</Typography>
</Grid>
<Grid item xs={12} align="left">
{this.props.data.isSeller ?
<Typography component="body2" variant="body2">
Say hi to your peer robot! Be helpful and concise. Let him know how to send you {this.props.data.currencyCode}.
</Typography>
:
<Typography component="body2" variant="body2">
Say hi to your peer robot! Ask for payment details and click 'Confirm {this.props.data.currencyCode} sent' as soon as you send the payment.
</Typography>
}
</Grid>
<Chat data={this.props.data}/>
2022-01-09 20:05:19 +00:00
<Grid item xs={12} align="center">
{openDisputeButton ? this.showOpenDisputeButton() : ""}
{sendFiatButton ? this.showFiatSentButton() : ""}
{receivedFiatButton ? this.showFiatReceivedButton() : ""}
2022-01-09 15:28:12 +00:00
</Grid>
{this.showBondIsLocked()}
2022-01-09 15:28:12 +00:00
</Grid>
)
}
showRateSelect(){
return(
<Grid container spacing={1}>
<Grid item xs={12} align="center">
<Typography component="h6" variant="h6">
🎉Trade finished!🥳
</Typography>
</Grid>
<Grid item xs={12} align="center">
<Typography component="body2" variant="body2" align="center">
What do you think of <b>{this.props.data.isMaker ? this.props.data.takerNick : this.props.data.makerNick}</b>?
</Typography>
</Grid>
<Grid item xs={12} align="center">
<Rating name="size-large" defaultValue={2} size="large" onChange={this.handleRatingChange} />
</Grid>
<Grid item xs={12} align="center">
<Button color='primary' to='/' component={Link}>Start Again</Button>
</Grid>
</Grid>
)
}
2022-01-09 12:14:11 +00:00
render() {
return (
2022-01-08 15:34:09 +00:00
<Grid container spacing={1} style={{ width:330}}>
<Grid item xs={12} align="center">
<Typography component="h5" variant="h5">
TradeBox
</Typography>
<Paper elevation={12} style={{ padding: 8,}}>
2022-01-09 12:14:11 +00:00
{/* Maker and taker Bond request */}
2022-01-09 14:07:05 +00:00
{this.props.data.bondInvoice ? this.showQRInvoice() : ""}
2022-01-09 12:14:11 +00:00
{/* Waiting for taker and taker bond request */}
2022-01-09 14:07:05 +00:00
{this.props.data.isMaker & this.props.data.statusCode == 1 ? this.showMakerWait() : ""}
{this.props.data.isMaker & this.props.data.statusCode == 3 ? this.showTakerFound() : ""}
2022-01-09 12:14:11 +00:00
{/* Send Invoice (buyer) and deposit collateral (seller) */}
2022-01-09 14:07:05 +00:00
{this.props.data.isSeller & this.props.data.escrowInvoice != null ? this.showEscrowQRInvoice() : ""}
{this.props.data.isBuyer & this.props.data.invoiceAmount != null ? this.showInputInvoice() : ""}
{this.props.data.isBuyer & this.props.data.statusCode == 7 ? this.showWaitingForEscrow() : ""}
{this.props.data.isSeller & this.props.data.statusCode == 8 ? this.showWaitingForBuyerInvoice() : ""}
2022-01-09 12:14:11 +00:00
{/* In Chatroom - No fiat sent - showChat(showSendButton, showReveiceButton, showDisputeButton) */}
2022-01-09 20:05:19 +00:00
{this.props.data.isBuyer & this.props.data.statusCode == 9 ? this.showChat(true,false,true) : ""}
{this.props.data.isSeller & this.props.data.statusCode == 9 ? this.showChat(false,false,true) : ""}
{/* In Chatroom - Fiat sent - showChat(showSendButton, showReveiceButton, showDisputeButton) */}
2022-01-09 20:05:19 +00:00
{this.props.data.isBuyer & this.props.data.statusCode == 10 ? this.showChat(false,false,true) : ""}
{this.props.data.isSeller & this.props.data.statusCode == 10 ? this.showChat(false,true,true) : ""}
2022-01-09 12:14:11 +00:00
{/* Trade Finished */}
2022-01-12 14:26:26 +00:00
{(this.props.data.isSeller & this.props.data.statusCode > 12 & this.props.data.statusCode < 15) ? this.showRateSelect() : ""}
{(this.props.data.isBuyer & this.props.data.statusCode == 14) ? this.showRateSelect() : ""}
{/* Trade Finished - Payment Routing Failed */}
{this.props.data.isBuyer & this.props.data.statusCode == 15 ? this.showUpdateInvoice() : ""}
{/* Trade Finished - Payment Routing Failed - TODO Needs more planning */}
{this.props.data.statusCode == 11 ? this.showInDispute() : ""}
2022-01-09 12:14:11 +00:00
{/* TODO */}
{/* */}
{/* */}
2022-01-09 12:35:19 +00:00
</Paper>
</Grid>
</Grid>
);
}
}