import React from 'react' import withObservables from '@nozbe/with-observables' import { Q } from '@nozbe/watermelondb' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' import PropTypes from 'prop-types' import { Formik } from 'formik' import { withStyles } from '@material-ui/core/styles' import Card from '@material-ui/core/Card' import CardActions from '@material-ui/core/CardActions' import CardContent from '@material-ui/core/CardContent' import Button from '@material-ui/core/Button' import Typography from '@material-ui/core/Typography' import TextField from '@material-ui/core/TextField' import Collapse from '@material-ui/core/Collapse' import LiquidPledging from 'Embark/contracts/LiquidPledging' import LPVault from 'Embark/contracts/LPVault' import { getTokenLabel } from '../../utils/currencies' import { toWei } from '../../utils/conversions' import styles from './CardStyles' import { useRowData } from './hooks' const { withdraw } = LiquidPledging.methods const { confirmPayment } = LPVault.methods function Withdraw({ handleClose, classes, rowData, authorizedPayment }) { const { show, close } = useRowData(rowData, handleClose) const isPaying = rowData.pledgeState === 'Paying' return ( { const { amount } = values const paymentId = isPaying ? authorizedPayment[0]['returnValues']['idPayment'] : rowData.idPledge const args = isPaying ? [paymentId] : [paymentId, toWei(amount)] const sendFn = isPaying ? confirmPayment : withdraw try { const toSend = sendFn(...args) const estimateGas = await toSend.estimateGas() toSend .send({ gas: estimateGas }) .then(res => { console.log({res}) }) .catch(e => { console.log({e}) }) .finally(() => { close() }) } catch (error) { console.log(error) } }} > {({ values, errors, touched, handleChange, handleBlur, handleSubmit, setFieldValue, setStatus, status }) => (
{`${isPaying ? 'Confirm' : ''} Withdraw${isPaying ? 'al' : ''} ${values.amount || ''} ${values.amount ? getTokenLabel(rowData.pledge.token) : ''} from Pledge ${rowData.idPledge}`} {!isPaying && }
)}
) } Withdraw.propTypes = { classes: PropTypes.object.isRequired, rowData: PropTypes.object.isRequired, handleClose: PropTypes.func.isRequired, authorizedPayment: PropTypes.array.isRequired } const styledWithdraw = withStyles(styles)(Withdraw) export default withDatabase(withObservables(['rowData'], ({ database, rowData }) => ({ authorizedPayment : database.collections.get('vault_events').query( Q.where('ref', rowData.idPledge) ).observe() }))(styledWithdraw))