diff --git a/src/store/index.js b/src/store/index.js index 91d82140..285d70c9 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -3,6 +3,7 @@ import { endpoints } from "@/constants/endpoints.js"; import { getDateForSavedSessionTimeout } from "@/helpers/session-helper"; import globalMethods from "@/global-methods"; import { applicationConfig } from "@/constants/application-config"; +import { issPageValues } from "@/router/router-constants/issPage-values"; const storeId = 'main'; @@ -220,6 +221,40 @@ export const useMainStore = defineStore({ }); }, + saveVehicleDamage(isWindshieldRepair, selectedGlassToReplace, selectedWindshieldChipCount) { + const selectedGlassPassedInSorted = selectedGlassToReplace.slice().sort(); + const isGlassToReplaceTheSame = + this.order.damage.glassToReplace?.length === selectedGlassToReplace.length && + this.order.damage.glassToReplace + .slice() + .sort() + .every( + (obj, index) => + obj.glassLocation === selectedGlassPassedInSorted[index].glassLocation && + obj.glassName === selectedGlassPassedInSorted[index].glassName + ); + const isWindshieldRepairTheSame = + isWindshieldRepair === this.order.damage.isRepair; + + const isChipCountTheSame = + selectedWindshieldChipCount === this.order.damage.numberOfChips; + + const isDamageChanging = + !isGlassToReplaceTheSame || + !isWindshieldRepairTheSame || + (isWindshieldRepair && !isChipCountTheSame); + + if (isDamageChanging) { + //Reset dependent state when changing + this.resetGlassPartsState(); + + // Save new values + this.updateIsRepair(isWindshieldRepair); + this.updateNumberOfChips(isWindshieldRepair ? parseInt(selectedWindshieldChipCount) : null); + this.updateGlassToReplace(selectedGlassToReplace); + } + }, + updateVehicle(vehicle) { this.order.vehicle.carId = vehicle.carId; this.order.vehicle.category = vehicle.category; @@ -242,6 +277,17 @@ export const useMainStore = defineStore({ this.order.vehicle.imageColor = null; }, + resetGlassPartsState() { + this.order.lineItems.glassParts = null; + this.order.damage.partQuestionAnswers = null; + this.order.damage.moldingQuestionAnswers = null; + this.order.damage.capabilityQuestionAnswers = null; + this.applicationUser.pageData[issPageValues.PART_QUESTIONS] = null; + this.applicationUser.pageData[issPageValues.VEHICLE_PARTS] = null; + this.applicationUser.pageData[issPageValues.MOLDING_QUESTIONS] = null; + this.applicationUser.pageData[issPageValues.CAPABILITY_QUESTIONS] = null; + }, + updateVehicleYear(year) { if(this.order.vehicle.year != year) { @@ -275,6 +321,7 @@ export const useMainStore = defineStore({ this.order.vehicle.model = model; } }, + updateVehicleStyle(style) { if(this.order.vehicle.style != style) @@ -291,6 +338,18 @@ export const useMainStore = defineStore({ this.order.vehicle.style = style; } }, + + updateIsRepair(isRepair) { + this.order.damage.isRepair = isRepair; + }, + + updateNumberOfChips(numberOfChips) { + this.order.damage.numberOfChips = numberOfChips; + }, + + updateGlassToReplace(glassToReplace) { + this.order.damage.glassToReplace = glassToReplace; + }, addEventToBus (event) { this.applicationUser.eventBus.push(event);