|
@ -1,4 +1,5 @@ |
|
|
import React, { useState, useEffect } from "react" |
|
|
import React, { useState, useEffect } from "react" |
|
|
|
|
|
import { useToasts } from '@geist-ui/core'; |
|
|
import PropTypes from "prop-types" |
|
|
import PropTypes from "prop-types" |
|
|
|
|
|
|
|
|
export var fileContent = "{}" |
|
|
export var fileContent = "{}" |
|
@ -8,9 +9,15 @@ function SingleFileUpload({ onFileUploaded }) { |
|
|
const [, setFile] = useState() |
|
|
const [, setFile] = useState() |
|
|
const [fileUploaded, setFileUploaded] = useState(false) |
|
|
const [fileUploaded, setFileUploaded] = useState(false) |
|
|
|
|
|
|
|
|
|
|
|
const { setToast } = useToasts() |
|
|
|
|
|
const showSuccessToast = () => setToast({ text: "File successfully uploaded", delay: 2000 }); |
|
|
|
|
|
const showInvalidCSVToast = () => setToast({ text: "Invalid CSV File", delay: 2000 }); |
|
|
|
|
|
const showInvalidFileToast = () => setToast({ text: "Wrong file type", delay: 2000 }); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
|
if (fileUploaded) { |
|
|
if (fileUploaded) { |
|
|
onFileUploaded() |
|
|
onFileUploaded() |
|
|
|
|
|
showSuccessToast(); |
|
|
} |
|
|
} |
|
|
if (uploadFailed && !fileUploaded) { |
|
|
if (uploadFailed && !fileUploaded) { |
|
|
onFileUploaded() |
|
|
onFileUploaded() |
|
@ -20,7 +27,7 @@ function SingleFileUpload({ onFileUploaded }) { |
|
|
// Reads file. |
|
|
// Reads file. |
|
|
var readCallback = function (csvRawData) { |
|
|
var readCallback = function (csvRawData) { |
|
|
if (!checkCSVValidity(csvRawData)) { |
|
|
if (!checkCSVValidity(csvRawData)) { |
|
|
alert("Invalid CSV File") |
|
|
|
|
|
|
|
|
showInvalidCSVToast(); |
|
|
uploadFailed = true |
|
|
uploadFailed = true |
|
|
setFileUploaded(false) |
|
|
setFileUploaded(false) |
|
|
fileContent = {} |
|
|
fileContent = {} |
|
@ -49,7 +56,7 @@ function SingleFileUpload({ onFileUploaded }) { |
|
|
reader.onload = function (evt) { |
|
|
reader.onload = function (evt) { |
|
|
var uploadedFile = document.getElementById("fileInput").files[0] |
|
|
var uploadedFile = document.getElementById("fileInput").files[0] |
|
|
if (uploadedFile.type != "application/vnd.ms-excel") { |
|
|
if (uploadedFile.type != "application/vnd.ms-excel") { |
|
|
alert("Wrong file type") |
|
|
|
|
|
|
|
|
showInvalidFileToast(); |
|
|
fileContent = "{}" |
|
|
fileContent = "{}" |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|