Skip to content

Commit

Permalink
refactor: reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Seli0303 committed Jun 6, 2024
1 parent e4bca7c commit 9287bf1
Showing 1 changed file with 83 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default {
let parts = file.name.split('.')
let extension = parts.at(-1)
let type = file.type || extension
context.catchAndParseFile(content, type, new Date().getTime())
context.catchAndParseFile(content, type)
}
})
this.$refs.importRouteDropzone.removeAllFiles()
Expand All @@ -93,76 +93,103 @@ export default {
* @param {*} type
*/
catchAndParseFile (fileContent, type) {
let fileType = null
let newJobs = []
let newVehicles = []
let parsedInfos = null
let newSkills = []

if (type.indexOf('csv') > -1) {
fileType = 'csv'
if (this.expectedData === 'jobs') {
newJobs = Job.fromCsv(fileContent)
} else if (this.expectedData === 'vehicles') {
newVehicles = Vehicle.fromCsv(fileContent)
}
parsedInfos = this.parseCsvFile(fileContent)
} else if (type.indexOf('json') > -1 || type.indexOf('geojson') > -1) {
const parsedJson = JSON.parse(fileContent)
if (parsedJson && parsedJson.features) {
fileType = 'geojson'
if (this.expectedData === 'jobs') {
for (const j of parsedJson.features) {
try {
newJobs.push(Job.fromGeoJsonObject(j))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.jobs'),)
}
}
} else if (this.expectedData === 'vehicles') {
for (const v of parsedJson.features) {
try {
newVehicles.push(Vehicle.fromGeoJsonObject(v))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.vehicles'),)
}
}
}
parsedInfos = this.parseGeojsonFile(parsedJson)
} else {
fileType = 'json'
if (this.expectedData === 'jobs') {
for (const j of parsedJson) {
try {
newJobs.push(Job.fromObject(j))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.jobs'),)
}
}
} else if (this.expectedData === 'vehicles') {
for (const v of parsedJson) {
try {
newVehicles.push(Vehicle.fromObject(v))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.vehicles'),)
}
}
} else if (this.expectedData === 'skills') {
for (const s of parsedJson) {
try {
newSkills.push(Skill.fromObject(s))
} catch {
this.showError(this.$t('optimizationImport.notValidSkill'))
}
}
}
parsedInfos= this.parseJsonFile(parsedJson)
newSkills = parsedInfos.newSkills
}
}
if (fileType) {
this.$emit('saveOptimizationImport', {jobs: newJobs, vehicles: newVehicles, skills: newSkills})

if (parsedInfos) {
this.$emit('saveOptimizationImport', {jobs: parsedInfos.newJobs, vehicles: parsedInfos.newVehicles, skills: newSkills})
this.closeImporter()
} else {
this.showError(this.$t('routeImporter.failedToLoadFile'), {timeout: 0})
this.$emit('failedToImportFile')
}
},
parseCsvFile(fileContent) {
let newJobs = []
let newVehicles = []

if (this.expectedData === 'jobs') {
newJobs = Job.fromCsv(fileContent)
} else if (this.expectedData === 'vehicles') {
newVehicles = Vehicle.fromCsv(fileContent)
}
return {newJobs, newVehicles}
},
/**
* Parse geojson file
* @param parsedJson
* @returns {{newJobs: *[], newVehicles: *[]}}
*/
parseGeojsonFile(parsedJson) {
let newJobs = []
let newVehicles = []

if (this.expectedData === 'jobs') {
for (const j of parsedJson.features) {
try {
newJobs.push(Job.fromGeoJsonObject(j))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.jobs'),)
}
}
} else if (this.expectedData === 'vehicles') {
for (const v of parsedJson.features) {
try {
newVehicles.push(Vehicle.fromGeoJsonObject(v))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t('optimization.vehicles'),)
}
}
}
return {newJobs, newVehicles}
},
/**
* Parse json file
* @param parsedJson
* @returns {{newJobs: *[], newVehicles: *[], newSkills: *[]}}
*/
parseJsonFile(parsedJson) {
let newJobs = []
let newVehicles = []
let newSkills = []

if (this.expectedData === 'jobs') {
newJobs = this.parseJsonObjects(parsedJson, newJobs, Job, 'jobs')
} else if (this.expectedData === 'vehicles') {
newVehicles = this.parseJsonObjects(parsedJson, newVehicles, Vehicle, 'vehicles')
} else if (this.expectedData === 'skills') {
for (const s of parsedJson) {
try {
newSkills.push(Skill.fromObject(s))
} catch {
this.showError(this.$t('optimizationImport.notValidSkill'))
}
}
}
return {newJobs, newVehicles, newSkills}
},
parseJsonObjects(parsedJson, newObjects, ObjectClass, item) {
for (const j of parsedJson) {
try {
newObjects.push(ObjectClass.fromObject(j))
} catch {
this.showError(this.$t('optimizationImport.notValid') + this.$t(`optimization.${item}`),)
}
}
return newObjects
},
// save jobs from pasted JSON and return error if not a valid JSON
savePastedJson() {
try {
Expand Down

0 comments on commit 9287bf1

Please sign in to comment.