Skip to content

Commit cd26d64

Browse files
authored
refactor a couple things for less code complexity (#6)
1 parent 695f510 commit cd26d64

File tree

2 files changed

+58
-60
lines changed

2 files changed

+58
-60
lines changed

src/components/SubmitButton.js

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,22 @@ import Button from "@material-ui/core/Button";
44
import CheckIcon from "@material-ui/icons/Check";
55

66
const SubmitButton = props => {
7-
if (!props.submitted) {
8-
return (
9-
<Button
10-
variant="contained"
11-
color="secondary"
12-
type="submit"
13-
className={props.classes.button}
14-
>
15-
{props.textNormal}
16-
{props.icon}
17-
</Button>
18-
);
19-
} else {
20-
return (
21-
<Button
22-
variant="contained"
23-
color="secondary"
24-
type="submit"
25-
className={props.classes.button}
26-
disabled
27-
>
28-
{props.textSubmitted}
29-
<CheckIcon className={props.classes.icon} />
30-
</Button>
31-
);
32-
}
7+
const { submitted, textNormal, textSubmitted } = props;
8+
const text = submitted ? textSubmitted : textNormal;
9+
const icon = submitted ? <CheckIcon /> : props.icon;
10+
11+
return (
12+
<Button
13+
variant="contained"
14+
color="secondary"
15+
type="submit"
16+
disabled={submitted}
17+
className={props.classes.button}
18+
>
19+
{text}
20+
{icon}
21+
</Button>
22+
);
3323
};
3424

3525
const styles = theme =>

src/ipfs/getIpfs.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,41 @@ export const ipfsIsWorking = async ipfs => {
1414
}
1515
};
1616

17-
const getIpfs = (permissions = DEFAULT_PERMISSIONS) => {
18-
return new Promise(async (resolve, reject) => {
19-
// if already instantiated
20-
if (ipfsInstance) {
21-
return resolve(ipfsInstance);
22-
}
23-
24-
if (window.ipfs) {
25-
let ipfs = window.ipfs;
26-
if (window.ipfs.enable) {
27-
console.log("window.ipfs.enable is available!");
28-
// if user set permissions, make sure id and version are both added
29-
if (permissions.indexOf("id") < 0) {
30-
permissions.push("id");
31-
}
32-
if (permissions.indexOf("version") < 0) {
33-
permissions.push("version");
34-
}
35-
ipfs = await window.ipfs.enable({
36-
commands: permissions
37-
});
38-
} else {
39-
console.log("legacy window.ipfs is available!");
17+
export const loadWindowIpfs = async permissions => {
18+
if (window.ipfs) {
19+
let ipfs = window.ipfs;
20+
if (window.ipfs.enable) {
21+
// if user set permissions, make sure id and version are both added
22+
if (permissions.indexOf("id") < 0) {
23+
permissions.push("id");
4024
}
41-
const isWorking = await ipfsIsWorking(ipfs);
42-
if (isWorking) {
43-
ipfsInstance = ipfs;
44-
resolve(ipfsInstance);
45-
} else {
46-
console.log(
47-
"cannot access window.ipfs, may not be connected to a working gateway"
48-
);
25+
if (permissions.indexOf("version") < 0) {
26+
permissions.push("version");
4927
}
28+
ipfs = await window.ipfs.enable({
29+
commands: permissions
30+
});
31+
} else {
5032
}
33+
const isWorking = await ipfsIsWorking(ipfs);
34+
if (isWorking) {
35+
return ipfs;
36+
} else {
37+
return null;
38+
}
39+
}
40+
return null;
41+
};
5142

52-
console.log("window.ipfs is not available, downloading from CDN...");
43+
export const loadJsIpfs = async () => {
44+
return new Promise((resolve, reject) => {
5345
const script = document.createElement("script");
5446
script.src = "https://unpkg.com/ipfs/dist/index.min.js";
5547
script.onload = async () => {
5648
const ipfs = await window.Ipfs.create();
5749
const isWorking = await ipfsIsWorking(ipfs);
5850
if (isWorking) {
59-
ipfsInstance = ipfs;
60-
resolve(ipfsInstance);
51+
resolve(ipfs);
6152
} else {
6253
reject(new Error("js-ipfs is not able to load"));
6354
}
@@ -67,4 +58,21 @@ const getIpfs = (permissions = DEFAULT_PERMISSIONS) => {
6758
});
6859
};
6960

61+
const getIpfs = (permissions = DEFAULT_PERMISSIONS) => {
62+
// if already instantiated
63+
if (ipfsInstance) {
64+
return ipfsInstance;
65+
}
66+
67+
ipfsInstance = await loadWindowIpfs(permissions)
68+
if(ipfsInstance){
69+
console.log("window.ipfs is available!")
70+
return ipfsInstance
71+
}
72+
73+
console.log("window.ipfs is not available, downloading from CDN...");
74+
ipfsInstance = await loadJsIpfs();
75+
return ipfsInstance
76+
};
77+
7078
export default getIpfs;

0 commit comments

Comments
 (0)