Skip to content

Commit 0bc7826

Browse files
committed
refactor: reduce complexity
1 parent eb878d3 commit 0bc7826

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

src/fragments/forms/map-form/components/optimization/components/optimization-import/optimization-import.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ export default {
116116
this.$emit('failedToImportFile')
117117
}
118118
},
119-
/**
120-
* Parse csv file
121-
* @param fileContent
122-
* @returns {{newJobs: *[], newVehicles: *[]}}
123-
*/
124119
parseCsvFile(fileContent) {
125120
let newJobs = []
126121
let newVehicles = []
@@ -171,21 +166,9 @@ export default {
171166
let newSkills = []
172167

173168
if (this.expectedData === 'jobs') {
174-
for (const j of parsedJson) {
175-
try {
176-
newJobs.push(Job.fromObject(j))
177-
} catch {
178-
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.jobs'),)
179-
}
180-
}
169+
newJobs = this.parseJsonObjects(parsedJson, newJobs, Job, 'jobs')
181170
} else if (this.expectedData === 'vehicles') {
182-
for (const v of parsedJson) {
183-
try {
184-
newVehicles.push(Vehicle.fromObject(v))
185-
} catch {
186-
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.vehicles'),)
187-
}
188-
}
171+
newVehicles = this.parseJsonObjects(parsedJson, newVehicles, Vehicle, 'vehicles')
189172
} else if (this.expectedData === 'skills') {
190173
for (const s of parsedJson) {
191174
try {
@@ -197,7 +180,16 @@ export default {
197180
}
198181
return {newJobs, newVehicles, newSkills}
199182
},
200-
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+
},
201193
// save jobs from pasted JSON and return error if not a valid JSON
202194
savePastedJson() {
203195
try {

0 commit comments

Comments
 (0)