This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/sia-skynet/src/components/HomeUpload/HomeUpload.js

132 lines
4.0 KiB
JavaScript
Raw Normal View History

2020-02-12 22:13:34 +00:00
import React, { Component } from "react";
import classNames from "classnames";
import Dropzone from "react-dropzone";
import Reveal from "react-reveal/Reveal";
2020-02-12 17:23:10 +00:00
2020-02-12 22:13:34 +00:00
import { Button, UploadFile } from "../";
2020-02-13 22:07:42 +00:00
import { Deco3, Deco4, Deco5, Folder, DownArrow } from "../../svg";
2020-02-12 22:13:34 +00:00
import "./HomeUpload.scss";
2020-02-12 17:23:10 +00:00
export default class HomeUpload extends Component {
2020-02-12 22:13:34 +00:00
state = { files: [] };
2020-02-12 17:23:10 +00:00
handleDrop = async acceptedFiles => {
this.setState({
files: [
...acceptedFiles.map(file => {
2020-02-12 22:13:34 +00:00
return { file, status: "uploading" };
2020-02-12 17:23:10 +00:00
}),
2020-02-12 22:13:34 +00:00
...this.state.files
]
});
2020-02-12 17:23:10 +00:00
2020-02-12 22:13:34 +00:00
acceptedFiles.forEach(async file => {
2020-02-13 19:23:57 +00:00
const url = `/api/skyfile?filename=${file.name}`;
2020-02-12 22:13:34 +00:00
const fd = new FormData();
fd.append("file", file);
2020-02-12 17:23:10 +00:00
const onComplete = (status, skylink) => {
2020-02-12 22:13:34 +00:00
this.setState(state => {
const index = state.files.findIndex(f => f.file === file);
2020-02-12 17:23:10 +00:00
2020-02-12 22:13:34 +00:00
return {
files: [
2020-02-12 17:23:10 +00:00
...state.files.slice(0, index),
2020-02-12 22:13:34 +00:00
{
...state.files[index],
status,
url: `https://siasky.net/${skylink}`
},
2020-02-12 17:23:10 +00:00
...state.files.slice(index + 1)
]
2020-02-12 22:13:34 +00:00
};
});
};
2020-02-12 17:23:10 +00:00
try {
const response = await fetch(url, {
method: "POST",
body: fd,
mode: "cors"
});
const { skylink } = await response.json();
2020-02-12 22:13:34 +00:00
onComplete("complete", skylink);
} catch (error) {
onComplete("error");
2020-02-12 17:23:10 +00:00
}
2020-02-12 22:13:34 +00:00
});
};
2020-02-12 17:23:10 +00:00
render() {
return (
<Reveal effect="active">
<div className="home-upload">
2020-02-13 22:07:42 +00:00
<div className="home-upload-white fadeInUp delay4">
<div className="home-upload-split">
<div className="home-upload-box ">
<Dropzone
onDrop={acceptedFiles => this.handleDrop(acceptedFiles)}
>
{({ getRootProps, getInputProps, isDragActive }) => (
<>
<div
className={classNames("home-upload-dropzone", {
"drop-active": isDragActive
})}
{...getRootProps()}
>
<span className="home-upload-text">
<h3>Pin a File</h3>
Drag &amp; drop your file(s) here to pin
</span>
<Button iconLeft>
<Folder />
Browse
</Button>
</div>
<input {...getInputProps()} className="offscreen" />
</>
)}
</Dropzone>
{this.state.files.length > 0 && (
<div className="home-uploaded-files">
{this.state.files.map((file, i) => {
return <UploadFile key={i} {...file} />;
2020-02-12 22:13:34 +00:00
})}
2020-02-12 17:23:10 +00:00
</div>
2020-02-13 22:07:42 +00:00
)}
</div>
<div className="home-upload-retrieve">
<div className="home-upload-text">
2020-02-13 22:28:16 +00:00
<h3>Have a Skylink?</h3>
2020-02-13 22:07:42 +00:00
<p>Enter the ID to retrieve the file</p>
2020-02-12 17:23:10 +00:00
2020-02-13 22:07:42 +00:00
<form className="home-upload-retrieve-form">
<input type="text" placeholder="sia://" />
<button type="submit">
<DownArrow />
</button>
</form>
</div>
2020-02-12 17:23:10 +00:00
</div>
2020-02-13 22:07:42 +00:00
</div>
2020-02-12 17:23:10 +00:00
</div>
2020-02-12 22:13:34 +00:00
<p className="bottom-text fadeInUp delay5">
Once a file has been uploaded, a 46 byte link called a 'Skylink' is
generated. That link can then be shared with anyone to fetch the
file from Skynet.
2020-02-12 17:23:10 +00:00
</p>
2020-02-12 22:13:34 +00:00
<Deco3 className="deco-3 fadeInUp delay6" />
<Deco4 className="deco-4 fadeInUp delay6" />
<Deco5 className="deco-5 fadeInUp delay6" />
2020-02-12 17:23:10 +00:00
</div>
</Reveal>
2020-02-12 22:13:34 +00:00
);
2020-02-12 17:23:10 +00:00
}
}