Skip to content

Commit

Permalink
Merge pull request #36 from collector-bank/bug/flickering-iframe
Browse files Browse the repository at this point in the history
resume and suspend less times
  • Loading branch information
tobiassundgren-collector authored Mar 30, 2022
2 parents 227a5d6 + 4f1c689 commit e160866
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
20 changes: 12 additions & 8 deletions src/view/frontend/web/js/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ define([
localStorage.remove('checkout-data');
localStorage.remove('cart');

self.setCheckoutData();
// Why do we need this? Can it be removed?
// self.setCheckoutData();
this.cartData = customerData.get('cart');

// Reload page if we cannot use collector checkout
Expand All @@ -54,12 +55,10 @@ define([
});

$(document).on('ajax:updateCartItemQty', function() {
collectorIframe.suspend();
self.fetchShippingRates();
});

$(document).on('ajax:removeFromCart', function() {
collectorIframe.suspend();
self.fetchShippingRates();
});

Expand All @@ -85,7 +84,14 @@ define([
such as a changed email, mobile phone number or delivery address.
This event is also fired the first time the customer is identified.
*/
this.addressUpdated(event);
console.log("customer updated");
// TODO - This should be possible to be nicer!
if(typeof this.FirstCustomerUpdateHasBeenTriggered === 'undefined')
{
this.FirstCustomerUpdateHasBeenTriggered = true;
}else{
this.addressUpdated(event);
}
break;

case 'collectorCheckoutShippingUpdated':
Expand Down Expand Up @@ -390,7 +396,7 @@ define([

/** @inheritdoc */
beforeSend: function () {
collectorIframe.suspend();
collectorIframe.suspend();
},

/** @inheritdoc */
Expand All @@ -409,14 +415,12 @@ define([
alert({
content: msg
});
collectorIframe.resume();
}
}
})
.fail(function (error) {
console.log(JSON.stringify(error));
})
.always(function () {
collectorIframe.resume();
});
},
});
Expand Down
23 changes: 9 additions & 14 deletions src/view/frontend/web/js/iframe.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
define([
], function () {
'use strict';


// Make sure we dont do any resumes before user done a action that sends a suspend
var suspendActionHaveBeenTriggered = false;

function suspend() {
if (typeof window.collector.suspendCount == 'undefined') {
window.collector.suspendCount = 0;
}
window.collector.suspendCount++;
if (window.collector.suspendCount == 1) {
window.collector.checkout.api.suspend();
}
console.log("suspended");
suspendActionHaveBeenTriggered = true;
};
function resume() {
if (typeof window.collector.suspendCount == 'undefined') {
window.collector.suspendCount = 1;
}
window.collector.suspendCount--;
if (window.collector.suspendCount <= 0) {
if (suspendActionHaveBeenTriggered === true) {
window.collector.checkout.api.resume();
window.collector.suspendCount = 0;
}
console.log("resumed");
}
};

return {
Expand Down
4 changes: 3 additions & 1 deletion src/view/frontend/web/js/view/cart/shipping-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ define([
'ko',
'underscore',
'uiComponent',
'Webbhuset_CollectorCheckout/js/iframe',
'Magento_Checkout/js/model/shipping-service',
'Magento_Catalog/js/price-utils',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/action/select-shipping-method',
'Magento_Checkout/js/action/set-shipping-information',
'Magento_Checkout/js/checkout-data'
], function (ko, _, Component, shippingService, priceUtils, quote, selectShippingMethodAction, setShippingInformationAction, checkoutData) {
], function (ko, _, Component, collectorIframe, shippingService, priceUtils, quote, selectShippingMethodAction, setShippingInformationAction, checkoutData) {
'use strict';

return Component.extend({
Expand Down Expand Up @@ -76,6 +77,7 @@ define([
* @returns bool
*/
selectShippingMethod: function (methodData) {
collectorIframe.suspend();
selectShippingMethodAction(methodData);
checkoutData.setSelectedShippingRate(methodData['carrier_code'] + '_' + methodData['method_code']);

Expand Down

0 comments on commit e160866

Please sign in to comment.