Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat UGN-207 - added loading state to DropZone #15

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/components/DropZone.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Box, Button, Text, useTheme, useToast } from "@chakra-ui/react"
import { getDropZoneBorder } from "../utils/getDropZoneBorder"
import { useDropzone } from "react-dropzone"
import XLSX from "xlsx"
import { useState } from "react"
import { getDropZoneBorder } from "../utils/getDropZoneBorder"

const UPLOAD_TITLE = "Upload .xlsx, .xls or .csv file"
const ERROR_TOAST_DESCRIPTION = "upload rejected"
const ACTIVE_DROP_ZONE_TITLE = "Drop file here..."
const BUTTON_TITLE = "Select file"
const LOADING_TITLE = "Processing..."

type DropZoneProps = {
onContinue: (data: XLSX.WorkBook) => void
Expand All @@ -17,12 +19,14 @@ export const DropZone = ({ onContinue }: DropZoneProps) => {
colors: { rsi },
} = useTheme()
const toast = useToast()
const [loading, setLoading] = useState(false)
const { getRootProps, getInputProps, isDragActive, open } = useDropzone({
noClick: true,
noKeyboard: true,
maxFiles: 1,
accept: ".xls, .csv, .xlsx",
onDropRejected: (fileRejections) => {
setLoading(false)
fileRejections.forEach((fileRejection) => {
toast({
status: "error",
Expand All @@ -32,18 +36,18 @@ export const DropZone = ({ onContinue }: DropZoneProps) => {
})
},
onDrop: async ([file]) => {
setLoading(true)
const arrayBuffer = await file.arrayBuffer()
const workbook = XLSX.read(arrayBuffer)
onContinue(workbook)
},
})

return (
<Box
{...getRootProps()}
{...getDropZoneBorder(rsi["500"])}
width="100%"
cursor="pointer"
onClick={open}
display="flex"
justifyContent="center"
alignItems="center"
Expand All @@ -55,12 +59,18 @@ export const DropZone = ({ onContinue }: DropZoneProps) => {
<Text size="lg" lineHeight={7} fontWeight="semibold">
{ACTIVE_DROP_ZONE_TITLE}
</Text>
) : loading ? (
<Text size="lg" lineHeight={7} fontWeight="semibold">
{LOADING_TITLE}
</Text>
) : (
<>
<Text fontSize="lg" lineHeight={7} fontWeight="semibold">
{UPLOAD_TITLE}
</Text>
<Button mt="1rem">{BUTTON_TITLE}</Button>
<Button mt="1rem" onClick={open}>
{BUTTON_TITLE}
</Button>
</>
)}
</Box>
Expand Down
2 changes: 1 addition & 1 deletion src/components/SelectSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flex, Heading, Radio, RadioGroup, Stack, Button } from "@chakra-ui/react"
import { Flex, Heading, Radio, RadioGroup, Stack } from "@chakra-ui/react"
import { useState } from "react"
import { ContinueButton } from "./ContinueButton"

Expand Down