From dedf3d0bd3afb03b1c08e681428d1818e2b53b72 Mon Sep 17 00:00:00 2001 From: FrankRua Date: Tue, 18 Jan 2022 16:30:44 -0500 Subject: [PATCH] Event bus integration --- .../funnel-header/funnel-header.vue | 79 +++++++++++-------- src/constants/store-actions.js | 3 + src/helpers/event-bus/event-bus.js | 33 ++------ src/store/index.js | 20 ++++- 4 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/common-components/funnel-header/funnel-header.vue b/src/common-components/funnel-header/funnel-header.vue index 8f1253201..7b46f226a 100644 --- a/src/common-components/funnel-header/funnel-header.vue +++ b/src/common-components/funnel-header/funnel-header.vue @@ -1,55 +1,64 @@ diff --git a/src/constants/store-actions.js b/src/constants/store-actions.js index ae0755963..590cf1080 100644 --- a/src/constants/store-actions.js +++ b/src/constants/store-actions.js @@ -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 }; diff --git a/src/helpers/event-bus/event-bus.js b/src/helpers/event-bus/event-bus.js index 6ca248d19..8ac156776 100644 --- a/src/helpers/event-bus/event-bus.js +++ b/src/helpers/event-bus/event-bus.js @@ -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; - } } \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js index cb2e4a3dc..818fd3bd5 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -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