Skip to content

Commit

Permalink
simple IPFS uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
elmariachi111 committed Aug 12, 2020
1 parent e655cbb commit 27a34f3
Show file tree
Hide file tree
Showing 3 changed files with 4,299 additions and 931 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@web3-react/core": "^6.0.9",
"@web3-react/injected-connector": "^6.0.7",
"ethers": "^4.0.47",
"ipfs": "^0.44.0",
"react": "^16.8",
"react-dom": "^16.8",
"web3": "^1.2.7"
Expand Down
57 changes: 53 additions & 4 deletions src/IpfsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
import React from "react";
import React, { useState, useEffect } from "react";

import IPFS from "ipfs";
import _secrets from "../.secrets.json";

const IpfsPage: React.FC = () => {
const [ipfsNode, setIpfsNode] = useState();

const [files, setFiles] = useState([]);

async function addToIpfs(content: string | any[]): Promise<any[]> {
const addResult = ipfsNode!.add(content);
const results = [];

for await (const result of addResult) {
results.push(result);
}
return results;
}

const submit = async (e) => {
e.preventDefault();
e.stopPropagation();
const _currentContent = e.target["thecontent"].value;
const ipfsResult = await addToIpfs(_currentContent);
console.log(ipfsResult);

setFiles([...files, ipfsResult[0]]);
};

useEffect(() => {
(async () => {
const _ipfsNode = await IPFS.create();
console.log("ipfs node is running");
setIpfsNode(_ipfsNode);
})();
}, []);

return (
<div>
<p>
ipfs page
</p>
<form onSubmit={submit}>
<textarea name="thecontent">test</textarea>
<button type="submit" value="store!">
store!
</button>
</form>
<h2>Files:</h2>
<ul>
{files.map((f) => (
<li>
<a
href={`https://ipfs.io/ipfs/${f.cid.toString()}`}
target="_blank"
>
{f.cid.toString()}
</a>
</li>
))}
</ul>
</div>
);
};
Expand Down
Loading

0 comments on commit 27a34f3

Please sign in to comment.