diff --git a/src/layouts/vehicle-damage/vehicle-damage.vue b/src/layouts/vehicle-damage/vehicle-damage.vue index 9bfe25a6..4988285e 100644 --- a/src/layouts/vehicle-damage/vehicle-damage.vue +++ b/src/layouts/vehicle-damage/vehicle-damage.vue @@ -291,17 +291,9 @@ export default { }, async forwardButtonAction() { - await this.dispatchStoreAction( - this.storeActions.SAVE_VEHICLE_DAMAGE, - { - isWindshieldRepair: this.isWindshieldRepair, - selectedGlassToReplace: this.selectedGlassToReplace(), - selectedWindshieldChipCount: - this.selectedWindshieldOptions.selectedWindshieldChipCount, - }, - false - ); - + await this.mainStore.saveVehicleDamage(this.isWindshieldRepair, + this.selectedGlassToReplace(), + this.selectedWindshieldOptions.selectedWindshieldChipCount); return this.navigateForward(); }, diff --git a/src/store/index.js b/src/store/index.js index 696250d5..03dda415 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import { getDateForSavedSessionTimeout } from "@/helpers/session-helper"; import globalMethods from "@/global-methods"; import { experimentTriggers } from "@/constants/experiments"; import { applicationConfig } from "@/constants/application-config"; +import { issPageValues } from "@/router/router-constants/issPage-values"; const storeId = 'main'; @@ -41,7 +42,7 @@ const getDefaultState = () => { }, lineItems: { glassParts: null, - }, + }, serviceLocation: { address: null, city: null, @@ -231,6 +232,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; @@ -253,10 +288,28 @@ 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; + }, + + resetDamageState() { + this.order.damage.isRepair = null; + this.order.damage.numberOfChips = null; + this.order.damage.glassToReplace = null; + }, + updateVehicleYear(year) { if(this.order.vehicle.year != year) { this.resetVehicleState(); + this.resetDamageState(); this.order.vehicle.year = year; } }, @@ -267,6 +320,7 @@ export const useMainStore = defineStore({ const year = this.order.vehicle.year; this.resetVehicleState(); + this.resetDamageState(); this.order.vehicle.year = year; this.order.vehicle.make = make; @@ -280,12 +334,14 @@ export const useMainStore = defineStore({ const make = this.order.vehicle.make; this.resetVehicleState(); + this.resetDamageState(); this.order.vehicle.year = year; this.order.vehicle.make = make; this.order.vehicle.model = model; } }, + updateVehicleStyle(style) { if(this.order.vehicle.style != style) @@ -295,6 +351,7 @@ export const useMainStore = defineStore({ const model = this.order.vehicle.model; this.resetVehicleState(); + this.resetDamageState(); this.order.vehicle.year = year; this.order.vehicle.make = make; @@ -302,6 +359,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); diff --git a/src/store/store.spec.js b/src/store/store.spec.js index 5bf8bbad..1f74048d 100644 --- a/src/store/store.spec.js +++ b/src/store/store.spec.js @@ -140,4 +140,21 @@ describe("Store", () => { expect(globalMethods.callHttpClient).toHaveBeenCalled(); }); + it("saveVehicleDamage, should update damage", () => { + // Arrange + store.order.damage = { + glassToReplace: [{ glassName: "Single", glassLocation: "Windshield" }], + }; + + // Act + store.saveVehicleDamage( false, + [{ glassName: "Rear", glassLocation: "quarter" }], + 0); + + // Assert + expect(store.order.damage.glassToReplace).toEqual([{ glassName: "Rear", glassLocation: "quarter" }]); + expect(store.order.damage.isRepair).toEqual(false); + expect(store.order.damage.numberOfChips).toEqual(null); + }); + }); \ No newline at end of file