From 0b4b1dff3c0fbc9c6b8af63038091118e1f79a4a Mon Sep 17 00:00:00 2001 From: Eddie Wang Date: Tue, 3 Dec 2019 17:28:20 -0500 Subject: [PATCH] add loading symbol --- .../src/components/Dropzone.tsx | 115 +++++++++++------- 1 file changed, 70 insertions(+), 45 deletions(-) diff --git a/packages/siaviewnode-client/src/components/Dropzone.tsx b/packages/siaviewnode-client/src/components/Dropzone.tsx index ff3b7640..47cc11e5 100644 --- a/packages/siaviewnode-client/src/components/Dropzone.tsx +++ b/packages/siaviewnode-client/src/components/Dropzone.tsx @@ -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" }} > - {isDragActive ? ( -

Drop to 🚀 ...

- ) : ( -

Drag 'n' drop a Sia file here, or click to select a Sia file.

- )} + {error && error} + {!error && + (loading ? ( + + ) : isDragActive ? ( +

Drop to 🚀 ...

+ ) : ( +

Drag 'n' drop a Sia file here, or click to select a Sia file.

+ ))} )