@@ -81,7 +81,7 @@ export default {
8181 let parts = file . name . split ( '.' )
8282 let extension = parts . at ( - 1 )
8383 let type = file . type || extension
84- context . catchAndParseFile ( content , type , new Date ( ) . getTime ( ) )
84+ context . catchAndParseFile ( content , type )
8585 }
8686 } )
8787 this . $refs . importRouteDropzone . removeAllFiles ( )
@@ -93,76 +93,103 @@ export default {
9393 * @param {* } type
9494 */
9595 catchAndParseFile ( fileContent , type ) {
96- let fileType = null
97- let newJobs = [ ]
98- let newVehicles = [ ]
96+ let parsedInfos = null
9997 let newSkills = [ ]
98+
10099 if ( type . indexOf ( 'csv' ) > - 1 ) {
101- fileType = 'csv'
102- if ( this . expectedData === 'jobs' ) {
103- newJobs = Job . fromCsv ( fileContent )
104- } else if ( this . expectedData === 'vehicles' ) {
105- newVehicles = Vehicle . fromCsv ( fileContent )
106- }
100+ parsedInfos = this . parseCsvFile ( fileContent )
107101 } else if ( type . indexOf ( 'json' ) > - 1 || type . indexOf ( 'geojson' ) > - 1 ) {
108102 const parsedJson = JSON . parse ( fileContent )
109103 if ( parsedJson && parsedJson . features ) {
110- fileType = 'geojson'
111- if ( this . expectedData === 'jobs' ) {
112- for ( const j of parsedJson . features ) {
113- try {
114- newJobs . push ( Job . fromGeoJsonObject ( j ) )
115- } catch {
116- this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.jobs' ) , )
117- }
118- }
119- } else if ( this . expectedData === 'vehicles' ) {
120- for ( const v of parsedJson . features ) {
121- try {
122- newVehicles . push ( Vehicle . fromGeoJsonObject ( v ) )
123- } catch {
124- this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.vehicles' ) , )
125- }
126- }
127- }
104+ parsedInfos = this . parseGeojsonFile ( parsedJson )
128105 } else {
129- fileType = 'json'
130- if ( this . expectedData === 'jobs' ) {
131- for ( const j of parsedJson ) {
132- try {
133- newJobs . push ( Job . fromObject ( j ) )
134- } catch {
135- this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.jobs' ) , )
136- }
137- }
138- } else if ( this . expectedData === 'vehicles' ) {
139- for ( const v of parsedJson ) {
140- try {
141- newVehicles . push ( Vehicle . fromObject ( v ) )
142- } catch {
143- this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.vehicles' ) , )
144- }
145- }
146- } else if ( this . expectedData === 'skills' ) {
147- for ( const s of parsedJson ) {
148- try {
149- newSkills . push ( Skill . fromObject ( s ) )
150- } catch {
151- this . showError ( this . $t ( 'optimizationImport.notValidSkill' ) )
152- }
153- }
154- }
106+ parsedInfos = this . parseJsonFile ( parsedJson )
107+ newSkills = parsedInfos . newSkills
155108 }
156109 }
157- if ( fileType ) {
158- this . $emit ( 'saveOptimizationImport' , { jobs : newJobs , vehicles : newVehicles , skills : newSkills } )
110+
111+ if ( parsedInfos ) {
112+ this . $emit ( 'saveOptimizationImport' , { jobs : parsedInfos . newJobs , vehicles : parsedInfos . newVehicles , skills : newSkills } )
159113 this . closeImporter ( )
160114 } else {
161115 this . showError ( this . $t ( 'routeImporter.failedToLoadFile' ) , { timeout : 0 } )
162116 this . $emit ( 'failedToImportFile' )
163117 }
164118 } ,
119+ parseCsvFile ( fileContent ) {
120+ let newJobs = [ ]
121+ let newVehicles = [ ]
122+
123+ if ( this . expectedData === 'jobs' ) {
124+ newJobs = Job . fromCsv ( fileContent )
125+ } else if ( this . expectedData === 'vehicles' ) {
126+ newVehicles = Vehicle . fromCsv ( fileContent )
127+ }
128+ return { newJobs, newVehicles}
129+ } ,
130+ /**
131+ * Parse geojson file
132+ * @param parsedJson
133+ * @returns {{newJobs: *[], newVehicles: *[]} }
134+ */
135+ parseGeojsonFile ( parsedJson ) {
136+ let newJobs = [ ]
137+ let newVehicles = [ ]
138+
139+ if ( this . expectedData === 'jobs' ) {
140+ for ( const j of parsedJson . features ) {
141+ try {
142+ newJobs . push ( Job . fromGeoJsonObject ( j ) )
143+ } catch {
144+ this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.jobs' ) , )
145+ }
146+ }
147+ } else if ( this . expectedData === 'vehicles' ) {
148+ for ( const v of parsedJson . features ) {
149+ try {
150+ newVehicles . push ( Vehicle . fromGeoJsonObject ( v ) )
151+ } catch {
152+ this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( 'optimization.vehicles' ) , )
153+ }
154+ }
155+ }
156+ return { newJobs, newVehicles}
157+ } ,
158+ /**
159+ * Parse json file
160+ * @param parsedJson
161+ * @returns {{newJobs: *[], newVehicles: *[], newSkills: *[]} }
162+ */
163+ parseJsonFile ( parsedJson ) {
164+ let newJobs = [ ]
165+ let newVehicles = [ ]
166+ let newSkills = [ ]
165167
168+ if ( this . expectedData === 'jobs' ) {
169+ newJobs = this . parseJsonObjects ( parsedJson , newJobs , Job , 'jobs' )
170+ } else if ( this . expectedData === 'vehicles' ) {
171+ newVehicles = this . parseJsonObjects ( parsedJson , newVehicles , Vehicle , 'vehicles' )
172+ } else if ( this . expectedData === 'skills' ) {
173+ for ( const s of parsedJson ) {
174+ try {
175+ newSkills . push ( Skill . fromObject ( s ) )
176+ } catch {
177+ this . showError ( this . $t ( 'optimizationImport.notValidSkill' ) )
178+ }
179+ }
180+ }
181+ return { newJobs, newVehicles, newSkills}
182+ } ,
183+ parseJsonObjects ( parsedJson , newObjects , ObjectClass , item ) {
184+ for ( const j of parsedJson ) {
185+ try {
186+ newObjects . push ( ObjectClass . fromObject ( j ) )
187+ } catch {
188+ this . showError ( this . $t ( 'optimizationImport.notValid' ) + this . $t ( `optimization.${ item } ` ) , )
189+ }
190+ }
191+ return newObjects
192+ } ,
166193 // save jobs from pasted JSON and return error if not a valid JSON
167194 savePastedJson ( ) {
168195 try {
0 commit comments