add loading symbol
This commit is contained in:
parent
1571ab4677
commit
0b4b1dff3c
|
@ -1,24 +1,29 @@
|
||||||
/** @jsx jsx */
|
/** @jsx jsx */
|
||||||
import * as R from "ramda"
|
import * as R from "ramda"
|
||||||
import { useCallback } from "react"
|
import { useCallback, useState } from "react"
|
||||||
import { useDropzone } from "react-dropzone"
|
import { useDropzone } from "react-dropzone"
|
||||||
import { Box, Flex, jsx } from "theme-ui"
|
import { Box, Flex, jsx } from "theme-ui"
|
||||||
|
import { CircularProgress } from "@material-ui/core"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nginx is setup to automatically handle and rewrite the url path.
|
* 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 pName = R.prop("name")
|
||||||
|
|
||||||
const splitFilename = R.compose(R.head, R.split(".sia"))
|
const splitFilename = R.compose(R.head, R.split(".sia"))
|
||||||
|
|
||||||
function MyDropzone() {
|
function MyDropzone() {
|
||||||
const onDrop = useCallback(acceptedFiles => {
|
const [loading, setLoading] = useState(false)
|
||||||
|
const [error, setError] = useState(null)
|
||||||
|
const onDrop = useCallback(
|
||||||
|
acceptedFiles => {
|
||||||
|
setLoading(true)
|
||||||
const file = R.head(acceptedFiles)
|
const file = R.head(acceptedFiles)
|
||||||
const fd = new FormData()
|
const fd = new FormData()
|
||||||
const fileName = R.compose(splitFilename, pName)(file)
|
const fileName = R.compose(splitFilename, pName)(file)
|
||||||
console.log("filename", fileName)
|
|
||||||
fd.append("file", file)
|
fd.append("file", file)
|
||||||
if (window) {
|
if (window) {
|
||||||
const streamSaver = require("streamsaver")
|
const streamSaver = require("streamsaver")
|
||||||
|
@ -29,15 +34,22 @@ function MyDropzone() {
|
||||||
headers: {
|
headers: {
|
||||||
"Access-Control-Allow-Origin": "*"
|
"Access-Control-Allow-Origin": "*"
|
||||||
}
|
}
|
||||||
}).then(res => {
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (!res.ok) {
|
||||||
|
setLoading(false)
|
||||||
|
setError(res.status + " " + res.statusText)
|
||||||
|
return
|
||||||
|
}
|
||||||
const readableStream = res.body
|
const readableStream = res.body
|
||||||
const fileStream = streamSaver.createWriteStream(fileName)
|
const fileStream = streamSaver.createWriteStream(fileName)
|
||||||
|
|
||||||
// more optimized
|
// more optimized
|
||||||
if (window.WritableStream && readableStream.pipeTo) {
|
if (window.WritableStream && readableStream.pipeTo) {
|
||||||
return readableStream
|
return readableStream.pipeTo(fileStream).then(() => {
|
||||||
.pipeTo(fileStream)
|
setLoading(false)
|
||||||
.then(() => console.log("done writing"))
|
console.log("done writing")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
;(window as any).writer = fileStream.getWriter()
|
;(window as any).writer = fileStream.getWriter()
|
||||||
const reader = res.body.getReader()
|
const reader = res.body.getReader()
|
||||||
|
@ -49,10 +61,19 @@ function MyDropzone() {
|
||||||
? (window as any).writer.close()
|
? (window as any).writer.close()
|
||||||
: (window as any).writer.write(res.value).then(pump)
|
: (window as any).writer.write(res.value).then(pump)
|
||||||
)
|
)
|
||||||
|
.catch(e => {
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
pump()
|
pump()
|
||||||
})
|
})
|
||||||
|
.catch(e => {
|
||||||
|
setError(e)
|
||||||
|
setLoading(false)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, [])
|
},
|
||||||
|
[loading, setLoading, error, setError]
|
||||||
|
)
|
||||||
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop })
|
const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -62,11 +83,15 @@ function MyDropzone() {
|
||||||
sx={{ height: 400, justifyContent: "center", alignItems: "center" }}
|
sx={{ height: 400, justifyContent: "center", alignItems: "center" }}
|
||||||
>
|
>
|
||||||
<input {...getInputProps()} />
|
<input {...getInputProps()} />
|
||||||
{isDragActive ? (
|
{error && error}
|
||||||
|
{!error &&
|
||||||
|
(loading ? (
|
||||||
|
<CircularProgress color="secondary" />
|
||||||
|
) : isDragActive ? (
|
||||||
<p>Drop to 🚀 ...</p>
|
<p>Drop to 🚀 ...</p>
|
||||||
) : (
|
) : (
|
||||||
<p>Drag 'n' drop a Sia file here, or click to select a Sia file.</p>
|
<p>Drag 'n' drop a Sia file here, or click to select a Sia file.</p>
|
||||||
)}
|
))}
|
||||||
</Flex>
|
</Flex>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
|
|
Reference in New Issue