Skip to content

Commit

Permalink
fix: use event emitter for dynamic events out of contentful content, …
Browse files Browse the repository at this point in the history
…fix basket hash
  • Loading branch information
mcstover committed Oct 5, 2024
1 parent fc4f2e4 commit cd249f6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/components/Contentful/ButtonWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<script>
import KvButton from '@kiva/kv-components/vue/KvButton';
import emitter from '#src/plugins/event-emitter';
/**
* Contentful Button Wrapper
Expand Down Expand Up @@ -57,8 +58,8 @@ export default {
if (customEventName) {
// Current behavior is to replace a button navigation if a custom event name is passed
event.stopPropagation();
// Emit root level event that any component can listen for
this.$root.$emit(customEventName);
// Emit event that any component can listen for using the event-emitter plugin
emitter.emit(customEventName, { eventName: customEventName, event });
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/WwwFrame/TheBasketBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default {
},
computed: {
basketLink() {
return this.corporate ? this.addHashToRoute('show-basket') : '/basket';
return this.corporate ? this.addHashToRoute('#show-basket') : '/basket';
},
hideBasketBar() {
// hide this banner on managed lending landing + checkout pages
Expand Down
24 changes: 18 additions & 6 deletions src/pages/LandingPages/CorporateCampaign/CCLandingPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ import LoanSearchFilters, { getSearchableFilters } from '#src/api/fixtures/LoanS
import syncDate from '#src/util/syncDate';
import trackTransactionEvent from '#src/util/trackTransactionEvent';
import checkoutUtils from '#src/plugins/checkout-utils-mixin';
import emitter from '#src/plugins/event-emitter';
import updateLoanReservationTeam from '#src/graphql/mutation/updateLoanReservationTeam.graphql';
import CampaignHowKivaWorks from '#src/components/CorporateCampaign/CampaignHowKivaWorks';
import CampaignJoinTeamForm from '#src/components/CorporateCampaign/CampaignJoinTeamForm';
Expand Down Expand Up @@ -709,6 +710,11 @@ export default {
// show a loading screen if the page loads with an loan in the basket.
// const basketItems = this.rawPageData?.shop?.basket?.items?.values ?? [];
this.loadingPage = basketItems.some(item => item.__typename === 'LoanReservation'); // eslint-disable-line no-underscore-dangle, max-len

// setup emitter watcher
emitter.on('jumpToLoans', () => {
this.jumpToLoans();
});
},
async mounted() {
this.isClientReady = typeof window !== 'undefined';
Expand Down Expand Up @@ -1502,16 +1508,23 @@ export default {
this.basketBalancing = false;
}
},
checkoutLightboxClosed() {
async checkoutLightboxClosed() {
this.checkoutVisible = false;
// gaurd against navigation reset if hash is already empty
if (this.$router?.hash !== '') {
await this.$router.push(this.adjustRouteHash('')).catch(() => {});
}
this.handleScrollPosition();
this.$router.push(this.adjustRouteHash('')).catch(() => {});
},
getLoanSectionRef() {
const refs = this.$refs;
return refs?.mlLoanDisplay?.$refs || refs?.mlLoanDisplay?.[0]?.$refs;
},
handleScrollPosition(y) {
if (this.scrollToLoans) {
this.scrollToLoans = false;
this.loanDisplayComponent.campaignLoanSection.scrollIntoView({ behavior: 'smooth' });
} else if (y) {
// Fetch Current Refs using method above and Navigate using the result
this.getLoanSectionRef()?.campaignLoanSection?.scrollIntoView({ behavior: 'smooth' });
} else if (typeof y !== 'undefined') {
window.scrollTo(0, y);
}
},
Expand Down Expand Up @@ -1637,7 +1650,6 @@ export default {
jumpToLoans() {
this.scrollToLoans = true;
this.checkoutLightboxClosed();
this.handleScrollPosition();
},
adjustRouteHash(hash) {
const route = { ...this.$route };
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/event-emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mitt from 'mitt';

// eslint-disable-next-line new-cap
const emitter = new mitt();

export default emitter;

0 comments on commit cd249f6

Please sign in to comment.