From 6e0c9688d33a01d2ce63023d481d58e73639b56b Mon Sep 17 00:00:00 2001 From: Katie Date: Tue, 30 Aug 2022 13:12:18 -0400 Subject: [PATCH] CSR-111 Reset answers if they change --- src/constants/store-actions.js | 1 + src/layouts/vehicle-parts/vehicle-parts.vue | 2 + src/store/index.js | 53 ++++++++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/constants/store-actions.js b/src/constants/store-actions.js index 8d03c5b95..b77b1456f 100644 --- a/src/constants/store-actions.js +++ b/src/constants/store-actions.js @@ -58,6 +58,7 @@ const storeActions = { SAVE_REGISTRATION_ADDRESS_LOOKUP: "saveRegistrationAddressLookup", SAVE_GLASS_PARTS: "saveGlassParts", SAVE_PART_QUESTION_ANSWERS: "savePartQuestionAnswers", + RESET_MOLDING_AND_CAPABILITY_QUESTIONS_IF_NEEDED: "resetMoldingAndCapabilityQuestionAnswersIfNeeded", SAVE_MOLDING_QUESTION_ANSWERS: "saveMoldingQuestionAnswers", SAVE_CAPABILITY_QUESTION_ANSWERS: "saveCapabilityQuestionAnswers" }; diff --git a/src/layouts/vehicle-parts/vehicle-parts.vue b/src/layouts/vehicle-parts/vehicle-parts.vue index a276ef132..6f81b0003 100644 --- a/src/layouts/vehicle-parts/vehicle-parts.vue +++ b/src/layouts/vehicle-parts/vehicle-parts.vue @@ -188,6 +188,8 @@ export default { throw new Error("Could not match any parts to the selected parts"); } + await this.dispatchStoreAction(this.storeActions.RESET_MOLDING_AND_CAPABILITY_QUESTIONS_IF_NEEDED, matchedParts, false); + // Navigate to the next page this.navigateForward(matchedParts); }, diff --git a/src/store/index.js b/src/store/index.js index 76ce9c3a9..5f044c449 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1007,20 +1007,69 @@ export const actions = { !previousResultsArray.every((x, i) => x.result === partQuestionAnswersArray[i].result); if (havePartQuestionAnswersChanged) { + context.commit(storeMutations.UPDATE_GLASS_PARTS, null); context.commit(storeMutations.UPDATE_MOLDING_QUESTION_ANSWERS, null); context.commit(storeMutations.UPDATE_CAPABILITY_QUESTION_ANSWERS, null); - state.applicationUser.pageData[fmgPageValues.MOLDING_QUESTIONS] = null; - state.applicationUser.pageData[fmgPageValues.CAPABILITY_QUESTIONS] = null; + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.VEHICLE_PARTS, data: null }); + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.MOLDING_QUESTIONS, data: null }); + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.CAPABILITY_QUESTIONS, data: null }); } //Save new values context.commit(storeMutations.UPDATE_PART_QUESTION_ANSWERS, partQuestionAnswersArray); }, + resetMoldingAndCapabilityQuestionAnswersIfNeeded(context, matchedParts) { + const previousResultsArray = [{ parts: context.getters.lineItems.glassParts }]; + + function getAllPartNumbers(partsOrQuestions) { + return [...partsOrQuestions].map(glass => glass.parts).flat().map(part => part.partNumber).sort().join(","); + } + + const previouslySelectedPartNumbers = getAllPartNumbers(previousResultsArray); + const currentlySelectedPartNumbers = getAllPartNumbers(matchedParts); + + const haveSelectedVehiclePartsChanged = previouslySelectedPartNumbers !== currentlySelectedPartNumbers; + + if (haveSelectedVehiclePartsChanged) { + context.commit(storeMutations.UPDATE_GLASS_PARTS, null); + context.commit(storeMutations.UPDATE_MOLDING_QUESTION_ANSWERS, null); + context.commit(storeMutations.UPDATE_CAPABILITY_QUESTION_ANSWERS, null); + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.MOLDING_QUESTIONS, data: null }); + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.CAPABILITY_QUESTIONS, data: null }); + } + + console.log(previousResultsArray) + console.log(matchedParts) + console.log(haveSelectedVehiclePartsChanged) + }, saveMoldingQuestionAnswers(context, moldingQuestionAnswers) { + const previousResultsArray = context.getters.damage.moldingQuestionAnswers; + // TODO NOT ACCURATE!! answerData/answerQuestions keeps changing, partNum is undefined a lot + const haveMoldingQuestionAnswersChanged = previousResultsArray?.length !== moldingQuestionAnswers.length || + !previousResultsArray.every((x, i) => + x.answeredQuestions && + x.answeredQuestions.length === moldingQuestionAnswers[i].answeredQuestions?.length && + x.answeredQuestions.every((y, j) => y.selectedAnswerText === moldingQuestionAnswers[i].answeredQuestions[j].selectedAnswerText)); + + if (haveMoldingQuestionAnswersChanged) { + // TODO reset glass parts + context.commit(storeMutations.UPDATE_GLASS_PARTS, null); + context.commit(storeMutations.UPDATE_CAPABILITY_QUESTION_ANSWERS, null); + context.commit(storeMutations.UPDATE_PAGE_DATA, { page: fmgPageValues.CAPABILITY_QUESTIONS, data: null }); + } + //Save new values context.commit(storeMutations.UPDATE_MOLDING_QUESTION_ANSWERS, moldingQuestionAnswers); }, saveCapabilityQuestionAnswers(context, capabilityQuestionAnswers) { + const previousResultsArray = context.getters.damage.capabilityQuestionAnswers; + const haveCapabilityQuestionAnswersChanged = previousResultsArray?.length !== capabilityQuestionAnswers.length || + !previousResultsArray.every((x, i) => x.result === capabilityQuestionAnswers[i].result); + + if (haveCapabilityQuestionAnswersChanged) { + context.commit(storeMutations.UPDATE_GLASS_PARTS, null); + } + //Save new values context.commit(storeMutations.UPDATE_CAPABILITY_QUESTION_ANSWERS, capabilityQuestionAnswers); },