import React, { useState, useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { Dialog, DialogTitle, DialogContentText, DialogActions, DialogContent, Box, Button, Tooltip, Grid, TextField, useTheme, Typography, } from '@mui/material'; import Countdown from 'react-countdown'; import currencies from '../../../static/assets/currencies.json'; import { apiClient } from '../../services/api'; import { Order } from '../../models'; import { ConfirmationDialog } from '../Dialogs'; import { Page } from '../../basic/NavBar'; import { LoadingButton } from '@mui/lab'; interface TakeButtonProps { order: Order; setOrder: (state: Order) => void; baseUrl: string; hasRobot: boolean; } interface OpenDialogsProps { inactiveMaker: boolean; confirmation: boolean; } const closeAll = { inactiveMaker: false, confirmation: false }; const TakeButton = ({ order, setOrder, baseUrl, hasRobot }: TakeButtonProps): JSX.Element => { const { t } = useTranslation(); const theme = useTheme(); const [takeAmount, setTakeAmount] = useState(''); const [badRequest, setBadRequest] = useState(''); const [loadingTake, setLoadingTake] = useState(false); const [open, setOpen] = useState(closeAll); const currencyCode: string = order.currency == 1000 ? 'Sats' : currencies[`${order.currency}`]; const InactiveMakerDialog = function () { return ( setOpen({ ...open, inactiveMaker: false })}> {t('The maker is away')} {t( 'By taking this order you risk wasting your time. If the maker does not proceed in time, you will be compensated in satoshis for 50% of the maker bond.', )} ); }; const countdownTakeOrderRenderer = function ({ seconds, completed }) { if (isNaN(seconds) || completed) { return takeOrderButton(); } else { return ( {t('Take Order')} ); } }; const handleTakeAmountChange = function (e) { if (e.target.value != '' && e.target.value != null) { setTakeAmount(`${parseFloat(e.target.value)}`); } else { setTakeAmount(e.target.value); } }; const amountHelperText = useMemo(() => { const amount = order.currency == 1000 ? Number(takeAmount) / 100000000 : Number(takeAmount); if (amount < Number(order.min_amount) && takeAmount != '') { return t('Too low'); } else if (amount > Number(order.max_amount) && takeAmount != '') { return t('Too high'); } else { return null; } }, [order, takeAmount]); const onTakeOrderClicked = function () { if (order.maker_status == 'Inactive') { setOpen({ inactiveMaker: true, confirmation: false }); } else { setOpen({ inactiveMaker: false, confirmation: true }); } }; const invalidTakeAmount = useMemo(() => { const amount = order.currency == 1000 ? Number(takeAmount) / 100000000 : Number(takeAmount); return ( amount < Number(order.min_amount) || amount > Number(order.max_amount) || takeAmount == '' || takeAmount == null ); }, [takeAmount, order]); const takeOrderButton = function () { if (order.has_range) { return (
{t('Take Order')}
{t('Take Order')}
); } else { return ( {t('Take Order')} ); } }; const takeOrder = function () { setLoadingTake(true); apiClient .post(baseUrl, '/api/order/?order_id=' + order.id, { action: 'take', amount: order.currency == 1000 ? takeAmount / 100000000 : takeAmount, }) .then((data) => { setLoadingTake(false); if (data.bad_request) { setBadRequest(data.bad_request); } else { setOrder(data); setBadRequest(''); } }); }; return ( {badRequest != '' ? ( {t(badRequest)} ) : ( <> )} setOpen({ ...open, confirmation: false })} onClickDone={() => { takeOrder(); setLoadingTake(true); setOpen(closeAll); }} hasRobot={hasRobot} /> ); }; export default TakeButton;