mirror of
https://github.com/status-im/liquid-funding.git
synced 2025-02-16 21:36:46 +00:00
add delegate pledges to back project page
This commit is contained in:
parent
f091a72f61
commit
0d874ae881
@ -1,8 +1,7 @@
|
|||||||
import database from '../db'
|
import database from '../db'
|
||||||
import { Q } from '@nozbe/watermelondb'
|
import { Q } from '@nozbe/watermelondb'
|
||||||
import { when } from 'ramda'
|
|
||||||
import { getPledgesWithDelegates } from './pledges'
|
import { getPledgesWithDelegates } from './pledges'
|
||||||
import { getProfilesById } from './profiles'
|
import { getProfilesById, getProfileById } from './profiles'
|
||||||
|
|
||||||
const delegatesCollection = database.collections.get('delegates')
|
const delegatesCollection = database.collections.get('delegates')
|
||||||
export const getDelegateProfiles = async pledges => {
|
export const getDelegateProfiles = async pledges => {
|
||||||
@ -65,6 +64,13 @@ export const getExistingDelegates = async () => {
|
|||||||
return delegates
|
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) => {
|
export const delegateExists = async (profileId, idPledge, idx) => {
|
||||||
const delegates = await delegatesCollection.query(
|
const delegates = await delegatesCollection.query(
|
||||||
Q.where('profile_id', profileId),
|
Q.where('profile_id', profileId),
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import React from 'react'
|
import React, { Fragment } from 'react'
|
||||||
import { Formik } from 'formik'
|
import { Formik } from 'formik'
|
||||||
import withObservables from '@nozbe/with-observables'
|
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 } from './hooks'
|
import { useProjectData, useProfileData } from './hooks'
|
||||||
import Divider from '@material-ui/core/Divider'
|
import Divider from '@material-ui/core/Divider'
|
||||||
import TextField from '@material-ui/core/TextField'
|
import TextField from '@material-ui/core/TextField'
|
||||||
import MenuItem from '@material-ui/core/MenuItem'
|
import MenuItem from '@material-ui/core/MenuItem'
|
||||||
@ -46,10 +46,10 @@ const Title = ({ className, manifest }) => (
|
|||||||
<Divider />
|
<Divider />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
const SubmissionSection = ({ classes, profiles, delegatePledges }) => {
|
||||||
const SubmissionSection = ({ classes, profiles }) => (
|
return (
|
||||||
<Formik
|
<Formik
|
||||||
initialValues={{ delegateProfile: ''}}
|
initialValues={{ delegateProfile: '', delegatePledge: '' }}
|
||||||
onSubmit={console.log}
|
onSubmit={console.log}
|
||||||
>
|
>
|
||||||
{({
|
{({
|
||||||
@ -62,7 +62,9 @@ const SubmissionSection = ({ classes, profiles }) => (
|
|||||||
setFieldValue,
|
setFieldValue,
|
||||||
setStatus,
|
setStatus,
|
||||||
status
|
status
|
||||||
}) => (
|
}) => {
|
||||||
|
const filteredPledges = values.delegateProfile ? delegatePledges.filter(d => d.profile.id == values.delegateProfile.id) : null
|
||||||
|
return (
|
||||||
<form onSubmit={handleSubmit} className={classes.submissionRoot}>
|
<form onSubmit={handleSubmit} className={classes.submissionRoot}>
|
||||||
<TextField
|
<TextField
|
||||||
className={classes.textField}
|
className={classes.textField}
|
||||||
@ -78,33 +80,59 @@ const SubmissionSection = ({ classes, profiles }) => (
|
|||||||
value={values.delegateProfile || ''}
|
value={values.delegateProfile || ''}
|
||||||
>
|
>
|
||||||
{profiles && profiles.map(profile => (
|
{profiles && profiles.map(profile => (
|
||||||
<MenuItem style={{ display: 'flex', alignItems: 'center' }} key={profile.name} value={profile.name}>
|
<MenuItem style={{ display: 'flex', alignItems: 'center' }} key={profile.name} value={profile}>
|
||||||
{profile.name}
|
{profile.name}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
))}
|
))}
|
||||||
</TextField>
|
</TextField>
|
||||||
|
{filteredPledges && <TextField
|
||||||
|
className={classes.textField}
|
||||||
|
id="delegatePledge"
|
||||||
|
name="delegatePledge"
|
||||||
|
select
|
||||||
|
label="Select Pledge for Funding"
|
||||||
|
placeholder="Select Pledge for Funding"
|
||||||
|
margin="normal"
|
||||||
|
variant="outlined"
|
||||||
|
onChange={handleChange}
|
||||||
|
onBlur={handleBlur}
|
||||||
|
value={values.delegatePledge || ''}
|
||||||
|
>
|
||||||
|
{filteredPledges.map(pledge => (
|
||||||
|
<MenuItem style={{ display: 'flex', alignItems: 'center' }} key={pledge.idPledge} value={pledge.idPledge}>
|
||||||
|
{`Pledge no: ${pledge.idPledge}`}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</TextField>}
|
||||||
</form>
|
</form>
|
||||||
)}
|
) }
|
||||||
|
}
|
||||||
</Formik>
|
</Formik>
|
||||||
)
|
)}
|
||||||
|
|
||||||
function BackProject({classes, match, profile, projectAddedEvents, delegateAddedEvents}) {
|
function BackProject({classes, match, profile, delegates, projectAddedEvents, delegateAddedEvents}) {
|
||||||
const projectId = match.params.id
|
const projectId = match.params.id
|
||||||
const { projectAge, projectAssets, manifest, delegateProfiles } = useProjectData(projectId, profile, projectAddedEvents)
|
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 (
|
return (
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<Title className={classes.title} manifest={manifest} />
|
<Title className={classes.title} manifest={manifest} />
|
||||||
<SubmissionSection classes={classes} profiles={delegateProfiles} />
|
<SubmissionSection classes={classes} profiles={delegateProfiles} delegatePledges={delegatePledges}/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO get all pledges for a delegate profile
|
||||||
const StyledProject = withStyles(styles)(BackProject)
|
const StyledProject = withStyles(styles)(BackProject)
|
||||||
export default withDatabase(withObservables([], ({ database, match }) => ({
|
export default withDatabase(withObservables([], ({ database, match }) => ({
|
||||||
profile: database.collections.get('profiles').query(
|
profile: database.collections.get('profiles').query(
|
||||||
Q.where('id_profile', match.params.id)
|
Q.where('id_profile', match.params.id)
|
||||||
).observe(),
|
).observe(),
|
||||||
|
delegates: database.collections.get('delegates').query(
|
||||||
|
Q.on('profiles','id_profile', 3)
|
||||||
|
).observe(),
|
||||||
projectAddedEvents: database.collections.get('lp_events').query(
|
projectAddedEvents: database.collections.get('lp_events').query(
|
||||||
Q.where('event', 'ProjectAdded')
|
Q.where('event', 'ProjectAdded')
|
||||||
).observe(),
|
).observe(),
|
||||||
|
@ -2,13 +2,13 @@ import EmbarkJS from 'Embark/EmbarkJS'
|
|||||||
import web3 from 'Embark/web3'
|
import web3 from 'Embark/web3'
|
||||||
import LiquidPledging from 'Embark/contracts/LiquidPledging'
|
import LiquidPledging from 'Embark/contracts/LiquidPledging'
|
||||||
import { useState, useEffect, useMemo, useContext } from 'react'
|
import { useState, useEffect, useMemo, useContext } from 'react'
|
||||||
|
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 { FundingContext } from '../../context'
|
import { FundingContext } from '../../context'
|
||||||
import { getDelegateProfiles } from '../../actions/profiles'
|
import { getDelegateProfiles } from '../../actions/profiles'
|
||||||
|
import { getDelegatePledgesByProfile } from '../../actions/delegates'
|
||||||
console.log({LiquidPledging})
|
|
||||||
|
|
||||||
async function getProjectAge(id, events, setState){
|
async function getProjectAge(id, events, setState){
|
||||||
const event = events.find(e => e.returnValues.idProject === id)
|
const event = events.find(e => e.returnValues.idProject === id)
|
||||||
@ -19,8 +19,6 @@ async function getProjectAge(id, events, setState){
|
|||||||
async function getProjectAssets(projectId, setState){
|
async function getProjectAssets(projectId, setState){
|
||||||
EmbarkJS.onReady(async (err) => {
|
EmbarkJS.onReady(async (err) => {
|
||||||
const projectInfo = await LiquidPledging.methods.getPledgeAdmin(projectId).call()
|
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]
|
const CID = projectInfo.url.split('/').slice(-1)[0]
|
||||||
console.log({CID, projectInfo, ipfs})
|
console.log({CID, projectInfo, ipfs})
|
||||||
getFiles(CID)
|
getFiles(CID)
|
||||||
@ -52,6 +50,25 @@ async function fetchAndAddDelegateProfiles(account, setState) {
|
|||||||
setState(profiles)
|
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
|
const getProjectManifest = assets => assets ? JSON.parse(assets.find(a => a.name.toLowerCase() === 'manifest.json').content) : null
|
||||||
|
|
||||||
export function useProjectData(projectId, profile, projectAddedEvents) {
|
export function useProjectData(projectId, profile, projectAddedEvents) {
|
||||||
|
@ -5,7 +5,8 @@ import { LiquidModel } from '../utils/models'
|
|||||||
export default class Profile extends LiquidModel {
|
export default class Profile extends LiquidModel {
|
||||||
static table = 'profiles'
|
static table = 'profiles'
|
||||||
static associations = {
|
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
|
@field('addr') addr
|
||||||
@ -18,6 +19,7 @@ export default class Profile extends LiquidModel {
|
|||||||
@field('id_profile') idProfile
|
@field('id_profile') idProfile
|
||||||
@field('block_number') blockNumber
|
@field('block_number') blockNumber
|
||||||
@children('pledges') pledges
|
@children('pledges') pledges
|
||||||
|
@children('delegates') delegates
|
||||||
|
|
||||||
@action async markAsCanceled() {
|
@action async markAsCanceled() {
|
||||||
await this.update(profile => {
|
await this.update(profile => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user