liquid-funding/app/components/TransferDialog.jsx

107 lines
3.5 KiB
React
Raw Normal View History

2018-12-03 20:22:51 +00:00
import React from 'react'
2018-12-03 20:54:24 +00:00
import { Formik } from 'formik'
2018-12-04 19:39:45 +00:00
import LiquidPledgingMock from 'Embark/contracts/LiquidPledgingMock'
2018-12-03 20:22:51 +00:00
import Button from '@material-ui/core/Button'
import TextField from '@material-ui/core/TextField'
import Dialog from '@material-ui/core/Dialog'
import DialogActions from '@material-ui/core/DialogActions'
import DialogContent from '@material-ui/core/DialogContent'
import DialogContentText from '@material-ui/core/DialogContentText'
import DialogTitle from '@material-ui/core/DialogTitle'
2018-12-03 20:54:24 +00:00
import { getTokenLabel } from '../utils/currencies'
2018-12-04 19:39:45 +00:00
import { toWei } from '../utils/conversions'
import { FundingContext } from '../context'
2018-12-04 19:39:45 +00:00
const { transfer } = LiquidPledgingMock.methods
2018-12-03 20:22:51 +00:00
const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => (
2018-12-03 20:54:24 +00:00
<Formik
initialValues={{}}
onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => {
2018-12-04 19:39:45 +00:00
const { owner, id } = row
const { amount, idReceiver } = values
const args = [owner, id, toWei(amount.toString()), idReceiver]
transfer(...args)
.send()
.then(res => {
2018-12-05 17:05:21 +00:00
console.log({res})
const { events: { Transfer: { returnValues } } } = res
transferPledgeAmounts(returnValues)
2018-12-04 19:39:45 +00:00
handleClose()
})
.catch(e => {
console.log({e})
})
.finally(() => { resetForm() })
2018-12-03 20:54:24 +00:00
}}
>
{({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
2018-12-04 19:39:45 +00:00
submitForm,
2018-12-03 20:54:24 +00:00
setFieldValue,
setStatus,
status
}) => (
<form onSubmit={handleSubmit}>
<Dialog
open={!!row}
onClose={handleClose}
aria-labelledby="form-dialog-title"
>
<DialogTitle id="form-dialog-title">Transfer Funds</DialogTitle>
<DialogContent>
<DialogContentText>
2018-12-04 19:39:45 +00:00
{`Transfer ${values.amount || ''} ${values.amount ? getTokenLabel(row[6]) : ''} from Pledge ${row.id} ${values.idReceiver ? 'to Giver/Delegate/Project' : ''} ${values.idReceiver || ''}`}
2018-12-03 20:54:24 +00:00
</DialogContentText>
<TextField
autoFocus
margin="normal"
id="amount"
name="amount"
label="Amount to transfer"
placeholder="Amount to transfer"
variant="outlined"
type="number"
autoComplete="off"
fullWidth
onChange={handleChange}
onBlur={handleBlur}
value={values.amount || ''}
/>
<TextField
margin="normal"
id="idReceiver"
name="idReceiver"
label="Receiver of funds"
placeholder="Receiver of funds"
variant="outlined"
helperText="Destination of the amount, can be a Giver/Project sending to a Giver, a Delegate or a Project; a Delegate sending to another Delegate, or a Delegate pre-commiting it to a Project"
autoComplete="off"
fullWidth
onChange={handleChange}
onBlur={handleBlur}
value={values.idReceiver || ''}
/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Cancel
</Button>
2018-12-04 19:39:45 +00:00
<Button onClick={submitForm} color="primary" type="submit">
Transfer
2018-12-03 20:54:24 +00:00
</Button>
</DialogActions>
</Dialog>
</form>
)}
</Formik>
)
2018-12-03 20:22:51 +00:00
export default TransferDialog