From 5608b91966a1678f09ac577a935b124982a90b92 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Fri, 9 Apr 2021 15:03:49 +0200 Subject: [PATCH] fix: file download redirects to bee node, upload limited to 1 file (#62) * fix: file download redirects to bee node, upload limited to 1 file, error handling * fix: upload error handling * chore: altered naming as per PR review * chore: renamed retrieve and browse to download --- src/pages/files/index.tsx | 197 +++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 108 deletions(-) diff --git a/src/pages/files/index.tsx b/src/pages/files/index.tsx index 84afcc5..e412890 100644 --- a/src/pages/files/index.tsx +++ b/src/pages/files/index.tsx @@ -2,14 +2,15 @@ import { ReactElement, useState } from 'react' import { beeApi } from '../../services/bee' import { makeStyles, Theme, createStyles } from '@material-ui/core/styles' -import { Paper, InputBase, IconButton, Button, Container, CircularProgress } from '@material-ui/core' +import { Paper, InputBase, IconButton, Button, Container, CircularProgress, FormHelperText } from '@material-ui/core' import { Search } from '@material-ui/icons' import { DropzoneArea } from 'material-ui-dropzone' import ClipboardCopy from '../../components/ClipboardCopy' import TroubleshootConnectionCard from '../../components/TroubleshootConnectionCard' -import { Data, FileData } from '@ethersphere/bee-js' import { useApiHealth, useDebugApiHealth } from '../../hooks/apiHooks' +import { apiHost } from '../../constants' +import { Utils } from '@ethersphere/bee-js' const useStyles = makeStyles((theme: Theme) => createStyles({ @@ -17,7 +18,6 @@ const useStyles = makeStyles((theme: Theme) => padding: '2px 4px', display: 'flex', alignItems: 'center', - width: 400, }, input: { marginLeft: theme.spacing(1), @@ -36,133 +36,114 @@ const useStyles = makeStyles((theme: Theme) => export default function Files(): ReactElement { const classes = useStyles() - const [inputMode, setInputMode] = useState<'browse' | 'upload'>('browse') - const [searchInput, setSearchInput] = useState('') - const [searchResult, setSearchResult] = useState | null>(null) - const [loadingSearch, setLoadingSearch] = useState(false) + const [inputMode, setInputMode] = useState<'download' | 'upload'>('download') + const [referenceInput, setReferenceInput] = useState('') + const [referenceError, setReferenceError] = useState(null) const { health, isLoadingHealth } = useApiHealth() const { nodeHealth, isLoadingNodeHealth } = useDebugApiHealth() - const [files, setFiles] = useState([]) + const [file, setFile] = useState(null) const [uploadReference, setUploadReference] = useState('') - const [uploadingFile, setUploadingFile] = useState(false) - - const getFile = () => { - setLoadingSearch(true) - beeApi.files - .downloadFile(searchInput) - .then(res => { - setSearchResult(res) - - const downloadUrl = window.URL.createObjectURL(new Blob([res.data])) - const link = document.createElement('a') - link.href = downloadUrl - link.setAttribute('download', 'file.zip') //any other extension - document.body.appendChild(link) - link.click() - link.remove() - }) - .catch(() => { - // FIXME: handle the error - }) - .finally(() => { - setLoadingSearch(false) - }) - } + const [uploadError, setUploadError] = useState(null) + const [isUploadingFile, setIsUploadingFile] = useState(false) const uploadFile = () => { - setUploadingFile(true) + if (file === null) return + setIsUploadingFile(true) + setUploadError(null) beeApi.files - .uploadFile(files[0]) + .uploadFile(file) .then(hash => { setUploadReference(hash) - setFiles([]) - }) - .catch(() => { - // FIXME: handle the error + setFile(null) }) + .catch(setUploadError) .finally(() => { - setUploadingFile(false) + setIsUploadingFile(false) }) } const handleChange = (files?: File[]) => { if (files) { - setFiles(files) + setFile(files[0]) + setUploadReference('') } } + const handleReferenceChange = (e: React.ChangeEvent) => { + setReferenceInput(e.target.value) + + if (Utils.Hex.isHexString(e.target.value, 64)) setReferenceError(null) + else setReferenceError(new Error('Incorrect format of swarm hash')) + } + + if (isLoadingHealth || isLoadingNodeHealth) { + return ( + + + + ) + } + + if (!health || nodeHealth?.status !== 'ok') return + return ( -
- { - // FIXME: this should be broken up - /* eslint-disable no-nested-ternary */ - nodeHealth?.status === 'ok' && health ? ( - -
- - + +
+ {inputMode === 'download' && ( + <> + + + + + + + {referenceError && {referenceError.message}} + + )} + {inputMode === 'upload' && ( +
+
+ +
+ + {isUploadingFile && ( + + + + )} + {uploadReference && ( +
+ {uploadReference} + +
+ )} + {uploadError && {uploadError.message}}
- {inputMode === 'browse' ? ( - - setSearchInput(e.target.value)} - /> - getFile()} className={classes.iconButton} aria-label="search"> - - - - ) : ( -
- {uploadingFile ? ( - - - - ) : ( -
- {uploadReference ? ( - - {uploadReference} - - - ) : null} - -
- -
-
- )} -
- )} - {loadingSearch ? ( - - - - ) : ( -
{searchResult}
- )} - - ) : isLoadingHealth || isLoadingNodeHealth ? ( - - - - ) : ( - - ) /* eslint-enable no-nested-ternary */ - } -
+
+
+ )} + ) }