Skip to content

Commit e1ab0d7

Browse files
Seli0303blackbirdem
authored andcommitted
refactor: decrease complexity
1 parent 8d018a4 commit e1ab0d7

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
@@ -187,26 +187,10 @@ export default {
187187
let id = data.placeInputId
188188
// pickEditSource indicates which property the place should fill
189189
if (data.pickEditSource === 'jobs') {
190-
if (id > context.jobs.length) {
191-
let job = Job.fromPlace(data.place)
192-
job.setId(id)
193-
context.jobs.push(job)
194-
} else {
195-
context.jobs[data.pickPlaceIndex].location = data.place.coordinates
196-
}
190+
context.setJobLocation(id, data)
197191
context.manageJobs(id)
198192
} else if (data.pickEditSource === 'vehicleStart') {
199-
if (id > context.vehicles.length) {
200-
let v = Vehicle.fromPlace(data.place)
201-
v.setId(id)
202-
context.vehicles.push(v)
203-
} else {
204-
const v = context.vehicles[data.pickPlaceIndex]
205-
if (v.end[0] === v.start[0] && v.end[1] === v.start[1]) {
206-
context.vehicles[data.pickPlaceIndex].end = data.place.coordinates
207-
}
208-
context.vehicles[data.pickPlaceIndex].start = data.place.coordinates
209-
}
193+
context.setVehicleStartLocation(id, data)
210194
context.manageVehicles(id)
211195
} else if (data.pickEditSource === 'vehicleEnd') {
212196
this.vehicles[data.pickPlaceIndex].end = data.place.coordinates
@@ -336,6 +320,38 @@ export default {
336320
}
337321
this.updateAppRoute()
338322
},
323+
/**
324+
* Set location of job with id from given data or create new Job
325+
* @param id
326+
* @param data
327+
*/
328+
setJobLocation(id, data){
329+
if (id > this.jobs.length) {
330+
let job = Job.fromPlace(data.place)
331+
job.setId(id)
332+
this.jobs.push(job)
333+
} else {
334+
this.jobs[data.pickPlaceIndex].location = data.place.coordinates
335+
}
336+
},
337+
/**
338+
* Set start location of vehicle with id from given data or create new Job
339+
* @param id
340+
* @param data
341+
*/
342+
setVehicleStartLocation(id, data){
343+
if (id > this.vehicles.length) {
344+
let v = Vehicle.fromPlace(data.place)
345+
v.setId(id)
346+
this.vehicles.push(v)
347+
} else {
348+
const v = this.vehicles[data.pickPlaceIndex]
349+
if (v.end[0] === v.start[0] && v.end[1] === v.start[1]) {
350+
this.vehicles[data.pickPlaceIndex].end = data.place.coordinates
351+
}
352+
this.vehicles[data.pickPlaceIndex].start = data.place.coordinates
353+
}
354+
},
339355
/**
340356
* After each change on the map search we redirect the user to the built target app route
341357
* The data will be loaded from the path and the map will be updated, keeping the

0 commit comments

Comments
 (0)