From 58feda32b75233a489bf5bdb11461b13ff68a4fa Mon Sep 17 00:00:00 2001 From: Clara Ni Date: Thu, 19 Dec 2024 15:06:51 +0100 Subject: [PATCH 1/5] front: support any type of files in importTrainSchedules Signed-off-by: Clara Ni --- .../importTrainSchedule.json | 4 +- .../importTrainSchedule.json | 4 +- front/src/common/uploadFileModal.tsx | 58 ++----------------- .../ImportTrainScheduleConfig.tsx | 33 +++++++---- .../helpers/handleParseFiles.ts | 56 +++++------------- 5 files changed, 42 insertions(+), 113 deletions(-) diff --git a/front/public/locales/en/operationalStudies/importTrainSchedule.json b/front/public/locales/en/operationalStudies/importTrainSchedule.json index ee826430b13..ce9c24adbbf 100644 --- a/front/public/locales/en/operationalStudies/importTrainSchedule.json +++ b/front/public/locales/en/operationalStudies/importTrainSchedule.json @@ -9,13 +9,11 @@ "error": "An error has occurred", "errorEmptyFile": "Empty file", "errorImport": "Unable to convert data to TrainSchedule", - "errorInvalidJSONFormat": "Invalid JSON format", - "errorInvalidXMLFormat": "Invalid XML format", "errorNoDate": "You must enter a date.", "errorNoFrom": "You must fill in an origin.", "errorNoTo": "You must fill in a destination.", "errorSameFromTo": "Origin and destination must be different", - "errorUnsupportedFileType": "Unsupported file type" + "errorInvalidFile": "Invalid file" }, "failure": "Operation failed", "from": "Origin", diff --git a/front/public/locales/fr/operationalStudies/importTrainSchedule.json b/front/public/locales/fr/operationalStudies/importTrainSchedule.json index 25048276f3b..a53558a1bda 100644 --- a/front/public/locales/fr/operationalStudies/importTrainSchedule.json +++ b/front/public/locales/fr/operationalStudies/importTrainSchedule.json @@ -9,13 +9,11 @@ "error": "Une erreur est survenue", "errorEmptyFile": "Fichier vide", "errorImport": "Impossible de convertir les données en TrainSchedule", - "errorInvalidJSONFormat": "Format JSON invalide", - "errorInvalidXMLFormat": "Format XML invalide", "errorNoDate": "Vous devez renseigner une date.", "errorNoFrom": "Vous devez renseigner une origine.", "errorNoTo": "Vous devez renseigner une destination.", "errorSameFromTo": "L'origine et la destination doivent être différentes.", - "errorUnsupportedFileType": "Type de fichier non supporté" + "errorInvalidFile": "Fichier invalide" }, "failure": "Opération échouée", "from": "Origine", diff --git a/front/src/common/uploadFileModal.tsx b/front/src/common/uploadFileModal.tsx index f465e29186b..0b0e7146da7 100644 --- a/front/src/common/uploadFileModal.tsx +++ b/front/src/common/uploadFileModal.tsx @@ -1,4 +1,4 @@ -import { useCallback, useContext, useState } from 'react'; +import { useContext, useState } from 'react'; import { Download } from '@osrd-project/ui-icons'; import { isNil } from 'lodash'; @@ -16,50 +16,6 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => { const { t } = useTranslation(['operationalStudies/importTrainSchedule']); const { closeModal } = useContext(ModalContext); const [selectedFile, setSelectedFile] = useState(undefined); - const [isValid, setIsValid] = useState(undefined); - - const parseXML = (xmlString: string) => { - try { - const parser = new DOMParser(); - const xmlDoc = parser.parseFromString(xmlString, 'application/xml'); - const parserError = xmlDoc.getElementsByTagName('parsererror'); - if (parserError.length > 0) { - throw new Error('Invalid XML'); - } - return undefined; - } catch (error) { - return t('errorMessages.errorInvalidXMLFormat').toString(); - } - }; - // TODO : create the translation keys - const validateFile = useCallback( - async (fileToValidate: File): Promise => { - if (fileToValidate.size === 0) { - return t('errorMessages.errorEmptyFile').toString(); - } - if (fileToValidate.type === 'application/json') { - try { - JSON.parse(await fileToValidate.text()); - } catch (e) { - return t('errorMessages.errorInvalidJSONFormat').toString(); - } - } else if ( - fileToValidate.type === 'application/railml' || - fileToValidate.name.endsWith('.railml') - ) { - const fileContent = await fileToValidate.text(); - const xmlError = parseXML(fileContent); - if (xmlError) { - return xmlError; - } - } else { - return t('errorMessages.errorUnsupportedFileType').toString(); - } - - return undefined; - }, - [t] - ); return ( <> @@ -73,21 +29,15 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => { { if (e.target.files && e.target.files.length > 0) { - const error = await validateFile(e.target.files[0]); - setIsValid(error); - if (isNil(error)) { - setSelectedFile(e.target.files[0]); - } + setSelectedFile(e.target.files[0]); } else { setSelectedFile(undefined); - setIsValid(undefined); } }} /> - {!isNil(isValid) &&
{isValid}
} @@ -105,7 +55,7 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => {