add enrich pledges

This commit is contained in:
Barry Gitarts 2019-05-22 09:38:04 -04:00 committed by Barry G
parent cbf4c367cd
commit ff2a9be9c2
3 changed files with 31 additions and 3 deletions

View File

@ -7,7 +7,7 @@ import withObservables from '@nozbe/with-observables'
import { Q } from '@nozbe/watermelondb' import { Q } from '@nozbe/watermelondb'
import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import { useProjectData, useProfileData } from './hooks' import { useProjectData, useProfileData, usePledgesAuthorizations } from './hooks'
import { Button, Divider, Typography, Card, CardActions, CardContent, FormControlLabel, Switch } from '@material-ui/core' import { Button, Divider, Typography, Card, CardActions, CardContent, FormControlLabel, Switch } from '@material-ui/core'
import { toEther, /*toWei*/ } from '../../utils/conversions' import { toEther, /*toWei*/ } from '../../utils/conversions'
import { getTokenLabel } from '../../utils/currencies' import { getTokenLabel } from '../../utils/currencies'
@ -196,11 +196,12 @@ const SubmissionSection = ({ classes, projectId, openSnackBar, pledges }) => {
) )
} }
function ProjectPledges({classes, match, delegates: _delegates, projectAddedEvents, delegateAddedEvents: _delegateAddedEvents, pledges}) { function ProjectPledges({classes, match, delegates: _delegates, projectAddedEvents, delegateAddedEvents: _delegateAddedEvents, pledges, authorizedPayments}) {
const projectId = match.params.id const projectId = match.params.id
const { manifest, delegateProfiles, openSnackBar } = useProjectData(projectId, projectAddedEvents) const { manifest, delegateProfiles, openSnackBar } = useProjectData(projectId, projectAddedEvents)
const delegatePledges = useProfileData(delegateProfiles) const delegatePledges = useProfileData(delegateProfiles)
console.log('pledges', {pledges}) const enrichedPledges = usePledgesAuthorizations(pledges, authorizedPayments)
console.log('pledges', {pledges, authorizedPayments, enrichedPledges})
return ( return (
<div className={classes.root}> <div className={classes.root}>
<Title className={classes.title} manifest={manifest} /> <Title className={classes.title} manifest={manifest} />
@ -232,6 +233,9 @@ export default withDatabase(withObservables([], ({ database, match }) => ({
Q.where('intended_project', match.params.id), Q.where('intended_project', match.params.id),
Q.where('owner_id', match.params.id) Q.where('owner_id', match.params.id)
) )
).observe(),
authorizedPayments: database.collections.get('vault_events').query(
Q.where('event', 'AuthorizePayment')
).observe() ).observe()
}))(StyledPledges)) }))(StyledPledges))

View File

@ -6,6 +6,7 @@ import { unnest } from 'ramda'
import { timeSinceBlock } from '../../utils/dates' import { timeSinceBlock } from '../../utils/dates'
import { getFiles, ipfs } from '../../utils/ipfs' import { getFiles, ipfs } from '../../utils/ipfs'
import { databaseExists } from '../../utils/db' import { databaseExists } from '../../utils/db'
import { arrayToObject } from '../../utils/array'
import { FundingContext } from '../../context' import { FundingContext } from '../../context'
import { getDelegateProfiles } from '../../actions/profiles' import { getDelegateProfiles } from '../../actions/profiles'
import { getDelegatePledgesByProfile } from '../../actions/delegates' import { getDelegatePledgesByProfile } from '../../actions/delegates'
@ -132,3 +133,22 @@ export function useProjectData(projectId, projectAddedEvents) {
openSnackBar openSnackBar
} }
} }
function mergePledgesAuthorizations(pledges, authorizations, setState) {
const auths = arrayToObject(authorizations, 'ref')
const enriched = pledges.map(pledge => {
const { idPledge } = pledge
if (auths[idPledge]) pledge.authorization = auths[idPledge]
return pledge
})
setState(enriched)
}
export function usePledgesAuthorizations(pledges, authorizations) {
const [enrichedPledges, setEnrichedPledges] = useState(pledges)
useEffect(() => {
mergePledgesAuthorizations(pledges, authorizations, setEnrichedPledges)
}, [pledges, authorizations])
return enrichedPledges
}

4
src/utils/array.js Normal file
View File

@ -0,0 +1,4 @@
export const arrayToObject = (arr, keyField) => Object.assign(
{},
...arr.map(item => ({[item[keyField]]: item}))
)