diff --git a/app/actions/delegates.js b/app/actions/delegates.js index 4f8445b..7bb7ffa 100644 --- a/app/actions/delegates.js +++ b/app/actions/delegates.js @@ -1,8 +1,7 @@ import database from '../db' import { Q } from '@nozbe/watermelondb' -import { when } from 'ramda' import { getPledgesWithDelegates } from './pledges' -import { getProfilesById } from './profiles' +import { getProfilesById, getProfileById } from './profiles' const delegatesCollection = database.collections.get('delegates') export const getDelegateProfiles = async pledges => { @@ -65,6 +64,13 @@ export const getExistingDelegates = async () => { return delegates } +export const getDelegatePledgesByProfile = async profile => { + const delegates = await delegatesCollection.query( + Q.where('profile_id', profile.id) + ).fetch() + return delegates +} + export const delegateExists = async (profileId, idPledge, idx) => { const delegates = await delegatesCollection.query( Q.where('profile_id', profileId), diff --git a/app/components/projects/BackProject.jsx b/app/components/projects/BackProject.jsx index 379949a..46bc84c 100644 --- a/app/components/projects/BackProject.jsx +++ b/app/components/projects/BackProject.jsx @@ -1,10 +1,10 @@ -import React from 'react' +import React, { Fragment } from 'react' import { Formik } from 'formik' 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 } from './hooks' +import { useProjectData, useProfileData } from './hooks' import Divider from '@material-ui/core/Divider' import TextField from '@material-ui/core/TextField' import MenuItem from '@material-ui/core/MenuItem' @@ -46,65 +46,93 @@ const Title = ({ className, manifest }) => ( ) +const SubmissionSection = ({ classes, profiles, delegatePledges }) => { + return ( + + {({ + values, + errors, + touched, + handleChange, + handleBlur, + handleSubmit, + setFieldValue, + setStatus, + status + }) => { + const filteredPledges = values.delegateProfile ? delegatePledges.filter(d => d.profile.id == values.delegateProfile.id) : null + return ( +
+ + {profiles && profiles.map(profile => ( + + {profile.name} + + ))} + + {filteredPledges && + {filteredPledges.map(pledge => ( + + {`Pledge no: ${pledge.idPledge}`} + + ))} + } +
+ ) } + } +
+ )} -const SubmissionSection = ({ classes, profiles }) => ( - - {({ - values, - errors, - touched, - handleChange, - handleBlur, - handleSubmit, - setFieldValue, - setStatus, - status - }) => ( -
- - {profiles && profiles.map(profile => ( - - {profile.name} - - ))} - -
- )} -
-) - -function BackProject({classes, match, profile, projectAddedEvents, delegateAddedEvents}) { +function BackProject({classes, match, profile, delegates, projectAddedEvents, delegateAddedEvents}) { const projectId = match.params.id const { projectAge, projectAssets, manifest, delegateProfiles } = useProjectData(projectId, profile, projectAddedEvents) - console.log({delegateAddedEvents}) + const delegatePledges = useProfileData(delegateProfiles) + const delegateProfilesArr = delegates.map(d => d.profile.fetch()) + console.log({delegateAddedEvents, profile, delegates, delegateProfilesArr, delegateProfiles}, profile[0].delegates.fetch()) return (
- <SubmissionSection classes={classes} profiles={delegateProfiles} /> + <SubmissionSection classes={classes} profiles={delegateProfiles} delegatePledges={delegatePledges}/> </div> ) } +//TODO get all pledges for a delegate profile const StyledProject = withStyles(styles)(BackProject) export default withDatabase(withObservables([], ({ database, match }) => ({ profile: database.collections.get('profiles').query( Q.where('id_profile', match.params.id) ).observe(), + delegates: database.collections.get('delegates').query( + Q.on('profiles','id_profile', 3) + ).observe(), projectAddedEvents: database.collections.get('lp_events').query( Q.where('event', 'ProjectAdded') ).observe(), diff --git a/app/components/projects/hooks.js b/app/components/projects/hooks.js index 40cd0ad..3517d8e 100644 --- a/app/components/projects/hooks.js +++ b/app/components/projects/hooks.js @@ -2,13 +2,13 @@ import EmbarkJS from 'Embark/EmbarkJS' import web3 from 'Embark/web3' import LiquidPledging from 'Embark/contracts/LiquidPledging' import { useState, useEffect, useMemo, useContext } from 'react' +import { unnest } from 'ramda' import { timeSinceBlock } from '../../utils/dates' import { getFiles, ipfs } from '../../utils/ipfs' import { databaseExists } from '../../utils/db' import { FundingContext } from '../../context' import { getDelegateProfiles } from '../../actions/profiles' - -console.log({LiquidPledging}) +import { getDelegatePledgesByProfile } from '../../actions/delegates' async function getProjectAge(id, events, setState){ const event = events.find(e => e.returnValues.idProject === id) @@ -19,8 +19,6 @@ async function getProjectAge(id, events, setState){ async function getProjectAssets(projectId, setState){ EmbarkJS.onReady(async (err) => { const projectInfo = await LiquidPledging.methods.getPledgeAdmin(projectId).call() - const pledgeInfo = await LiquidPledging.methods.getPledgeDelegate(5).call() - console.log({pledgeInfo}) const CID = projectInfo.url.split('/').slice(-1)[0] console.log({CID, projectInfo, ipfs}) getFiles(CID) @@ -52,6 +50,25 @@ async function fetchAndAddDelegateProfiles(account, setState) { setState(profiles) } +async function fetchAndAddDelegatePledges(profiles, setState) { + const dPledges = [] + profiles.forEach(profile => { + const delegatePledges = getDelegatePledgesByProfile(profile) + dPledges.push(delegatePledges) + }) + const resolved = await Promise.all(dPledges) + setState(unnest(resolved)) +} + +export function useProfileData(profiles) { + const [delegatePledges, setDelegatePledges] = useState(null) + + useEffect(() => { + fetchAndAddDelegatePledges(profiles, setDelegatePledges) + }, [profiles]) + return delegatePledges +} + const getProjectManifest = assets => assets ? JSON.parse(assets.find(a => a.name.toLowerCase() === 'manifest.json').content) : null export function useProjectData(projectId, profile, projectAddedEvents) { diff --git a/app/model/profile.js b/app/model/profile.js index 0729693..abbff4e 100644 --- a/app/model/profile.js +++ b/app/model/profile.js @@ -5,7 +5,8 @@ import { LiquidModel } from '../utils/models' export default class Profile extends LiquidModel { static table = 'profiles' static associations = { - pledges: { type: 'has_many', foreignKey: 'profile_id' } + pledges: { type: 'has_many', foreignKey: 'profile_id' }, + delegates: { type: 'has_many', foreignKey: 'profile_id' } } @field('addr') addr @@ -18,6 +19,7 @@ export default class Profile extends LiquidModel { @field('id_profile') idProfile @field('block_number') blockNumber @children('pledges') pledges + @children('delegates') delegates @action async markAsCanceled() { await this.update(profile => {