2019-02-01 21:22:27 +00:00
import React , { createRef } from 'react'
2018-12-02 17:25:49 +00:00
import { Formik } from 'formik'
2018-12-21 19:31:46 +00:00
import LiquidPledging from 'Embark/contracts/LiquidPledging'
2018-12-02 17:25:49 +00:00
import Button from '@material-ui/core/Button'
import MenuItem from '@material-ui/core/MenuItem'
import TextField from '@material-ui/core/TextField'
import Snackbar from '@material-ui/core/Snackbar'
2019-02-01 21:22:27 +00:00
import InputAdornment from '@material-ui/core/InputAdornment'
import CloudUpload from '@material-ui/icons/CloudUpload'
2018-12-02 17:25:49 +00:00
import web3 from 'Embark/web3'
2018-12-02 14:46:20 +00:00
import { MySnackbarContentWrapper } from './base/SnackBars'
2019-02-02 19:56:08 +00:00
import { captureFile } from '../utils/ipfs'
import ImageViewer from './image/ImageViewer'
2018-11-29 20:52:24 +00:00
2018-12-21 19:31:46 +00:00
const { addGiver , addDelegate , addProject } = LiquidPledging . methods
2018-12-02 17:25:49 +00:00
const FUNDER = 'FUNDER'
const DELEGATE = 'DELEGATE'
2018-12-05 17:05:21 +00:00
const PROJECT = 'PROJECT'
2018-12-02 17:25:49 +00:00
const helperText = {
[ FUNDER ] : 'The length of time in hours the Funder has to veto when the delegates pledge funds to a project' ,
2018-12-05 17:05:21 +00:00
[ DELEGATE ] : 'The length of time in hours the Delegate can be vetoed. Whenever this delegate is in a delegate chain the time allowed to veto any event must be greater than or equal to this time' ,
[ PROJECT ] : 'The length of time the Project has to veto when the project delegates to another delegate and they pledge those funds to a project'
2018-12-02 17:25:49 +00:00
}
2018-12-05 17:05:21 +00:00
const funderNameLabel = {
[ FUNDER ] : 'Funding' ,
[ DELEGATE ] : 'Delegate' ,
[ PROJECT ] : 'Project'
}
const buttonLabel = {
[ FUNDER ] : 'FUNDING' ,
[ DELEGATE ] : 'DELEGATE' ,
[ PROJECT ] : 'PROJECT'
}
const sendFns = {
[ FUNDER ] : addGiver ,
[ DELEGATE ] : addDelegate ,
[ PROJECT ] : addProject
}
const adminProfiles = [ FUNDER , DELEGATE , PROJECT ]
2018-11-30 16:52:08 +00:00
const hoursToSeconds = hours => hours * 60 * 60
2018-11-30 15:33:48 +00:00
const addFunderSucessMsg = response => {
2018-11-30 16:52:08 +00:00
const { events : { GiverAdded : { returnValues : { idGiver } } } } = response
return ` Funder created with ID of ${ idGiver } `
2018-11-30 15:33:48 +00:00
}
2018-12-02 17:25:49 +00:00
const addDelegateSucessMsg = response => {
const { events : { DelegateAdded : { returnValues : { idDelegate } } } } = response
return ` Delegate created with ID of ${ idDelegate } `
}
2018-12-05 17:05:21 +00:00
const addProjectSucessMsg = response => {
2018-12-05 18:36:44 +00:00
const { events : { ProjectAdded : { returnValues : { idProject } } } } = response
2018-12-05 17:05:21 +00:00
return ` Project created with ID of ${ idProject } `
}
const successMsg = {
[ FUNDER ] : addFunderSucessMsg ,
[ DELEGATE ] : addDelegateSucessMsg ,
[ PROJECT ] : addProjectSucessMsg
}
2018-11-29 20:52:24 +00:00
2019-02-01 21:22:27 +00:00
let uploadInput = createRef ( )
2018-12-02 14:46:20 +00:00
const AddFunder = ( { appendFundProfile } ) => (
2018-11-29 20:52:24 +00:00
< Formik
2018-12-05 17:05:21 +00:00
initialValues = { { adminType : FUNDER , funderName : '' , funderDescription : '' , commitTime : '' } }
2018-11-30 15:33:48 +00:00
onSubmit = { async ( values , { setSubmitting , resetForm , setStatus } ) => {
2018-12-02 17:25:49 +00:00
const { adminType , funderName , funderDescription , commitTime } = values
2018-11-30 16:52:08 +00:00
const account = await web3 . eth . getCoinbase ( )
2018-12-05 17:05:21 +00:00
//TODO add field for parent project
const args = adminType === PROJECT
? [ funderName , funderDescription , account , 0 , hoursToSeconds ( commitTime ) , 0 ]
: [ funderName , funderDescription , hoursToSeconds ( commitTime ) , 0 ]
2018-12-02 17:25:49 +00:00
const isFunder = adminType === FUNDER
2018-12-05 17:05:21 +00:00
const sendFn = sendFns [ adminType ]
2018-12-02 17:25:49 +00:00
sendFn ( ... args )
2018-11-30 15:33:48 +00:00
. estimateGas ( { from : account } )
. then ( async gas => {
2018-12-02 17:25:49 +00:00
sendFn ( ... args )
2018-11-30 15:33:48 +00:00
. send ( { from : account , gas : gas + 100 } )
. then ( res => {
2018-12-05 17:05:21 +00:00
console . log ( { res } )
2018-12-02 17:25:49 +00:00
if ( isFunder ) appendFundProfile ( res . events . GiverAdded )
2018-11-30 15:33:48 +00:00
setStatus ( {
2018-12-02 17:25:49 +00:00
snackbar : {
variant : 'success' ,
2018-12-05 17:05:21 +00:00
message : successMsg [ adminType ] ( res )
2018-12-02 17:25:49 +00:00
}
2018-11-30 15:33:48 +00:00
} )
} )
. catch ( e => {
console . log ( { e } )
setStatus ( {
snackbar : { variant : 'error' , message : 'There was an error' }
} )
} )
} )
2018-11-29 20:52:24 +00:00
} }
>
2018-11-30 15:33:48 +00:00
{ ( {
2019-02-01 21:22:27 +00:00
values ,
errors ,
touched ,
handleChange ,
handleBlur ,
handleSubmit ,
setFieldValue ,
setStatus ,
status
2018-11-30 15:33:48 +00:00
} ) => (
< form onSubmit = { handleSubmit } style = { { display : 'flex' , flexDirection : 'column' } } >
2018-12-02 17:25:49 +00:00
< TextField
id = "adminType"
name = "adminType"
select
label = "Select admin type"
placeholder = "Select admin type"
margin = "normal"
variant = "outlined"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . adminType || '' }
>
{ adminProfiles . map ( profile => (
< MenuItem style = { { display : 'flex' , alignItems : 'center' } } key = { profile } value = { profile } >
{ profile }
< / MenuItem >
) ) }
< / TextField >
2018-11-30 15:33:48 +00:00
< TextField
id = "funderName"
name = "funderName"
2018-12-05 17:05:21 +00:00
label = { ` ${ funderNameLabel [ values . adminType ] } Name ` }
placeholder = { ` ${ funderNameLabel [ values . adminType ] } Name ` }
2018-11-30 15:33:48 +00:00
margin = "normal"
variant = "outlined"
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . funderName || '' }
/ >
2019-02-01 21:22:27 +00:00
< input
ref = { ( input ) => { uploadInput = input } }
type = "file"
2019-02-13 16:28:26 +00:00
multiple
2019-02-01 22:35:14 +00:00
onChange = {
( e ) => captureFile (
e ,
hash => setFieldValue ( 'funderDescription' , hash ) ,
profileImg => setStatus ( { profileImg } )
)
}
2019-02-01 21:22:27 +00:00
style = { { display : 'none' } } / >
2018-11-30 15:33:48 +00:00
< TextField
2018-11-30 16:38:58 +00:00
id = "funderDescription"
name = "funderDescription"
2019-02-01 21:22:27 +00:00
InputProps = { {
startAdornment : (
< InputAdornment position = "start" >
< CloudUpload style = { { cursor : 'pointer' } } onClick = { ( ) => uploadInput . click ( ) } / >
< / InputAdornment >
) ,
} }
label = { < span > Description ( URL or IPFS Hash ) < / span > }
2018-12-02 13:24:31 +00:00
placeholder = "Description (URL or IPFS Hash)"
2018-11-30 15:33:48 +00:00
margin = "normal"
variant = "outlined"
onChange = { handleChange }
onBlur = { handleBlur }
2018-11-30 16:38:58 +00:00
value = { values . funderDescription || '' }
2018-11-30 15:33:48 +00:00
/ >
2019-02-02 19:56:08 +00:00
{ /* {status && status.profileImg && <img src={status.profileImg.img} alt='ipfs' style={{maxWidth: '90%'}} />} */ }
{ status && < ImageViewer status = { status } / > }
2018-11-30 15:33:48 +00:00
< TextField
id = "commitTime"
name = "commitTime"
label = "Commit time in hours"
placeholder = "Commit time in hours"
margin = "normal"
variant = "outlined"
2018-12-02 17:25:49 +00:00
helperText = { helperText [ values . adminType ] }
2018-11-30 15:33:48 +00:00
onChange = { handleChange }
onBlur = { handleBlur }
value = { values . commitTime || '' }
/ >
< Button variant = "contained" color = "primary" type = "submit" >
2018-12-05 17:05:21 +00:00
{ ` ADD ${ buttonLabel [ values . adminType ] } PROFILE ` }
2018-11-30 15:33:48 +00:00
< / Button >
2019-02-01 22:35:14 +00:00
{ status && status . snackbar && < Snackbar
2019-02-13 16:28:26 +00:00
anchorOrigin = { {
vertical : 'bottom' ,
horizontal : 'left' ,
} }
open = { ! ! status . snackbar }
autoHideDuration = { 6000 }
onClose = { ( ) => setStatus ( null ) }
>
2018-11-30 15:33:48 +00:00
< MySnackbarContentWrapper
onClose = { ( ) => setStatus ( null ) }
variant = { status . snackbar . variant }
message = { status . snackbar . message }
/ >
< / Snackbar > }
< / form >
) }
< / Formik >
2018-11-29 20:52:24 +00:00
)
2018-11-30 16:52:08 +00:00
export default AddFunder