From ad4e4b3799b6b6f12140832d6d4777d13768740c Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Wed, 13 Feb 2019 11:28:26 -0500 Subject: [PATCH] getProjectAssets from ipfs --- app/components/AddFunder.jsx | 17 +++++++++-------- app/components/projects/Project.jsx | 20 +++++++++++++++++++- app/utils/ipfs.js | 25 ++++++++++++++++++------- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/app/components/AddFunder.jsx b/app/components/AddFunder.jsx index 8129ad4..db0ad01 100644 --- a/app/components/AddFunder.jsx +++ b/app/components/AddFunder.jsx @@ -137,6 +137,7 @@ const AddFunder = ({ appendFundProfile }) => ( { uploadInput = input }} type="file" + multiple onChange={ (e) => captureFile( e, @@ -181,14 +182,14 @@ const AddFunder = ({ appendFundProfile }) => ( {`ADD ${buttonLabel[values.adminType]} PROFILE`} {status && status.snackbar && setStatus(null)} - > + anchorOrigin={{ + vertical: 'bottom', + horizontal: 'left', + }} + open={!!status.snackbar} + autoHideDuration={6000} + onClose={() => setStatus(null)} + > setStatus(null)} variant={status.snackbar.variant} diff --git a/app/components/projects/Project.jsx b/app/components/projects/Project.jsx index ff61d51..e90bbbe 100644 --- a/app/components/projects/Project.jsx +++ b/app/components/projects/Project.jsx @@ -19,6 +19,7 @@ import PropTypes from 'prop-types' import { toEther } from '../../utils/conversions' import { getTokenLabel } from '../../utils/currencies' import { timeSinceBlock } from '../../utils/dates' +import { getFiles } from '../../utils/ipfs' const styles = theme => ({ root: { @@ -129,17 +130,34 @@ async function getProjectAge(id, events, setState){ setState(timeSinceBlock(timestamp, 'days')) } +async function getProjectAssets(hash, setState){ + getFiles(hash) + .then((files) => { + setState(files) + const manifest = files[2] + console.log({files}, JSON.parse(manifest.content)) + }) + .catch(console.log) +} + function Project({ classes, match, profile, transfers, pledges, projectAddedEvents }) { const projectId = match.params.id const [projectAge, setAge] = useState(null) + const [projectAssets, setAssets] = useState(null) useEffect(() => { getProjectAge(projectId, projectAddedEvents, setAge) - }) + }, [projectAge]) + + useEffect(() => { + getProjectAssets('QmZbFULchk4wKdYoHv13jkTs2Wf4NYYJ38aCFG97g97DNn', setAssets) + }, []) + const received = useMemo(() => getReceivedAmount(projectId, transfers), [projectId, transfers]) const withdrawn = useMemo(() => getWithdrawnAmount(projectId, transfers), [projectId, transfers]) const amountsPledged = useMemo(() => getAmountsPledged(pledges), [pledges]) const numberOfBackers = useMemo(() => getNumberOfBackers(pledges), [pledges]) + console.log({profile, projectAssets}) return (
diff --git a/app/utils/ipfs.js b/app/utils/ipfs.js index 7191d35..cb3b2db 100644 --- a/app/utils/ipfs.js +++ b/app/utils/ipfs.js @@ -11,20 +11,31 @@ export const captureFile = (event, cb, imgCb) => { event.stopPropagation() event.preventDefault() const file = event.target.files[0] - saveToIpfs(file, cb, imgCb) + const files = event.target.files + const formattedFiles = formatFileList(files) + console.log({files, formattedFiles}) + saveToIpfs(formattedFiles, cb, imgCb) +} + +const formatFileList = files => { + const formattedList = [] + for (let i=0; i { const { name, type } = file const content = fileReaderPullStream(file) - return [{ + return { path: `/root/${name}`, content - }] + } } -const saveToIpfs = (file, cb, imgCb) => { +const saveToIpfs = (files, cb, imgCb) => { let ipfsId - ipfs.add(formatForIpfs(file), { progress: (prog) => console.log(`received: ${prog}`) }) + ipfs.add(files, { progress: (prog) => console.log(`received: ${prog}`) }) .then((response) => { console.log(response) ipfsId = response[0].hash @@ -41,7 +52,7 @@ export const getImageFromIpfs = async (hash, cb) => { }; export const getFromIpfs = async hash => { - const files = await getFile(hash) + const files = await getFiles(hash) const file = files.slice(-1)[0] const { content } = file const arrayBufferView = new Uint8Array(content) @@ -50,7 +61,7 @@ export const getFromIpfs = async hash => { return { ...file, img } } -export const getFile = CID => { +export const getFiles = CID => { const clean = CID.split('/').slice(-1)[0] return new Promise(function(resolve, reject) { ipfs.get(clean, function (err, files) {