2019-01-23 17:40:11 +00:00
|
|
|
import React, { Fragment, Component } from 'react'
|
2018-12-03 15:29:14 +00:00
|
|
|
import MaterialTable from 'material-table'
|
2019-01-22 19:39:25 +00:00
|
|
|
import withObservables from '@nozbe/with-observables'
|
|
|
|
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
|
2018-12-03 15:29:14 +00:00
|
|
|
import { toEther } from '../utils/conversions'
|
|
|
|
import { getTokenLabel } from '../utils/currencies'
|
2018-12-03 20:22:51 +00:00
|
|
|
import TransferDialog from './TransferDialog'
|
2018-12-06 17:09:43 +00:00
|
|
|
import WithdrawCard from './table/WithdrawCard'
|
2018-12-03 15:29:14 +00:00
|
|
|
|
|
|
|
const convertToHours = seconds => seconds / 60 / 60
|
|
|
|
const projectText = project => project === '0' ? 'N/A' : project
|
2018-12-07 11:52:02 +00:00
|
|
|
const pledgeStateMap = {
|
|
|
|
0: 'Pledged',
|
|
|
|
1: 'Paying',
|
|
|
|
2: 'Paid'
|
|
|
|
}
|
2019-01-23 17:40:11 +00:00
|
|
|
const convertToDatetime = async field => {
|
2019-01-22 19:39:25 +00:00
|
|
|
const { commitTime } = field
|
2019-01-23 17:40:11 +00:00
|
|
|
const profile = await field.profile.fetch()
|
2018-12-12 20:46:02 +00:00
|
|
|
if (!profile || Number(commitTime) === 0) return 0
|
2018-12-10 15:22:52 +00:00
|
|
|
const time = Number(commitTime) + Number(profile.commitTime)
|
|
|
|
const date = new Date(time * 1000)
|
2018-12-09 12:10:17 +00:00
|
|
|
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
|
|
|
}
|
2019-01-23 17:40:11 +00:00
|
|
|
const formatField = async field => ({
|
2019-01-22 19:39:25 +00:00
|
|
|
...field.getFields(),
|
2019-01-23 17:40:11 +00:00
|
|
|
commitTime: await convertToDatetime(field),
|
2018-12-03 15:29:14 +00:00
|
|
|
amount: toEther(field.amount),
|
|
|
|
token: getTokenLabel(field.token),
|
2018-12-07 11:52:02 +00:00
|
|
|
intendedProject: projectText(field.intendedProject),
|
2019-01-23 17:40:11 +00:00
|
|
|
pledgeState: pledgeStateMap[field.pledgeState],
|
|
|
|
transferTo: field.transferTo,
|
|
|
|
pledge: field
|
2018-12-03 15:29:14 +00:00
|
|
|
})
|
2019-01-23 17:40:11 +00:00
|
|
|
class PledgesTable extends Component {
|
2018-12-03 20:22:51 +00:00
|
|
|
state = {
|
2019-01-23 17:40:11 +00:00
|
|
|
data: [],
|
2018-12-03 20:22:51 +00:00
|
|
|
row: false,
|
|
|
|
}
|
2018-12-03 15:29:14 +00:00
|
|
|
|
2019-01-23 17:40:11 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.setData()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { pledges } = this.props
|
|
|
|
const { data } = this.state
|
2019-01-25 17:18:44 +00:00
|
|
|
if (data.length) {
|
|
|
|
pledges.some((pledge, idx) => {
|
|
|
|
const current = data[idx]
|
|
|
|
if (current) {
|
|
|
|
if (toEther(pledge.amount) != current.amount || pledgeStateMap[pledge.pledgeState] != current.pledgeState) this.setData()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (pledges.length && !data.length) this.setData()
|
2019-01-23 17:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setData = async () => {
|
|
|
|
const { pledges } = this.props
|
|
|
|
const data = await Promise.all(pledges.map(formatField))
|
|
|
|
this.setState({ data })
|
|
|
|
}
|
|
|
|
|
2018-12-03 20:22:51 +00:00
|
|
|
handleClickOpen = row => {
|
2019-01-23 17:40:11 +00:00
|
|
|
this.setState({ row })
|
2018-12-03 20:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleClose = () => {
|
2019-01-23 17:40:11 +00:00
|
|
|
this.setState({ row: false })
|
2018-12-03 20:22:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:09:43 +00:00
|
|
|
clearRowData = () => this.setState({ rowData: null })
|
|
|
|
|
2018-12-03 20:22:51 +00:00
|
|
|
render() {
|
2019-01-23 17:40:11 +00:00
|
|
|
const { data, row, rowData } = this.state
|
2018-12-03 20:22:51 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2018-12-04 22:10:43 +00:00
|
|
|
<TransferDialog
|
|
|
|
row={row}
|
|
|
|
handleClose={this.handleClose}
|
|
|
|
/>
|
2018-12-03 20:22:51 +00:00
|
|
|
<MaterialTable
|
|
|
|
columns={[
|
2019-01-22 19:39:25 +00:00
|
|
|
{ title: 'Pledge Id', field: 'pledgeId', type: 'numeric' },
|
2018-12-03 20:22:51 +00:00
|
|
|
{ title: 'Owner', field: 'owner' },
|
|
|
|
{ title: 'Amount Funded', field: 'amount', type: 'numeric' },
|
|
|
|
{ title: 'Token', field: 'token' },
|
|
|
|
{ title: 'Commit Time', field: 'commitTime', type: 'numeric' },
|
|
|
|
{ title: 'Number of Delegates', field: 'nDelegates', type: 'numeric' },
|
|
|
|
{ title: 'Intended Project', field: 'intendedProject' },
|
2018-12-07 11:52:02 +00:00
|
|
|
{ title: 'Pledge State', field: 'pledgeState' },
|
2018-12-03 20:22:51 +00:00
|
|
|
]}
|
2019-01-23 17:40:11 +00:00
|
|
|
data={data}
|
2018-12-03 20:22:51 +00:00
|
|
|
title="Pledges"
|
2019-01-10 19:37:23 +00:00
|
|
|
options={{ showEmptyDataSourceMessage: true }}
|
2018-12-03 20:22:51 +00:00
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
icon: 'compare_arrows',
|
|
|
|
tooltip: 'Transfer funds',
|
|
|
|
onClick: (event, rowData) => {
|
|
|
|
this.handleClickOpen(rowData)
|
|
|
|
}
|
2018-12-06 17:09:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: 'attach_money',
|
|
|
|
tooltip: 'Request Withdrawl',
|
|
|
|
onClick: (event, rowData) => {
|
|
|
|
console.log({rowData})
|
|
|
|
this.setState({ rowData })
|
|
|
|
}
|
2018-12-03 20:22:51 +00:00
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
2018-12-07 11:52:02 +00:00
|
|
|
{rowData && <WithdrawCard rowData={rowData} clearRowData={this.clearRowData} />}
|
2018-12-03 20:22:51 +00:00
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 19:39:25 +00:00
|
|
|
export default withDatabase(withObservables([], ({ database }) => ({
|
2019-01-23 17:40:11 +00:00
|
|
|
pledges: database.collections.get('pledges').query().observeWithColumns(['amount', 'pledge_state']),
|
2019-01-22 19:39:25 +00:00
|
|
|
}))(PledgesTable))
|