DigitalConsumer.ISS/src/mixins/base-mixin.js
2024-03-25 13:05:09 -04:00

138 lines
5.2 KiB
JavaScript

import { mapStores } from 'pinia';
import { useMainStore } from '@/store';
import navigationScenarios from '@/router/router-constants/navigation-scenarios';
import vehicleCategories from '@/constants/vehicle-categories.js';
import queryStrings from '@/constants/query-strings';
import dynamicStrings from '@/constants/dynamic-strings';
import routerParams from '@/router/router-constants/router-params';
import widgetFields from '@/constants/cms-widget-fields.js';
import partTypeStrings from '@/constants/part-type-strings';
export default {
data() {
return {
cmsContentByWidget: {}
};
},
methods: {
setCmsContent(cmsContent) {
this.$root.cmsContentByWidget = cmsContent;
},
getCmsContent(widgetName, fieldName) {
return this.$root.cmsContentByWidget?.[widgetName]?.[fieldName] ? this.$root.cmsContentByWidget[widgetName][fieldName] : '';
},
getInputQuestionWidgetAnswersNullSafe(widgetName) {
const rawAnswers = this.getCmsContent(widgetName, widgetFields.INPUT_QUESTION_WIDGET.ANSWERS);
return rawAnswers || [];
},
getFooterInfoBoxHeight() {
const footerInfoBox = document.querySelector('.footer#infoBox');
return footerInfoBox ? footerInfoBox.offsetHeight : 0;
},
navigateBack(vm) {
const self = vm ?? this;
self.$router.navigateWithSpinner(this.navigationScenarios.CLICKED_BACK, self.$route);
},
savePageDataToStore(page, data) {
useMainStore().updatePageData({ page, data });
},
// TODO confirm this is not what we want
// getTierOnePackagePrice(lineItems) {
// const lineItemsToPrice = lineItems.filter((lineItem) => (
// lineItem.partType !== partTypeStrings.FRONT_WIPER
// && lineItem.partType !== partTypeStrings.REAR_WIPER
// && lineItem.partType !== partTypeStrings.RAIN_DEFENSE
// ));
// return this.getTotalPriceOfAllLineItemsAndChildParts(lineItemsToPrice, false);
// },
getTotalPriceOfAllLineItemsAndChildParts(lineItems, includeTax) {
let totalPrice = 0;
lineItems.forEach((lineItem) => {
totalPrice += this.getTotalLineItemPrice(lineItem, includeTax);
if (lineItem.childParts) {
totalPrice += this.getTotalPriceOfAllLineItemsAndChildParts(
lineItem.childParts,
includeTax
);
}
});
return totalPrice;
},
getTotalLineItemPrice(lineItem, includeTax) {
if (includeTax) {
return (
lineItem.kitPrice
+ lineItem.laborAmount
+ lineItem.sellingPrice
+ lineItem.salesTax
);
}
return lineItem.kitPrice + lineItem.laborAmount + lineItem.sellingPrice;
},
getDisplayAmountDue(lineItems) {
return this.getAmountDue(lineItems).toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
},
getAmountDue(lineItems) {
if (!useMainStore().isNoComp && !useMainStore().isITAC) {
return useMainStore().order.currentDeductible;
}
let amountDue = 0;
if (lineItems.glassParts) {
amountDue += this.getTotalPriceOfAllLineItemsAndChildParts(
lineItems.glassParts,
false
);
}
if (lineItems.supportingItems) {
amountDue += this.getTotalPriceOfAllLineItemsAndChildParts(
lineItems.supportingItems,
false
);
}
if (lineItems.vaps) {
amountDue += this.getTotalPriceOfAllLineItemsAndChildParts(lineItems.vaps, false);
}
if (lineItems.promos) {
amountDue += this.getTotalPriceOfAllLineItemsAndChildParts(lineItems.promos, false);
}
return ((amountDue * 100) / 100).toFixed(2);
},
scrollToPageTop() {
const container = document.getElementsByClassName('page-container-grouped-styles')[0];
container.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
},
scrollToPageBottom() {
const container = document.getElementsByClassName('page-container-grouped-styles')[0];
container.scrollTo({ top: container.scrollHeight, left: 0, behavior: 'smooth' });
}
},
computed: {
// store will be accessible globally as its id + 'Store'
...mapStores(useMainStore),
navigationScenarios() {
return navigationScenarios;
},
vehicleCategories() {
return vehicleCategories;
},
queryStrings() {
return queryStrings;
},
dynamicStrings() {
return dynamicStrings;
},
cssClassNameForCmsWidget() {
return `widget-name-${this.cmsWidgetName}`;
},
routerParams() {
return routerParams;
}
}
};