add loading symbol

This commit is contained in:
Eddie Wang 2019-12-03 17:28:20 -05:00
parent 1571ab4677
commit 0b4b1dff3c
No known key found for this signature in database
GPG Key ID: DBFB3E83121BEDD1
1 changed files with 70 additions and 45 deletions

View File

@ -1,58 +1,79 @@
/** @jsx jsx */
import * as R from "ramda"
import { useCallback } from "react"
import { useCallback, useState } from "react"
import { useDropzone } from "react-dropzone"
import { Box, Flex, jsx } from "theme-ui"
import { CircularProgress } from "@material-ui/core"
/**
* nginx is setup to automatically handle and rewrite the url path.
*/
const API_ENDPOINT = "/api"
const API_ENDPOINT = "http://144.76.136.122/api"
const pName = R.prop("name")
const splitFilename = R.compose(R.head, R.split(".sia"))
function MyDropzone() {
const onDrop = useCallback(acceptedFiles => {
const file = R.head(acceptedFiles)
const fd = new FormData()
const fileName = R.compose(splitFilename, pName)(file)
console.log("filename", fileName)
fd.append("file", file)
if (window) {
const streamSaver = require("streamsaver")
const url = API_ENDPOINT + "/siafile"
fetch(url, {
method: "POST",
body: fd,
headers: {
"Access-Control-Allow-Origin": "*"
}
}).then(res => {
const readableStream = res.body
const fileStream = streamSaver.createWriteStream(fileName)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const onDrop = useCallback(
acceptedFiles => {
setLoading(true)
const file = R.head(acceptedFiles)
const fd = new FormData()
const fileName = R.compose(splitFilename, pName)(file)
// more optimized
if (window.WritableStream && readableStream.pipeTo) {
return readableStream
.pipeTo(fileStream)
.then(() => console.log("done writing"))
}
;(window as any).writer = fileStream.getWriter()
const reader = res.body.getReader()
const pump = () =>
reader
.read()
.then(res =>
res.done
? (window as any).writer.close()
: (window as any).writer.write(res.value).then(pump)
)
pump()
})
}
}, [])
fd.append("file", file)
if (window) {
const streamSaver = require("streamsaver")
const url = API_ENDPOINT + "/siafile"
fetch(url, {
method: "POST",
body: fd,
headers: {
"Access-Control-Allow-Origin": "*"
}
})
.then(res => {
if (!res.ok) {
setLoading(false)
setError(res.status + " " + res.statusText)
return
}
const readableStream = res.body
const fileStream = streamSaver.createWriteStream(fileName)
// more optimized
if (window.WritableStream && readableStream.pipeTo) {
return readableStream.pipeTo(fileStream).then(() => {
setLoading(false)
console.log("done writing")
})
}
;(window as any).writer = fileStream.getWriter()
const reader = res.body.getReader()
const pump = () =>
reader
.read()
.then(res =>
res.done
? (window as any).writer.close()
: (window as any).writer.write(res.value).then(pump)
)
.catch(e => {
setLoading(false)
})
pump()
})
.catch(e => {
setError(e)
setLoading(false)
})
}
},
[loading, setLoading, error, setError]
)
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop })
return (
@ -62,11 +83,15 @@ function MyDropzone() {
sx={{ height: 400, justifyContent: "center", alignItems: "center" }}
>
<input {...getInputProps()} />
{isDragActive ? (
<p>Drop to 🚀 ...</p>
) : (
<p>Drag 'n' drop a Sia file here, or click to select a Sia file.</p>
)}
{error && error}
{!error &&
(loading ? (
<CircularProgress color="secondary" />
) : isDragActive ? (
<p>Drop to 🚀 ...</p>
) : (
<p>Drag 'n' drop a Sia file here, or click to select a Sia file.</p>
))}
</Flex>
</Box>
)