From ff2a9be9c2cc03596ed5843990d6c7b1405a2727 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Wed, 22 May 2019 09:38:04 -0400 Subject: [PATCH] add enrich pledges --- src/components/projects/ProjectPledges.jsx | 10 +++++++--- src/components/projects/hooks.js | 20 ++++++++++++++++++++ src/utils/array.js | 4 ++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/utils/array.js diff --git a/src/components/projects/ProjectPledges.jsx b/src/components/projects/ProjectPledges.jsx index 8775f5d..1742990 100644 --- a/src/components/projects/ProjectPledges.jsx +++ b/src/components/projects/ProjectPledges.jsx @@ -7,7 +7,7 @@ import withObservables from '@nozbe/with-observables' import { Q } from '@nozbe/watermelondb' import { withDatabase } from '@nozbe/watermelondb/DatabaseProvider' 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 { toEther, /*toWei*/ } from '../../utils/conversions' 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 { manifest, delegateProfiles, openSnackBar } = useProjectData(projectId, projectAddedEvents) const delegatePledges = useProfileData(delegateProfiles) - console.log('pledges', {pledges}) + const enrichedPledges = usePledgesAuthorizations(pledges, authorizedPayments) + console.log('pledges', {pledges, authorizedPayments, enrichedPledges}) return (
@@ -232,6 +233,9 @@ export default withDatabase(withObservables([], ({ database, match }) => ({ Q.where('intended_project', match.params.id), Q.where('owner_id', match.params.id) ) + ).observe(), + authorizedPayments: database.collections.get('vault_events').query( + Q.where('event', 'AuthorizePayment') ).observe() }))(StyledPledges)) diff --git a/src/components/projects/hooks.js b/src/components/projects/hooks.js index 7e0d2d9..8d88755 100644 --- a/src/components/projects/hooks.js +++ b/src/components/projects/hooks.js @@ -6,6 +6,7 @@ import { unnest } from 'ramda' import { timeSinceBlock } from '../../utils/dates' import { getFiles, ipfs } from '../../utils/ipfs' import { databaseExists } from '../../utils/db' +import { arrayToObject } from '../../utils/array' import { FundingContext } from '../../context' import { getDelegateProfiles } from '../../actions/profiles' import { getDelegatePledgesByProfile } from '../../actions/delegates' @@ -132,3 +133,22 @@ export function useProjectData(projectId, projectAddedEvents) { 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 +} diff --git a/src/utils/array.js b/src/utils/array.js new file mode 100644 index 0000000..62fec0f --- /dev/null +++ b/src/utils/array.js @@ -0,0 +1,4 @@ +export const arrayToObject = (arr, keyField) => Object.assign( + {}, + ...arr.map(item => ({[item[keyField]]: item})) +)