2018-12-06 12:09:43 -05:00
import React , { PureComponent } from 'react'
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 indigo from '@material-ui/core/colors/indigo'
import blueGrey from '@material-ui/core/colors/blueGrey'
import Collapse from '@material-ui/core/Collapse'
2018-12-21 14:31:46 -05:00
import LiquidPledging from 'Embark/contracts/LiquidPledging'
2018-12-07 06:52:02 -05:00
import LPVault from 'Embark/contracts/LPVault'
2018-12-06 12:09:43 -05:00
import { getTokenLabel } from '../../utils/currencies'
2018-12-06 14:58:47 -05:00
import { toWei } from '../../utils/conversions'
2018-12-12 14:55:43 -05:00
import { FundingContext } from '../../context'
2018-12-06 14:58:47 -05:00
2018-12-21 14:31:46 -05:00
const { withdraw } = LiquidPledging . methods
2018-12-07 06:52:02 -05:00
const { confirmPayment } = LPVault . methods
2018-12-06 12:09:43 -05:00
const styles = {
card : {
borderRadius : '0px' ,
borderTopStyle : 'groove' ,
borderBottom : '1px solid lightgray' ,
backgroundColor : indigo [ 50 ]
} ,
bullet : {
display : 'inline-block' ,
margin : '0 2px' ,
transform : 'scale(0.8)' ,
} ,
title : {
fontSize : 14 ,
} ,
amount : {
backgroundColor : blueGrey [ 50 ]
}
}
class Withdraw extends PureComponent {
state = { show : null }
componentDidMount ( ) {
this . setState ( { show : true } )
}
close = ( ) => {
this . setState (
{ show : false } ,
( ) => setTimeout ( ( ) => { this . props . clearRowData ( ) } , 500 )
)
}
render ( ) {
const { classes , rowData } = this . props
const { show } = this . state
2018-12-07 06:52:02 -05:00
const isPaying = rowData [ 7 ] === "1"
2018-12-06 12:09:43 -05:00
return (
2018-12-12 14:55:43 -05:00
< FundingContext.Consumer >
{ ( { authorizedPayments } ) =>
2018-12-06 12:09:43 -05:00
< Formik
initialValues = { { } }
onSubmit = { async ( values , { setSubmitting , resetForm , setStatus } ) => {
2018-12-06 14:58:47 -05:00
const { amount } = values
2019-01-25 15:26:30 -05:00
const paymentId = isPaying ? authorizedPayments . find ( r => r . ref === rowData . id ) [ 'idPayment' ] : rowData . pledgeId
2018-12-12 14:55:43 -05:00
const args = isPaying ? [ paymentId ] : [ paymentId , toWei ( amount ) ]
2018-12-07 06:52:02 -05:00
const sendFn = isPaying ? confirmPayment : withdraw
2018-12-12 14:55:43 -05:00
try {
const toSend = sendFn ( ... args ) ;
const estimateGas = await toSend . estimateGas ( ) ;
toSend . send ( { gas : estimateGas + 1000 } )
. then ( res => {
console . log ( { res } )
} )
. catch ( e => {
console . log ( { e } )
} )
. finally ( ( ) => {
this . close ( )
} )
} catch ( error ) {
console . log ( error )
}
2018-12-06 12:09:43 -05:00
} }
>
{ ( {
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
setFieldValue ,
setStatus ,
status
} ) => (
< Collapse in = { show } >
< form autoComplete = "off" onSubmit = { handleSubmit } style = { { display : 'flex' , flexDirection : 'column' , marginBottom : '0px' } } >
< Card className = { classes . card } elevation = { 0 } >
< CardContent >
2018-12-07 06:52:02 -05:00
< Typography variant = "h6" component = "h2" >
2019-01-25 15:26:30 -05:00
{ ` ${ isPaying ? 'Confirm' : '' } Withdraw ${ isPaying ? 'al' : '' } ${ values . amount || '' } ${ values . amount ? getTokenLabel ( rowData [ 6 ] ) : '' } from Pledge ${ rowData . pledgeId } ` }
2018-12-06 12:09:43 -05:00
< / Typography >
2018-12-07 06:52:02 -05:00
{ ! isPaying && < TextField
2018-12-06 12:09:43 -05:00
className = { classes . amount }
id = "amount"
name = "amount"
label = "Amount"
placeholder = "Amount"
margin = "normal"
variant = "outlined"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . amount || '' }
2018-12-07 06:52:02 -05:00
/ > }
2018-12-06 12:09:43 -05:00
< / CardContent >
< CardActions >
2018-12-07 06:52:02 -05:00
< Button size = "large" onClick = { this . close } > Cancel < / Button >
< Button size = "large" color = "primary" type = "submit" > { isPaying ? 'Confirm' : 'Withdraw' } < / Button >
2018-12-06 12:09:43 -05:00
< / CardActions >
< / Card >
< / form >
< / Collapse >
) }
< / Formik >
2018-12-12 14:55:43 -05:00
}
< / FundingContext.Consumer >
2018-12-06 12:09:43 -05:00
)
}
}
Withdraw . propTypes = {
classes : PropTypes . object . isRequired ,
rowData : PropTypes . object . isRequired
}
export default withStyles ( styles ) ( Withdraw )