1
- import { useCallback , useContext , useState } from 'react' ;
1
+ import { useContext , useState } from 'react' ;
2
2
3
3
import { Download } from '@osrd-project/ui-icons' ;
4
4
import { isNil } from 'lodash' ;
@@ -16,50 +16,6 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => {
16
16
const { t } = useTranslation ( [ 'operationalStudies/importTrainSchedule' ] ) ;
17
17
const { closeModal } = useContext ( ModalContext ) ;
18
18
const [ selectedFile , setSelectedFile ] = useState < File | undefined > ( undefined ) ;
19
- const [ isValid , setIsValid ] = useState < string | undefined > ( undefined ) ;
20
-
21
- const parseXML = ( xmlString : string ) => {
22
- try {
23
- const parser = new DOMParser ( ) ;
24
- const xmlDoc = parser . parseFromString ( xmlString , 'application/xml' ) ;
25
- const parserError = xmlDoc . getElementsByTagName ( 'parsererror' ) ;
26
- if ( parserError . length > 0 ) {
27
- throw new Error ( 'Invalid XML' ) ;
28
- }
29
- return undefined ;
30
- } catch ( error ) {
31
- return t ( 'errorMessages.errorInvalidXMLFormat' ) . toString ( ) ;
32
- }
33
- } ;
34
- // TODO : create the translation keys
35
- const validateFile = useCallback (
36
- async ( fileToValidate : File ) : Promise < string | undefined > => {
37
- if ( fileToValidate . size === 0 ) {
38
- return t ( 'errorMessages.errorEmptyFile' ) . toString ( ) ;
39
- }
40
- if ( fileToValidate . type === 'application/json' ) {
41
- try {
42
- JSON . parse ( await fileToValidate . text ( ) ) ;
43
- } catch ( e ) {
44
- return t ( 'errorMessages.errorInvalidJSONFormat' ) . toString ( ) ;
45
- }
46
- } else if (
47
- fileToValidate . type === 'application/railml' ||
48
- fileToValidate . name . endsWith ( '.railml' )
49
- ) {
50
- const fileContent = await fileToValidate . text ( ) ;
51
- const xmlError = parseXML ( fileContent ) ;
52
- if ( xmlError ) {
53
- return xmlError ;
54
- }
55
- } else {
56
- return t ( 'errorMessages.errorUnsupportedFileType' ) . toString ( ) ;
57
- }
58
-
59
- return undefined ;
60
- } ,
61
- [ t ]
62
- ) ;
63
19
64
20
return (
65
21
< >
@@ -73,21 +29,15 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => {
73
29
< input
74
30
type = "file"
75
31
name = "file"
76
- accept = ".json,.xml,.railml"
32
+ accept = ".json,.txt,. xml,.railml"
77
33
onChange = { async ( e ) => {
78
34
if ( e . target . files && e . target . files . length > 0 ) {
79
- const error = await validateFile ( e . target . files [ 0 ] ) ;
80
- setIsValid ( error ) ;
81
- if ( isNil ( error ) ) {
82
- setSelectedFile ( e . target . files [ 0 ] ) ;
83
- }
35
+ setSelectedFile ( e . target . files [ 0 ] ) ;
84
36
} else {
85
37
setSelectedFile ( undefined ) ;
86
- setIsValid ( undefined ) ;
87
38
}
88
39
} }
89
40
/>
90
- { ! isNil ( isValid ) && < div className = "text-danger" > { isValid } </ div > }
91
41
</ >
92
42
</ ModalBodySNCF >
93
43
< ModalFooterSNCF >
@@ -105,7 +55,7 @@ const UploadFileModal = ({ handleSubmit }: UploadFileModalProps) => {
105
55
< div className = "col-6" >
106
56
< button
107
57
type = "button"
108
- disabled = { isNil ( selectedFile ) || ! isNil ( isValid ) }
58
+ disabled = { isNil ( selectedFile ) }
109
59
className = "btn btn-block btn-sm btn-primary"
110
60
onClick = { ( ) => {
111
61
if ( selectedFile ) handleSubmit ( selectedFile ) ;
0 commit comments