215 lines
No EOL
12 KiB
JavaScript
215 lines
No EOL
12 KiB
JavaScript
import { fmgPageValues } from "@/router/router-constants/fmgPage-values";
|
|
import { storeMutations } from "@/constants/store-mutations.js";
|
|
import { damageLocationsSelected } from "@/constants/damage-locations-selected";
|
|
import { navigationScenarios } from "../router/router-constants/navigation-scenarios";
|
|
import { storeActions } from "@/constants/store-actions";
|
|
import { navigateToHeritageFunnel } from "@/helpers/heritage-integration/navigation-helper";
|
|
import baseMixin from "@/mixins/base-mixin.js";
|
|
import store from "@/store";
|
|
|
|
export default {
|
|
methods: {
|
|
hasPartQuestions(partsOrQuestions) {
|
|
return partsOrQuestions?.some(pq => pq.partQuestions?.length > 0);
|
|
},
|
|
hasGlassLocationWithMultipleParts(partsOrQuestions) {
|
|
return partsOrQuestions?.some(pq => pq.parts?.length > 1);
|
|
},
|
|
hasChildPartQuestions(partsOrQuestions) {
|
|
return partsOrQuestions?.some(pq => {
|
|
return pq.parts?.some(part => part.childPartQuestions?.length > 0);
|
|
});
|
|
},
|
|
hasCapabilityQuestions(partsOrQuestions) {
|
|
return partsOrQuestions?.some(pq => {
|
|
return pq.parts?.some(part => part.requiresCapabilityQuestions === true);
|
|
});
|
|
},
|
|
|
|
// method to only include keys listed for lineItems.glassParts in
|
|
// https://safelite.atlassian.net/wiki/spaces/DC/pages/17137665/Catalog+Front-End+State#glassParts
|
|
reducedGlassPartsArray(glassParts) {
|
|
const reducedGlassParts = [];
|
|
glassParts.forEach((glass) => {
|
|
if (Array.isArray(glass.parts) && glass.parts.length === 1) {
|
|
const singlePart = glass.parts[0];
|
|
reducedGlassParts.push({
|
|
partNumber: singlePart.partNumber,
|
|
description: singlePart.description,
|
|
color: singlePart.color,
|
|
requiresRecalibration: singlePart.requiresRecalibration,
|
|
requiresCapabilityQuestions: singlePart.requiresCapabilityQuestions,
|
|
recalibrationType: singlePart.recalibrationType,
|
|
childParts: singlePart.childParts,
|
|
price: singlePart.price,
|
|
});
|
|
}
|
|
});
|
|
return reducedGlassParts;
|
|
},
|
|
comparePageIndices(currentPage, fmgPage) {
|
|
const orderedVehicleQuestionPages = [
|
|
fmgPageValues.PART_QUESTIONS,
|
|
fmgPageValues.VEHICLE_PARTS,
|
|
fmgPageValues.MOLDING_QUESTIONS,
|
|
fmgPageValues.CAPABILITY_QUESTIONS,
|
|
fmgPageValues.QUOTE
|
|
]
|
|
|
|
return orderedVehicleQuestionPages.indexOf(currentPage) - orderedVehicleQuestionPages.indexOf(fmgPage);
|
|
},
|
|
currentPageComesBeforePage(currentPage = this.$route.query.fmgPage, fmgPage) {
|
|
return this.comparePageIndices(currentPage, fmgPage) < 0;
|
|
},
|
|
currentPageComesAfterPage(currentPage = this.$route.query.fmgPage, fmgPage) {
|
|
return this.comparePageIndices(currentPage, fmgPage) > 0;
|
|
},
|
|
setupInitialData(glass, i, alreadyAnsweredQuestions, vm) {
|
|
glass.key = glass.location + "-" + glass.name;
|
|
const self = vm ?? this;
|
|
// clear answerData if no questions are already answered
|
|
if (!alreadyAnsweredQuestions) { glass.answerData = null }
|
|
|
|
alreadyAnsweredQuestions?.forEach((answeredGlass) => {
|
|
// if answeredGlass lacks any of these properties then exit
|
|
if (!answeredGlass.location ||
|
|
!answeredGlass.name ||
|
|
!answeredGlass.answeredQuestions ||
|
|
!answeredGlass.result && !answeredGlass.partNum) { return }
|
|
|
|
// test if glass parts match
|
|
if (glass.location === answeredGlass.location && glass.name === answeredGlass.name) {
|
|
let answerString = "";
|
|
|
|
// loop through answeredQuestions for matches
|
|
answeredGlass.answeredQuestions.forEach((aq) => {
|
|
if (!aq.questionNum || !aq.selectedAnswerText) { return }
|
|
// determine which answer was previously chosen
|
|
const chosenAns = glass.questions[aq.questionNum-1].answers.find((a) => {
|
|
return a.answerText.toUpperCase() === aq.selectedAnswerText.toUpperCase();
|
|
});
|
|
// set the answerString to use for answerSelected
|
|
if (chosenAns.nextQuestionSequence) {
|
|
answerString = `${aq.questionNum}|nextQuestion|${chosenAns.nextQuestionSequence}|${chosenAns.answerText}`
|
|
} else {
|
|
answerString = `${aq.questionNum}|answer|${chosenAns.answerResult}|${chosenAns.answerText}`
|
|
}
|
|
|
|
// mark this question as answered (question-chain will read this)
|
|
glass.questions[aq.questionNum-1].answerSelected = answerString;
|
|
// mark this question as suppressed if needed (question-chain uses this)
|
|
if (aq.suppressQuestion) { glass.questions[aq.questionNum-1].suppressQuestion = true }
|
|
});
|
|
|
|
// advance the currentGlassIndex
|
|
self.currentGlassIndex = i;
|
|
|
|
const answerResult = (answeredGlass.partNum) ? answeredGlass.partNum : answeredGlass.result;
|
|
// the above logic for answerResult covers all 3 ___-questions page scenarios
|
|
|
|
// add answerData to current glass
|
|
glass.answerData = {
|
|
answerResult: answerResult,
|
|
answeredQuestions: answeredGlass.answeredQuestions
|
|
}
|
|
}
|
|
|
|
});
|
|
|
|
// Set up watch for each set of glass questions
|
|
// (updated when all questions for a glass have been answered in question-chain)
|
|
self.$watch("selectedAnswers." + glass.location + '-' + glass.name, (newValue) => {
|
|
if (newValue) {
|
|
self.handleAnswerUpdates(newValue);
|
|
}
|
|
}, {deep: true})
|
|
|
|
return glass;
|
|
},
|
|
// Can't use `this` because navigateForward is also called from vin-pages-mixin
|
|
async navigateForward(partsOrQuestions, vm) {
|
|
const self = vm ?? this;
|
|
const currentPage = self.$route.query.fmgPage;
|
|
|
|
const hasPartQuestions = this.hasPartQuestions(partsOrQuestions)
|
|
const hasGlassLocationWithMultipleParts = this.hasGlassLocationWithMultipleParts(partsOrQuestions);
|
|
const hasChildPartQuestions = this.hasChildPartQuestions(partsOrQuestions);
|
|
const hasCapabilityQuestions = this.hasCapabilityQuestions(partsOrQuestions);
|
|
|
|
if (hasPartQuestions && this.currentPageComesBeforePage(currentPage, fmgPageValues.PART_QUESTIONS)) {
|
|
self.$router.navigateWithSaving(self.navigationScenarios.HAS_PART_QUESTIONS, self.$route, {}, {}, { partsOrQuestions: partsOrQuestions });
|
|
}
|
|
else if (hasGlassLocationWithMultipleParts && this.currentPageComesBeforePage(currentPage, fmgPageValues.VEHICLE_PARTS)) {
|
|
// if multiple parts on any glass
|
|
// go to vehicle-parts page and pass the partsData
|
|
self.$router.navigateWithSaving(self.navigationScenarios.HAS_MULTIPLE_PARTS_TO_CHOOSE, self.$route, {}, {}, { partsOrQuestions: partsOrQuestions });
|
|
} else if (hasChildPartQuestions && this.currentPageComesBeforePage(currentPage, fmgPageValues.MOLDING_QUESTIONS)) {
|
|
// if any childpart questions
|
|
// go to molding-questions page and pass the partsData
|
|
self.$router.navigateWithSaving(self.navigationScenarios.HAS_MOLDING_QUESTIONS, self.$route, {}, {}, { partsOrQuestions: partsOrQuestions });
|
|
} else if (hasCapabilityQuestions && this.currentPageComesBeforePage(currentPage, fmgPageValues.CAPABILITY_QUESTIONS)) {
|
|
// if has capability questions
|
|
// go to capability-questions page and pass the partsData
|
|
|
|
// mimic part-questions page data for consistency
|
|
for (let partOrQuestion of partsOrQuestions) {
|
|
if (this.hasCapabilityQuestions([partOrQuestion])) {
|
|
let capabilityQuestionsForGlassLocation = (await baseMixin.methods.dispatchStoreAction(storeActions.GET_CAPABILITY_QUESTIONS, {
|
|
carId: store.getters.vehicle.carId,
|
|
partNumber: partOrQuestion.parts[0].partNumber
|
|
})).data;
|
|
|
|
capabilityQuestionsForGlassLocation.forEach(question => {
|
|
question.answers = question.answers.map(answer => {
|
|
return {
|
|
...answer,
|
|
answerResult: answer.answerResult1
|
|
}
|
|
})
|
|
})
|
|
|
|
partOrQuestion.capabilityQuestions = capabilityQuestionsForGlassLocation;
|
|
}
|
|
}
|
|
|
|
self.$router.navigateWithSaving(self.navigationScenarios.HAS_CAPABILITY_QUESTIONS, self.$route, {}, {}, { partsOrQuestions });
|
|
} else {
|
|
// if single parts only
|
|
const collectedGlassParts = this.reducedGlassPartsArray(partsOrQuestions);
|
|
// save to store lineItems.glassParts
|
|
// TODO KO
|
|
self.$store.commit(storeMutations.UPDATE_GLASS_PARTS, collectedGlassParts);
|
|
|
|
self.$refs.loadingModal.showModal();
|
|
navigateToHeritageFunnel();
|
|
}
|
|
},
|
|
backButtonAction() {
|
|
const partsOrQuestions = (this.$store.getters.pageData(fmgPageValues.PART_QUESTIONS) ?? this.$store.getters.pageData(fmgPageValues.VEHICLE_PARTS))?.partsOrQuestions;
|
|
const hasPartQuestions = this.hasPartQuestions(partsOrQuestions);
|
|
const hasGlassLocationWithMultipleParts = this.hasGlassLocationWithMultipleParts(partsOrQuestions);
|
|
const hasChildPartQuestions = this.hasChildPartQuestions(partsOrQuestions);
|
|
const hasCapabilityQuestions = this.hasCapabilityQuestions(partsOrQuestions);
|
|
let backNavigationScenario = navigationScenarios.CLICKED_BACK_WITH_NO_QUESTIONS;
|
|
|
|
const currentPage = this.$route.query.fmgPage;
|
|
if (hasCapabilityQuestions && this.currentPageComesAfterPage(currentPage, fmgPageValues.CAPABILITY_QUESTIONS)) {
|
|
backNavigationScenario = navigationScenarios.CLICKED_BACK_WITH_CAPABILITY_QUESTIONS;
|
|
}
|
|
else if (hasChildPartQuestions && this.currentPageComesAfterPage(currentPage, fmgPageValues.MOLDING_QUESTIONS)) {
|
|
backNavigationScenario = navigationScenarios.CLICKED_BACK_WITH_MOLDING_QUESTIONS;
|
|
}
|
|
else if (hasGlassLocationWithMultipleParts && this.currentPageComesAfterPage(currentPage, fmgPageValues.VEHICLE_PARTS)) {
|
|
backNavigationScenario = navigationScenarios.CLICKED_BACK_WITH_MULTIPLE_PARTS_TO_CHOOSE;
|
|
}
|
|
else if (hasPartQuestions && this.currentPageComesAfterPage(currentPage, fmgPageValues.PART_QUESTIONS)) {
|
|
backNavigationScenario = navigationScenarios.CLICKED_BACK_WITH_PART_QUESTIONS;
|
|
}
|
|
|
|
this.$router.navigateWithoutSaving(
|
|
backNavigationScenario,
|
|
this.$route
|
|
);
|
|
}
|
|
}
|
|
} |