Skip to content

Commit d2b31c3

Browse files
committed
refactor: decrease complexity
1 parent 27add0f commit d2b31c3

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,26 +191,10 @@ export default {
191191
let id = data.placeInputId
192192
// pickEditSource indicates which property the place should fill
193193
if (data.pickEditSource === 'jobs') {
194-
if (id > context.jobs.length) {
195-
let job = Job.fromPlace(data.place)
196-
job.setId(id)
197-
context.jobs.push(job)
198-
} else {
199-
context.jobs[data.pickPlaceIndex].location = data.place.coordinates
200-
}
194+
context.setJobLocation(id, data)
201195
context.manageJobs(id)
202196
} else if (data.pickEditSource === 'vehicleStart') {
203-
if (id > context.vehicles.length) {
204-
let v = Vehicle.fromPlace(data.place)
205-
v.setId(id)
206-
context.vehicles.push(v)
207-
} else {
208-
const v = context.vehicles[data.pickPlaceIndex]
209-
if (v.end[0] === v.start[0] && v.end[1] === v.start[1]) {
210-
context.vehicles[data.pickPlaceIndex].end = data.place.coordinates
211-
}
212-
context.vehicles[data.pickPlaceIndex].start = data.place.coordinates
213-
}
197+
context.setVehicleStartLocation(id, data)
214198
context.manageVehicles(id)
215199
} else if (data.pickEditSource === 'vehicleEnd') {
216200
this.vehicles[data.pickPlaceIndex].end = data.place.coordinates
@@ -340,6 +324,38 @@ export default {
340324
}
341325
this.updateAppRoute()
342326
},
327+
/**
328+
* Set location of job with id from given data or create new Job
329+
* @param id
330+
* @param data
331+
*/
332+
setJobLocation(id, data){
333+
if (id > this.jobs.length) {
334+
let job = Job.fromPlace(data.place)
335+
job.setId(id)
336+
this.jobs.push(job)
337+
} else {
338+
this.jobs[data.pickPlaceIndex].location = data.place.coordinates
339+
}
340+
},
341+
/**
342+
* Set start location of vehicle with id from given data or create new Job
343+
* @param id
344+
* @param data
345+
*/
346+
setVehicleStartLocation(id, data){
347+
if (id > this.vehicles.length) {
348+
let v = Vehicle.fromPlace(data.place)
349+
v.setId(id)
350+
this.vehicles.push(v)
351+
} else {
352+
const v = this.vehicles[data.pickPlaceIndex]
353+
if (v.end[0] === v.start[0] && v.end[1] === v.start[1]) {
354+
this.vehicles[data.pickPlaceIndex].end = data.place.coordinates
355+
}
356+
this.vehicles[data.pickPlaceIndex].start = data.place.coordinates
357+
}
358+
},
343359
/**
344360
* After each change on the map search we redirect the user to the built target app route
345361
* The data will be loaded from the path and the map will be updated, keeping the

0 commit comments

Comments
 (0)