Skip to content

Commit 4b85c61

Browse files
committed
refactor: decrease complexity
1 parent e1ab0d7 commit 4b85c61

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

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

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -493,47 +493,63 @@ export default {
493493
*/
494494
loadData () {
495495
if (this.$store.getters.mode === constants.modes.optimization) {
496-
// Empty the array and populate it with the
497-
// places from the appRoute without changing the
498-
// object reference because it is a prop
499-
const defaultJobs = this.jobs
500-
const defaultVehicles = this.vehicles
501-
const jobProps = this.parseProps(this.$store.getters.appRouteData.options.jobProps)
502-
const urlVehicles = this.$store.getters.appRouteData.options.vehicles
503-
const places = this.$store.getters.appRouteData.places
504-
let storedJobs = localStorage.getItem('jobs')
505-
let storedVehicles = localStorage.getItem('vehicles')
506-
// prioritise data from url, then data from local storage
507-
if (urlVehicles) {
508-
const vehicles = []
509-
for (let v of urlVehicles) {
510-
vehicles.push(Vehicle.fromObject(v))
511-
}
512-
this.vehicles = vehicles
513-
} else if (this.vehicles === undefined && storedVehicles) {
514-
const vehicles = []
515-
for (const v of JSON.parse(storedVehicles)) {
516-
vehicles.push(Vehicle.fromObject(v))
517-
}
518-
this.vehicles = vehicles
519-
} else if (this.vehicles === undefined || !this.vehicles.length) {
520-
this.vehicles = defaultVehicles
496+
this.loadVehicles()
497+
this.loadJobs()
498+
this.optimizeJobs()
499+
}
500+
},
501+
502+
/**
503+
* Load data of vehicles
504+
* prioritizing url data over storage data
505+
*/
506+
loadVehicles() {
507+
const defaultVehicles = this.vehicles
508+
const urlVehicles = this.$store.getters.appRouteData.options.vehicles
509+
let storedVehicles = localStorage.getItem('vehicles')
510+
// prioritise data from url, then data from local storage
511+
if (urlVehicles) {
512+
const vehicles = []
513+
for (let v of urlVehicles) {
514+
vehicles.push(Vehicle.fromObject(v))
521515
}
522-
const jobs = []
523-
if (places.length > 0) {
524-
for (const [i, place] of places.entries()) {
525-
jobs.push(new Job(place.lng, place.lat, place.placeName, jobProps[i]))
526-
}
527-
} else if (this.jobs === undefined && storedJobs) {
528-
for (const job of JSON.parse(storedJobs)) {
529-
jobs.push(Job.fromObject(job))
530-
}
516+
this.vehicles = vehicles
517+
} else if (this.vehicles === undefined && storedVehicles) {
518+
const vehicles = []
519+
for (const v of JSON.parse(storedVehicles)) {
520+
vehicles.push(Vehicle.fromObject(v))
531521
}
532-
this.jobs = jobs
533-
if (!this.jobs.length) {
534-
this.jobs = defaultJobs
522+
this.vehicles = vehicles
523+
} else if (this.vehicles === undefined || !this.vehicles.length) {
524+
this.vehicles = defaultVehicles
525+
}
526+
},
527+
528+
/**
529+
* Load data of jobs
530+
* prioritizing url data over storage data
531+
*/
532+
loadJobs() {
533+
// Empty the array and populate it with the
534+
// places from the appRoute without changing the
535+
// object reference because it is a prop
536+
const defaultJobs = this.jobs
537+
const jobProps = this.parseProps(this.$store.getters.appRouteData.options.jobProps)
538+
const places = this.$store.getters.appRouteData.places
539+
let storedJobs = localStorage.getItem('jobs')
540+
const jobs = []
541+
if (places.length > 0) {
542+
for (const [i, place] of places.entries()) {
543+
jobs.push(new Job(place.lng, place.lat, place.placeName, jobProps[i]))
535544
}
536-
this.optimizeJobs()
545+
} else if (this.jobs === undefined && storedJobs) {
546+
for (const job of JSON.parse(storedJobs)) {
547+
jobs.push(Job.fromObject(job))
548+
}
549+
}
550+
this.jobs = jobs
551+
if (!this.jobs.length) {
552+
this.jobs = defaultJobs
537553
}
538554
},
539555
// when jobs are changed update jobs and generate new route

0 commit comments

Comments
 (0)