CSR-1452 | Refactor and reformat

This commit is contained in:
Scott Kiener 2023-11-09 15:17:47 -05:00
parent 7eea3baa13
commit f72ce16936
3 changed files with 114 additions and 69 deletions

View file

@ -17,7 +17,7 @@ export const addableVapsPromoPartNumbers = [
]; ];
const promoErrorCodes = { const promoErrorCodes = {
CANNOT_COMBINE: "SimilarPromoAlreadyOnOrder" CANNOT_COMBINE: "SimilarPromoAlreadyOnOrder",
}; };
export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"]; export const pagesToStripPromoQueryStringFrom = ["quote", "payment-method"];
@ -114,70 +114,54 @@ export async function revalidatePromosAndValidateNewPromo(
return { validatePromoResponse, revalidatePromoResponse }; return { validatePromoResponse, revalidatePromoResponse };
} }
export function buildToastMessagesFromRevalidateOrValidatePromoResponse(promoResponse, oldActivePromos = [], oldInactivePromos = []) { export function buildToastMessagesFromRevalidateOrValidatePromoResponse(
promoResponse,
oldActivePromos = [],
oldInactivePromos = []
) {
const alerts = []; const alerts = [];
// Revalidate always has an "errors" array // Revalidate always has an "errors" array
if (promoResponse.errors) { if (promoResponse.errors) {
const oldPromoCodes = oldActivePromos.map(promo => getPromoCodeWithoutBundleIdentifier(promo.promoCode)); const oldPromoCodes = oldActivePromos.map((promo) =>
const activePromoCodesFromResponse = []; getPromoCodeWithoutBundleIdentifier(promo.promoCode)
// consolidate bundle promos to avoid displaying multiple toast messages for one bundle );
promoResponse.promoLineItems.forEach(newPromo => { const activePromoCodesFromResponse = getPromoCodesFromPromoObjectsWithoutDuplicates(
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(newPromo.promoCode); promoResponse.promoLineItems
if (!activePromoCodesFromResponse.includes(promoCodeToDisplay)) { );
activePromoCodesFromResponse.push(promoCodeToDisplay); const newlyActivatedPromoCodes = activePromoCodesFromResponse.filter(
} (newPromoCode) => !oldPromoCodes.includes(newPromoCode)
}); );
const inactivePromoCodesFromResponse = [];
promoResponse.errors.forEach(newInactive => { newlyActivatedPromoCodes.forEach((newlyActivatedPromoCode) => {
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(newInactive.promoCode); alerts.push(createPromoSuccessAlert(newlyActivatedPromoCode));
if (!inactivePromoCodesFromResponse.includes(promoCodeToDisplay)) {
inactivePromoCodesFromResponse.push(promoCodeToDisplay);
}
});
const newlyActivatedPromoCodes = activePromoCodesFromResponse.filter(newPromoCode => !oldPromoCodes.includes(newPromoCode));
newlyActivatedPromoCodes.forEach(newlyActivatedPromoCode => {
alerts.push({
messageHeadline: "Promo applied!",
messageCopy: `Promo "${newlyActivatedPromoCode}" was successfully applied to your cart.`,
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
});
}); });
const newlyInactivatedPromos = inactivePromoCodesFromResponse.filter(errorPromoCode => !oldInactivePromos.includes(errorPromoCode)); const inactivePromoCodesFromResponse = getPromoCodesFromPromoObjectsWithoutDuplicates(
newlyInactivatedPromos.forEach(newlyInactivatedPromo => { promoResponse.errors
alerts.push({ );
messageHeadline: "Promo error", const newlyInactivatedPromos = inactivePromoCodesFromResponse.filter(
messageCopy: `Sorry, promo code "${newlyInactivatedPromo}" is not valid`, (errorPromoCode) => !oldInactivePromos.includes(errorPromoCode)
type: "alert-danger", );
isDismissible: true,
shouldAutoFade: false, newlyInactivatedPromos.forEach((newlyInactivatedPromo) => {
}); alerts.push(createPromoErrorAlert(newlyInactivatedPromo));
}); });
} else { }
// validate response
else {
if (promoResponse.orderPromos) { if (promoResponse.orderPromos) {
let promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(promoResponse.orderPromos[0].promoCode); const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(
alerts.push({ promoResponse.orderPromos[0].promoCode
messageHeadline: "Promo applied!", );
messageCopy: `Promo "${promoCodeToDisplay}" was successfully applied to your cart.`, alerts.push(createPromoSuccessAlert(promoCodeToDisplay));
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
});
} else { } else {
const alertToPush = { alerts.push(
messageHeadline: "Promo error", createPromoErrorAlert(
type: "alert-danger", promoResponse.promoCode,
shouldAutoFade: false, promoResponse.errorCode,
isDismissible: true, promoResponse.additionalInfo
} )
if (promoResponse.errorCode == promoErrorCodes.CANNOT_COMBINE) { );
alertToPush.messageCopy = `Sorry, promo code "${promoResponse.promoCode}" cannot be combined with ${promoResponse.additionalInfo[0]}`;
} else {
alertToPush.messageCopy = `Sorry, promo code "${promoResponse.promoCode}" is not valid`;
}
alerts.push(alertToPush);
} }
} }
return alerts; return alerts;
@ -241,3 +225,42 @@ function findLineItemsWithPartType(typeToFind, itemsToSearch) {
); );
return partTypeMatches; return partTypeMatches;
} }
// returns a list of promo codes (string) only without duplicates or bundle identifiers
// <promos> parameter must be an array of objects with a 'promoCode' field
function getPromoCodesFromPromoObjectsWithoutDuplicates(promos) {
const promoCodes = [];
promos.forEach((promo) => {
const promoCodeToDisplay = getPromoCodeWithoutBundleIdentifier(promo.promoCode);
if (!promoCodes.includes(promoCodeToDisplay)) {
promoCodes.push(promoCodeToDisplay);
}
});
return promoCodes;
}
function createPromoSuccessAlert(promoCode) {
return {
messageHeadline: "Promo applied!",
messageCopy: `Promo "${promoCode}" was successfully applied to your cart.`,
type: "alert-success",
isDismissible: true,
shouldAutoFade: true,
};
}
function createPromoErrorAlert(promoCode, errorCode = null, additionalInfo) {
const alert = {
messageHeadline: "Promo error",
type: "alert-danger",
shouldAutoFade: false,
isDismissible: true,
};
if (errorCode == promoErrorCodes.CANNOT_COMBINE) {
alert.messageCopy = `Sorry, promo code "${promoCode}" cannot be combined with ${additionalInfo[0]}`;
} else {
alert.messageCopy = `Sorry, promo code "${promoCode}" is not valid`;
}
return alert;
}

View file

@ -214,15 +214,23 @@ export default {
vm.updateFooterButtonText(vm.customCtaCopy); vm.updateFooterButtonText(vm.customCtaCopy);
if (revalidatePromoResponse) { if (revalidatePromoResponse) {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, oldActivePromos, oldInactivePromos); const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidatePromoResponse,
oldActivePromos,
oldInactivePromos
);
revalidateAlerts.forEach(alert => { revalidateAlerts.forEach((alert) => {
vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade); vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
}); });
} }
if (validatePromoResponse) { if (validatePromoResponse) {
const validateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse); const validateAlerts =
vm.$refs.funnelHeader.pushGlobalAlert(validateAlerts[0], validateAlerts[0].shouldAutoFade); buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(
validateAlerts[0],
validateAlerts[0].shouldAutoFade
);
} }
}); });
}, },
@ -317,8 +325,12 @@ export default {
"payment-method", "payment-method",
false false
); );
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, this.lineItems.promos, this.inactivePromos); const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidateAlerts.forEach(alert => { revalidatePromoResponse,
this.lineItems.promos,
this.inactivePromos
);
revalidateAlerts.forEach((alert) => {
this.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade); this.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
}); });

View file

@ -80,8 +80,10 @@ import { Form, defineRule } from "vee-validate";
import { navigateToHeritageFunnel } from "@/helpers/heritage-integration/navigation-helper"; import { navigateToHeritageFunnel } from "@/helpers/heritage-integration/navigation-helper";
import { applicationConfig } from "@/constants/application-config"; import { applicationConfig } from "@/constants/application-config";
import { payWithInsuranceStates } from "@/constants/pay-with-insurance-states"; import { payWithInsuranceStates } from "@/constants/pay-with-insurance-states";
import { revalidatePromosAndValidateNewPromo, import {
buildToastMessagesFromRevalidateOrValidatePromoResponse } from "@/helpers/promotions-helper"; revalidatePromosAndValidateNewPromo,
buildToastMessagesFromRevalidateOrValidatePromoResponse,
} from "@/helpers/promotions-helper";
import { queryStrings } from "@/constants/query-strings"; import { queryStrings } from "@/constants/query-strings";
import { getQuerystringParameter } from "@/helpers/querystring-helper"; import { getQuerystringParameter } from "@/helpers/querystring-helper";
@ -176,14 +178,22 @@ export default {
vm.newValidatedPromos = validatePromoResponse?.orderPromos; vm.newValidatedPromos = validatePromoResponse?.orderPromos;
if (revalidatePromoResponse) { if (revalidatePromoResponse) {
const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(revalidatePromoResponse, oldActivePromos, oldInactivePromos); const revalidateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(
revalidateAlerts.forEach(alert => { revalidatePromoResponse,
oldActivePromos,
oldInactivePromos
);
revalidateAlerts.forEach((alert) => {
vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade); vm.$refs.funnelHeader.pushGlobalAlert(alert, alert.shouldAutoFade);
}); });
} }
if (validatePromoResponse) { if (validatePromoResponse) {
const validateAlerts = buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse); const validateAlerts =
vm.$refs.funnelHeader.pushGlobalAlert(validateAlerts[0], validateAlerts[0].shouldAutoFade); buildToastMessagesFromRevalidateOrValidatePromoResponse(validatePromoResponse);
vm.$refs.funnelHeader.pushGlobalAlert(
validateAlerts[0],
validateAlerts[0].shouldAutoFade
);
} }
}); });
}, },