From c9ec430449a8c4367b2d5366b29fd7abc64d0946 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Wed, 23 Jan 2019 12:40:11 -0500 Subject: [PATCH] use pledge model for transfers in pledges --- app/actions/pledges.js | 3 +- app/components/FundsManagement.jsx | 4 +-- app/components/PledgesTable.jsx | 48 ++++++++++++++++++-------- app/components/TransferDialog.jsx | 54 +++++++++++++++++------------- app/model/pledge.js | 15 +++++++++ 5 files changed, 84 insertions(+), 40 deletions(-) diff --git a/app/actions/pledges.js b/app/actions/pledges.js index 6aeb91a..75f50c3 100644 --- a/app/actions/pledges.js +++ b/app/actions/pledges.js @@ -14,7 +14,8 @@ const createPledge = (pledge, data, profiles) => { pledge.nDelegates = Number(nDelegates) pledge.pledgeState = pledgeState pledge.intendedProject = Number(intendedProject) - pledge.profile.id = profile.id + //pledge.profile.id = profile.id + pledge.profile.set(profile) } const pledgesCollection = database.collections.get('pledges') diff --git a/app/components/FundsManagement.jsx b/app/components/FundsManagement.jsx index bd4a44c..5b0ad10 100644 --- a/app/components/FundsManagement.jsx +++ b/app/components/FundsManagement.jsx @@ -12,9 +12,9 @@ const FundsManagement = ({ open }) => { const WebkitTransition = 'all 0.25s ease-out 0s' return ( - {({ allPledges, appendPledges, appendFundProfile, transferPledgeAmounts, fundProfiles }) => + {({ appendPledges, appendFundProfile }) =>
- + diff --git a/app/components/PledgesTable.jsx b/app/components/PledgesTable.jsx index 97a2d64..bf9a02b 100644 --- a/app/components/PledgesTable.jsx +++ b/app/components/PledgesTable.jsx @@ -1,4 +1,4 @@ -import React, { Fragment, PureComponent } from 'react' +import React, { Fragment, Component } from 'react' import MaterialTable from 'material-table' import withObservables from '@nozbe/with-observables' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' @@ -14,46 +14,66 @@ const pledgeStateMap = { 1: 'Paying', 2: 'Paid' } -const convertToDatetime = field => { +const convertToDatetime = async field => { const { commitTime } = field - const profile = field.profile + const profile = await field.profile.fetch() if (!profile || Number(commitTime) === 0) return 0 const time = Number(commitTime) + Number(profile.commitTime) const date = new Date(time * 1000) return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}` } -const formatField = field => ({ +const formatField = async field => ({ ...field.getFields(), - commitTime: convertToDatetime(field), + commitTime: await convertToDatetime(field), amount: toEther(field.amount), token: getTokenLabel(field.token), intendedProject: projectText(field.intendedProject), - pledgeState: pledgeStateMap[field.pledgeState] + pledgeState: pledgeStateMap[field.pledgeState], + transferTo: field.transferTo, + pledge: field }) -class PledgesTable extends PureComponent { +class PledgesTable extends Component { state = { + data: [], row: false, } + componentDidMount() { + this.setData() + } + + componentDidUpdate() { + const { pledges } = this.props + const { data } = this.state + pledges.some((pledge, idx) => { + const current = data[idx] + if (toEther(pledge.amount) != current.amount || pledgeStateMap[pledge.pledgeState] != current.pledgeState) this.setData() + }) + } + + setData = async () => { + const { pledges } = this.props + const data = await Promise.all(pledges.map(formatField)) + this.setState({ data }) + } + handleClickOpen = row => { - this.setState({ row }); + this.setState({ row }) } handleClose = () => { - this.setState({ row: false }); + this.setState({ row: false }) } clearRowData = () => this.setState({ rowData: null }) render() { - const { pledges, transferPledgeAmounts } = this.props - const { row, rowData } = this.state + const { data, row, rowData } = this.state return ( ({ - pledges: database.collections.get('pledges').query().observeWithColumns(['pledge_state']), + pledges: database.collections.get('pledges').query().observeWithColumns(['amount', 'pledge_state']), }))(PledgesTable)) diff --git a/app/components/TransferDialog.jsx b/app/components/TransferDialog.jsx index d0e6210..dd428a1 100644 --- a/app/components/TransferDialog.jsx +++ b/app/components/TransferDialog.jsx @@ -10,34 +10,42 @@ import DialogContentText from '@material-ui/core/DialogContentText' import DialogTitle from '@material-ui/core/DialogTitle' import { getTokenLabel } from '../utils/currencies' import { toWei } from '../utils/conversions' -import { FundingContext } from '../context' const { transfer } = LiquidPledging.methods -const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => ( +const TransferDialog = ({ row, handleClose }) => ( { - const { id } = row - const { idSender, amount, idReceiver } = values - const args = [idSender, id, toWei(amount.toString()), idReceiver] - const toSend = transfer(...args); - const estimatedGas = await toSend.estimateGas(); + onSubmit={async (values, { setSubmitting, resetForm, setStatus }) => { + const { pledgeId, pledge } = row + const { idSender, amount, idReceiver } = values + const args = [idSender, pledgeId, toWei(amount.toString()), idReceiver] + const toSend = transfer(...args) + const estimatedGas = await toSend.estimateGas() - toSend.send({gas: estimatedGas + 1000}) - .then(res => { - console.log({res}) - const { events: { Transfer: { returnValues } } } = res - transferPledgeAmounts(returnValues) - }) - .catch(e => { - console.log({e}) - }) - .finally(() => { - handleClose() - resetForm() - }) - }} + toSend + .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(() => { + handleClose() + resetForm() + }) + }} > {({ values, @@ -60,7 +68,7 @@ const TransferDialog = ({ row, handleClose, transferPledgeAmounts }) => ( Transfer Funds - {`Transfer ${values.amount || ''} ${values.amount ? getTokenLabel(row[6]) : ''} from Pledge ${row.id} ${values.idReceiver ? 'to Giver/Delegate/Project' : ''} ${values.idReceiver || ''}`} + {`Transfer ${values.amount || ''} ${values.amount ? getTokenLabel(row[6]) : ''} from Pledge ${row.pledgeId} ${values.idReceiver ? 'to Giver/Delegate/Project' : ''} ${values.idReceiver || ''}`} { + pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString() + }), + toPledge.prepareUpdate(pledge => { + pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString() + }) + ) + } }