CSR-706 | Re-work synchronicity of saveOrder
Allow multiple saveOrders to run synchronously Allow saveOrder to be awaited before navigating to heritage Allow saveOrder to run on routine navigation if an email address is present Add saveOrderPromise member to the store
This commit is contained in:
parent
f0748c4c0b
commit
972e02a5b2
4 changed files with 40 additions and 19 deletions
|
|
@ -46,7 +46,8 @@ const storeMutations = {
|
|||
// OTHER MUTATIONS
|
||||
UPDATE_PAGE_DATA: "updatePageData",
|
||||
UPDATE_STATE_WITH_ORDER_INFORMATION: "updateStateWithOrderInformation",
|
||||
UPDATE_SERVICE_LOCATION_WITH_VEHICLE_REGISTRATION: "updateServiceLocationWithVehicleRegistration"
|
||||
UPDATE_SERVICE_LOCATION_WITH_VEHICLE_REGISTRATION: "updateServiceLocationWithVehicleRegistration",
|
||||
UPDATE_SAVE_ORDER_PROMISE: "updateSaveOrderPromise",
|
||||
};
|
||||
|
||||
export { storeMutations };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { storeActions } from "@/constants/store-actions.js";
|
||||
import { getFunnelCookie, updateOrCreateFunnelCookie, deleteFunnelCookie } from "@/helpers/heritage-integration/cookie-helper.js";
|
||||
import baseMixin from "@/mixins/base-mixin";
|
||||
import store from "@/store";
|
||||
import { storeMutations } from "@/constants/store-mutations";
|
||||
|
||||
/*
|
||||
Will call API and hydrate state with data from API if present. If there is no order present
|
||||
|
|
@ -34,18 +36,20 @@ export async function loadOrderIfPresent() {
|
|||
update the cookie.
|
||||
*/
|
||||
export async function saveOrder() {
|
||||
const savedOrderInfo = await baseMixin.methods.dispatchStoreAction(storeActions.SAVE_ORDER);
|
||||
|
||||
// Save the referral information back from the store.
|
||||
await baseMixin.methods.dispatchStoreAction(storeActions.SET_REFERRAL_INFORMATION, {
|
||||
referralNumber: savedOrderInfo.data.referralNumber.toString(),
|
||||
referralCorrelationId: savedOrderInfo.data.referralCorrelationId,
|
||||
referralDate: savedOrderInfo.data.referralDate,
|
||||
accountNumber: savedOrderInfo.data.accountNumber.toString()
|
||||
}, false);
|
||||
|
||||
// Update the cookie with the referral information when saved.
|
||||
updateOrCreateFunnelCookie();
|
||||
var saveOrderPromise;
|
||||
if (store.getters.applicationUser.saveOrderPromise) {
|
||||
// queue newest request after current saveOrderPromise resolves
|
||||
saveOrderPromise = store.getters.applicationUser.saveOrderPromise.then(() => {
|
||||
// get a new saveOrderPromise
|
||||
return saveOrderHelper();
|
||||
});
|
||||
} else {
|
||||
// create an initial saveOrderPromise
|
||||
saveOrderPromise = saveOrderHelper();
|
||||
}
|
||||
store.commit(storeMutations.UPDATE_SAVE_ORDER_PROMISE, saveOrderPromise);
|
||||
// await here to allow for a caller to await and make the function synchronous
|
||||
await saveOrderPromise;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -65,4 +69,17 @@ async function loadOrder(referralNumber, referralDate, referralCorrelationId, ac
|
|||
}, false);
|
||||
|
||||
return response;
|
||||
}
|
||||
async function saveOrderHelper() {
|
||||
const savedOrderInfo = await baseMixin.methods.dispatchStoreAction(storeActions.SAVE_ORDER);
|
||||
// Save the referral information back from the store.
|
||||
await baseMixin.methods.dispatchStoreAction(storeActions.SET_REFERRAL_INFORMATION, {
|
||||
referralNumber: savedOrderInfo.data.referralNumber.toString(),
|
||||
referralCorrelationId: savedOrderInfo.data.referralCorrelationId,
|
||||
referralDate: savedOrderInfo.data.referralDate,
|
||||
accountNumber: savedOrderInfo.data.accountNumber.toString()
|
||||
}, false);
|
||||
|
||||
// Update the cookie with the referral information when saved.
|
||||
updateOrCreateFunnelCookie();
|
||||
}
|
||||
|
|
@ -177,9 +177,9 @@ async function navigate(scenario, currentRoute, invalidateOnSave, optionalQuery
|
|||
// Append page data to the store for the NEXT page, if any. It will be an empty object if none is provided.
|
||||
baseMixin.methods.savePageDataToStore(destinationFmgPageValue, optionalPageData);
|
||||
|
||||
// if cookie and referralNumber/Date exists
|
||||
if (getFunnelCookie()?.ReferralNumber && getFunnelCookie()?.ReferralDate) {
|
||||
await saveOrder();
|
||||
// if cookie and referralNumber/Date exists OR an emailAddress has been saved
|
||||
if ((getFunnelCookie()?.ReferralNumber && getFunnelCookie()?.ReferralDate) || store.getters.customer.emailAddress) {
|
||||
saveOrder();
|
||||
}
|
||||
|
||||
router.push({
|
||||
|
|
|
|||
|
|
@ -62,7 +62,8 @@ const getDefaultState = () => {
|
|||
applicationUser: {
|
||||
eventBus: [],
|
||||
pageData: {},
|
||||
savedSessionTimeout: getDateForSavedSessionTimeout()
|
||||
savedSessionTimeout: getDateForSavedSessionTimeout(),
|
||||
saveOrderPromise: null,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
|
@ -166,7 +167,6 @@ export const mutations = {
|
|||
state.order.customer.emailAddress = customerEmailAddress;
|
||||
},
|
||||
|
||||
|
||||
// EVENT BUS MUTATIONS
|
||||
addEventToBus(state, event) {
|
||||
state.applicationUser.eventBus.push(event);
|
||||
|
|
@ -269,7 +269,10 @@ export const mutations = {
|
|||
state.order.serviceLocation.city = state.order.vehicle.registration.city;
|
||||
state.order.serviceLocation.state = state.order.vehicle.registration.state;
|
||||
state.order.serviceLocation.zipCode = state.order.vehicle.registration.zipCode;
|
||||
}
|
||||
},
|
||||
updateSaveOrderPromise(state, saveOrderPromise){
|
||||
state.applicationUser.saveOrderPromise = saveOrderPromise;
|
||||
},
|
||||
}
|
||||
|
||||
// Export Getters
|
||||
|
|
|
|||
Loading…
Reference in a new issue