2019-03-07 16:42:25 +00:00
|
|
|
import EmbarkJS from 'Embark/EmbarkJS'
|
2019-03-06 19:14:22 +00:00
|
|
|
import web3 from 'Embark/web3'
|
2019-03-07 16:42:25 +00:00
|
|
|
import LiquidPledging from 'Embark/contracts/LiquidPledging'
|
2019-03-08 21:18:57 +00:00
|
|
|
import { useState, useEffect, useMemo, useContext } from 'react'
|
2019-03-19 20:41:23 +00:00
|
|
|
import { unnest } from 'ramda'
|
2019-03-06 19:14:22 +00:00
|
|
|
import { timeSinceBlock } from '../../utils/dates'
|
2019-03-07 21:05:48 +00:00
|
|
|
import { getFiles, ipfs } from '../../utils/ipfs'
|
|
|
|
import { databaseExists } from '../../utils/db'
|
2019-03-08 21:18:57 +00:00
|
|
|
import { FundingContext } from '../../context'
|
|
|
|
import { getDelegateProfiles } from '../../actions/profiles'
|
2019-03-19 20:41:23 +00:00
|
|
|
import { getDelegatePledgesByProfile } from '../../actions/delegates'
|
2019-03-14 22:11:16 +00:00
|
|
|
|
2019-03-06 19:14:22 +00:00
|
|
|
async function getProjectAge(id, events, setState){
|
|
|
|
const event = events.find(e => e.returnValues.idProject === id)
|
|
|
|
const { timestamp } = await web3.eth.getBlock(event.blockNumber)
|
|
|
|
setState(timeSinceBlock(timestamp, 'days'))
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:42:25 +00:00
|
|
|
async function getProjectAssets(projectId, setState){
|
|
|
|
EmbarkJS.onReady(async (err) => {
|
|
|
|
const projectInfo = await LiquidPledging.methods.getPledgeAdmin(projectId).call()
|
|
|
|
const CID = projectInfo.url.split('/').slice(-1)[0]
|
2019-03-07 21:05:48 +00:00
|
|
|
console.log({CID, projectInfo, ipfs})
|
2019-03-07 16:42:25 +00:00
|
|
|
getFiles(CID)
|
|
|
|
.then((files) => {
|
|
|
|
setState(files)
|
|
|
|
const manifest = files[2]
|
|
|
|
console.log({files}, JSON.parse(manifest.content))
|
|
|
|
})
|
2019-03-07 21:05:48 +00:00
|
|
|
.catch(async (err) => {
|
|
|
|
console.log('IPFS getFiles error: ', err)
|
|
|
|
databaseExists('ipfs')
|
|
|
|
.catch(() => location.reload())
|
|
|
|
|
|
|
|
getFiles(CID)
|
|
|
|
.then((files) => {
|
|
|
|
setState(files)
|
|
|
|
const manifest = files[2]
|
|
|
|
console.log({files}, JSON.parse(manifest.content))
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log('IPFS FAILED ON READY', err)
|
|
|
|
})
|
|
|
|
})
|
2019-03-07 16:42:25 +00:00
|
|
|
})
|
2019-03-06 19:14:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 19:53:18 +00:00
|
|
|
async function getPledge(dPledge) {
|
|
|
|
const pledge = await dPledge.pledge.fetch()
|
|
|
|
dPledge.pledgeData = pledge
|
|
|
|
return dPledge
|
|
|
|
}
|
|
|
|
|
2019-03-08 21:18:57 +00:00
|
|
|
async function fetchAndAddDelegateProfiles(account, setState) {
|
|
|
|
const profiles = await getDelegateProfiles(account)
|
|
|
|
setState(profiles)
|
|
|
|
}
|
|
|
|
|
2019-03-19 20:41:23 +00:00
|
|
|
async function fetchAndAddDelegatePledges(profiles, setState) {
|
|
|
|
const dPledges = []
|
|
|
|
profiles.forEach(profile => {
|
|
|
|
const delegatePledges = getDelegatePledgesByProfile(profile)
|
|
|
|
dPledges.push(delegatePledges)
|
|
|
|
})
|
|
|
|
const resolved = await Promise.all(dPledges)
|
2019-03-20 19:53:18 +00:00
|
|
|
const unnested = unnest(resolved)
|
|
|
|
const expanded = await Promise.all(unnested.map(getPledge))
|
|
|
|
setState(expanded)
|
2019-03-19 20:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useProfileData(profiles) {
|
|
|
|
const [delegatePledges, setDelegatePledges] = useState(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchAndAddDelegatePledges(profiles, setDelegatePledges)
|
|
|
|
}, [profiles])
|
|
|
|
return delegatePledges
|
|
|
|
}
|
|
|
|
|
2019-03-06 19:14:22 +00:00
|
|
|
const getProjectManifest = assets => assets ? JSON.parse(assets.find(a => a.name.toLowerCase() === 'manifest.json').content) : null
|
|
|
|
|
|
|
|
export function useProjectData(projectId, profile, projectAddedEvents) {
|
|
|
|
const [projectAge, setAge] = useState(null)
|
|
|
|
const [projectAssets, setAssets] = useState(null)
|
2019-03-07 21:05:48 +00:00
|
|
|
const [ipfsReady, setIpfsState] = useState(null)
|
2019-03-08 21:18:57 +00:00
|
|
|
const [delegateProfiles, setDelegateProfiles] = useState(null)
|
2019-03-28 19:50:44 +00:00
|
|
|
const { account, openSnackBar } = useContext(FundingContext)
|
2019-03-07 21:05:48 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
ipfs.on('ready', () => { setIpfsState(true) })
|
|
|
|
}, [projectId])
|
2019-03-06 19:14:22 +00:00
|
|
|
|
2019-03-08 21:18:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
fetchAndAddDelegateProfiles(account, setDelegateProfiles)
|
|
|
|
}, [account])
|
|
|
|
|
2019-03-06 19:14:22 +00:00
|
|
|
useEffect(() => {
|
|
|
|
getProjectAge(projectId, projectAddedEvents, setAge)
|
|
|
|
}, [projectAddedEvents])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2019-03-07 16:42:25 +00:00
|
|
|
getProjectAssets(projectId, setAssets)
|
2019-03-07 21:05:48 +00:00
|
|
|
}, [projectId, ipfsReady])
|
2019-03-06 19:14:22 +00:00
|
|
|
|
|
|
|
const manifest = useMemo(() => getProjectManifest(projectAssets), [projectAssets])
|
|
|
|
|
2019-03-28 19:50:44 +00:00
|
|
|
return {
|
|
|
|
projectAge,
|
|
|
|
projectAssets,
|
|
|
|
manifest,
|
|
|
|
delegateProfiles,
|
|
|
|
openSnackBar
|
|
|
|
}
|
2019-03-06 19:14:22 +00:00
|
|
|
}
|