2019-02-09 10:36:45 -05:00
import React from 'react'
import PropTypes from 'prop-types'
2019-02-08 21:58:37 -05:00
import { withStyles } from '@material-ui/core/styles'
import { Formik } from 'formik'
import LiquidPledging from 'Embark/contracts/LiquidPledging'
import Button from '@material-ui/core/Button'
import Typography from '@material-ui/core/Typography'
import TextField from '@material-ui/core/TextField'
import Card from '@material-ui/core/Card'
import CardActions from '@material-ui/core/CardActions'
import CardContent from '@material-ui/core/CardContent'
import Collapse from '@material-ui/core/Collapse'
import { getTokenLabel } from '../../utils/currencies'
import { toWei } from '../../utils/conversions'
import styles from './CardStyles'
2019-02-09 10:36:45 -05:00
import { useRowData } from './hooks'
2019-02-08 21:58:37 -05:00
const { transfer } = LiquidPledging . methods
function TransferCard ( { row , handleClose , classes } ) {
2019-02-09 10:36:45 -05:00
const { show , close } = useRowData ( row , handleClose )
2019-02-08 21:58:37 -05:00
return (
< Formik
initialValues = { { } }
onSubmit = { async ( values , { setSubmitting , resetForm , setStatus } ) => {
2019-03-14 18:11:16 -04:00
const { idPledge , pledge } = row
2019-02-08 21:58:37 -05:00
const { idSender , amount , idReceiver } = values
2019-03-14 18:11:16 -04:00
const args = [ idSender , idPledge , toWei ( amount . toString ( ) ) , idReceiver ]
2019-02-08 21:58:37 -05:00
const toSend = transfer ( ... args )
const estimatedGas = await toSend . estimateGas ( )
toSend
2019-02-08 22:23:54 -05:00
. send ( { gas : estimatedGas + 1000 } )
. then ( async res => {
console . log ( { res } )
const { events : { Transfer } } = res
if ( Array . isArray ( Transfer ) ) {
Transfer . forEach ( async t => {
const { to , amount } = t . returnValues
await pledge . transferTo ( to , amount )
} )
} else {
const { to , amount } = Transfer . returnValues
await pledge . transferTo ( to , amount )
}
} )
. catch ( e => {
console . log ( { e } )
} )
. finally ( ( ) => {
close ( )
resetForm ( )
} )
2019-02-08 21:58:37 -05:00
} }
>
{ ( {
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
submitForm ,
setFieldValue ,
setStatus ,
status
} ) => (
2019-02-08 22:23:54 -05:00
< Collapse in = { show } >
2019-02-08 21:58:37 -05:00
< form onSubmit = { handleSubmit } autoComplete = "off" >
< Card className = { classes . card } elevation = { 0 } >
< CardContent >
< Typography variant = "h6" component = "h2" > Transfer Funds < / Typography >
< Typography variant = "subheading" >
2019-03-14 18:11:16 -04:00
{ ` Transfer ${ values . amount || '' } ${ values . amount && row ? getTokenLabel ( row . pledge . token ) : '' } from Pledge ${ row . idPledge } ${ values . idReceiver ? 'to Giver/Delegate/Project' : '' } ${ values . idReceiver || '' } ` }
2019-02-08 21:58:37 -05:00
< / Typography >
< TextField
autoFocus
margin = "normal"
id = "amount"
name = "amount"
label = "Amount to transfer"
placeholder = "Amount to transfer"
variant = "outlined"
autoComplete = "off"
fullWidth
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . amount || '' }
/ >
< TextField
margin = "normal"
id = "idSender"
name = "idSender"
label = "Profile Id to send from"
placeholder = "Profile Id to send from"
variant = "outlined"
type = "number"
autoComplete = "off"
fullWidth
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . idSender || '' }
/ >
< 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 || '' }
/ >
< CardActions >
2019-02-09 10:36:45 -05:00
< Button size = "large" onClick = { close } >
2019-02-08 21:58:37 -05:00
Cancel
< / Button >
2019-02-14 14:02:00 -05:00
< Button size = "large" color = "primary" type = "submit" >
2019-02-08 21:58:37 -05:00
Transfer
< / Button >
< / CardActions >
< / CardContent >
< / Card >
< / form >
< / Collapse >
) }
< / Formik >
)
}
2019-02-09 10:36:45 -05:00
TransferCard . propTypes = {
classes : PropTypes . object . isRequired ,
row : PropTypes . object . isRequired ,
handleClose : PropTypes . func . isRequired ,
}
2019-02-08 21:58:37 -05:00
export default withStyles ( styles ) ( TransferCard )