Event bus integration

This commit is contained in:
FrankRua 2022-01-18 16:30:44 -05:00
parent 3ab5a97a4c
commit dedf3d0bd3
4 changed files with 72 additions and 63 deletions

View file

@ -1,55 +1,64 @@
<template>
<div
class="funnel-header d-flex justify-content-center align-items-center"
v-if="imageSrc"
>
<div class="funnel-header d-flex justify-content-center align-items-center" v-if="imageSrc">
<img class="logo-image img-fluid" :src="imageSrc" alt="Safelite logo" />
<alert
v-if="displayGlobalAlert"
:alertClass="globalAlertMessage.type"
:alertHeadline="globalAlertMessage.messageCopy"
:alertCopy="globalAlertMessage.messageHeadline"
v-bind:isDismissible="globalAlertMessage.isDismissible"
/>
</div>
<alert v-if="displayGlobalAlert" :alertClass="globalAlertMessage.type" :alertHeadline="globalAlertMessage.messageCopy" :alertCopy="globalAlertMessage.messageHeadline" v-bind:isDismissible="globalAlertMessage.isDismissible" />
</div>
</template>
<script>
import alert from "@/ux-components/alert/alert";
import eventBus from "@/helpers/event-bus/event-bus";
import {globalEvents, globalEventTypes} from '@/constants/events'
import {
globalEvents,
globalEventTypes
} from "@/constants/events";
export default {
name: "funnel-header",
data() {
return {
imageSrc: "",
displayGlobalAlert: false,
globalAlertMessage: {
isDismissible: false,
messageCopy: "",
messageHeadline: "",
type: "",
},
};
},
methods: {
initializeComponent(cmsContent) {
this.imageSrc = cmsContent.LogoImage;
name: "funnel-header",
data() {
return {
imageSrc: "",
displayGlobalAlert: false,
globalAlertMessage: {
isDismissible: false,
messageCopy: "",
messageHeadline: "",
type: "",
},
};
},
methods: {
initializeComponent(cmsContent) {
this.imageSrc = cmsContent.LogoImage;
},
},
components: {
alert,
},
mounted() {
// Check if alert event is on the bus
const alertEvent = eventBus.readAndPopEventFromBus(
globalEvents.Categories.GLOBAL_ALERT,
globalEvents.SubCategories.PAGE_NOT_FOUND
);
// If alert event is on the bus, then display the alert
if (alertEvent !== undefined) {
this.displayGlobalAlert = true;
this.globalAlertMessage = alertEvent;
}
},
},
components: {
alert,
},
};
</script>
<style lang="scss" scoped>
.funnel-header {
height: 56px;
height: 56px;
}
.logo-image {
max-width: 78px;
max-width: 78px;
}
</style>

View file

@ -11,6 +11,9 @@ const storeActions = {
GET_EVOX_IMAGE: "getEvoxImage",
LOOKUP_VEHICLE_BY_YMMS: "lookupVehicleByYmms",
LOOKUP_VEHICLE_BY_VIN: "lookupVehicleByVin",
// EVENT BUS
ADD_EVENT_TO_BUS: "addEventToBus",
REMOVE_EVENT_FROM_BUS: "removeEventFromBus",
};
export { storeActions };

View file

@ -1,42 +1,25 @@
import store from "@/store";
import { storeActions } from "@/constants/store-actions.js";
export default {
// Adds event to the bus given its category, subcategory, and eventValue;
addEventToBus(category, subCategory, eventValue) {
store.state.applicationUser.eventBus.push({
category: category,
subCategory: subCategory,
eventValue: eventValue,
});
store.commit(storeActions.ADD_EVENT_TO_BUS, { category: category, subCategory: subCategory, eventValue: eventValue });
},
// Finds event on the bus, removes the item, and returns its value to the caller.
readAndPopEventFromBus(category, subCategory) {
// Match our item and find the index
const matchedEvent = store.state.applicationUser.eventBus.find(({ category, subCategory }) => category === category && subCategory === subCategory);
const event = store.getters.eventBusItem(category, subCategory);
const itemIndex = store.state.applicationUser.eventBus.indexOf(matchedEvent);
store.commit(storeActions.REMOVE_EVENT_FROM_BUS, { category: category, subCategory: subCategory });
// If the item exists, remove it.
if (itemIndex > -1) {
store.state.applicationUser.eventBus.splice(itemIndex, 1);
}
return matchedEvent === undefined ? undefined : matchedEvent.eventValue;
return event;
},
// Finds the event on the bus and returns its value to the caller, does not remove it.
readEventFromBus(category, subCategory) {
const matchedEvent = store.state.applicationUser.eventBus.find(({ category, subCategory }) => category === category && subCategory === subCategory);
return matchedEvent === undefined ? undefined : matchedEvent.eventValue;
const event = store.getters.eventBusItem(category, subCategory);
return event;
},
// Checks if an item is on the bus or not, useful when you don't care about the value.
isItemOnBus(category, subCategory) {
const matchedEvent = store.state.applicationUser.eventBus.find(({ category, subCategory }) => category === category && subCategory === subCategory);
return matchedEvent !== undefined;
}
}

View file

@ -76,13 +76,27 @@ export default createStore({
state.order.vehicle.carId = data.carId;
state.order.vehicle.category = data.category;
},
updateLastPage(state, page) {
state.applicationUser.navigation.lastPage = page;
addEventToBus(state, event) {
state.applicationUser.eventBus.push(event);
},
removeEventFromBus(state, eventData) {
const matchedEvent = state.applicationUser.eventBus.find(({ category, subCategory }) => category === eventData.category && subCategory === eventData.subCategory);
const itemIndex = state.applicationUser.eventBus.indexOf(matchedEvent);
// If the item exists, remove it.
if (itemIndex > -1) {
state.applicationUser.eventBus.splice(itemIndex, 1);
}
}
},
getters: {
vehicle: state => state.order.vehicle,
eventBus: state => state.applicationUser.eventBus,
eventBusItem: (state) => (eventCategory, eventSubCategory) => {
const matchedEvent = state.applicationUser.eventBus.find(({ category, subCategory }) => category === eventCategory && subCategory === eventSubCategory);
return matchedEvent !== undefined ? matchedEvent.eventValue : undefined;
},
eventBus: state => state.applicationUser.eventBus
},
actions: {
// Vehicle API Actions