diff --git a/src/components/SingleFileUpload.jsx b/src/components/SingleFileUpload.jsx index 6ae1d42..f82517f 100644 --- a/src/components/SingleFileUpload.jsx +++ b/src/components/SingleFileUpload.jsx @@ -2,16 +2,20 @@ import React, { useState, useEffect } from "react" import csv from "csvtojson" import PropTypes from "prop-types" -export var fileContent = "{}" +export var fileContent = "{}"; +var uploadFailed = false; function SingleFileUpload({ onFileUploaded }) { const [, setFile] = useState() - const [fileUploaded, setFileUploaded] = useState(false) + const [fileUploaded, setFileUploaded] = useState(false); useEffect(() => { if (fileUploaded) { onFileUploaded() } + if(uploadFailed && !fileUploaded) { + onFileUploaded(); + } }, [fileUploaded]) // Reads file. @@ -19,16 +23,29 @@ function SingleFileUpload({ onFileUploaded }) { csv() .fromString(content) .preRawData((csvRawData) => { - csvRawData = csvRawData.replaceAll("Start Date", "Start_Date") - csvRawData = csvRawData.replaceAll("Start Time", "Start_Time") - csvRawData = csvRawData.replaceAll("End Date", "End_Date") - csvRawData = csvRawData.replaceAll("End Time", "End_Time") - csvRawData = csvRawData.replaceAll("Duration (decimal)", "Duration_Decimal") + + if(!checkCSVValidity(csvRawData)) { + alert("Invalid CSV File"); + uploadFailed = true; + setFileUploaded(false); + fileContent = {}; + return "{}"; + } + + csvRawData = csvRawData.replaceAll("Start Date", "Start_Date"); + csvRawData = csvRawData.replaceAll("Start Time", "Start_Time"); + csvRawData = csvRawData.replaceAll("End Date", "End_Date"); + csvRawData = csvRawData.replaceAll("End Time", "End_Time"); + csvRawData = csvRawData.replaceAll("Duration (decimal)", "Duration_Decimal"); + uploadFailed = false; + return csvRawData }) .then((csvRow) => { - fileContent = csvRow - setFileUploaded(true) + if(!uploadFailed) { + fileContent = csvRow; + setFileUploaded(true); + } }) } @@ -39,6 +56,12 @@ function SingleFileUpload({ onFileUploaded }) { const reader = new FileReader() reader.readAsText(changedFile) reader.onload = function (evt) { + var uploadedFile = document.getElementById('fileInput').files[0]; + if (uploadedFile.type != "application/vnd.ms-excel") { + alert("Wrong file type"); + fileContent = "{}"; + return; + } readCallback(evt.target.result) } } @@ -47,12 +70,27 @@ function SingleFileUpload({ onFileUploaded }) {