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-06 19:14:22 +00:00
|
|
|
import { useState, useEffect, useMemo } from 'react'
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
ipfs.on('ready', () => { setIpfsState(true) })
|
|
|
|
}, [projectId])
|
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])
|
|
|
|
|
|
|
|
return { projectAge, projectAssets, manifest }
|
|
|
|
}
|