Skip to content

Commit 9dc20f6

Browse files
ENGCOM-8779: Do not send an ajax request if there no affected sections #31933
- Merge Pull Request #31933 from ihor-sviziev/magento2:patch-6 - Merged commits: 1. 24dfb62 2. 4314fd9 3. c98919b 4. d95ba9c 5. 5728c0b 6. c63f878
2 parents 840b5f8 + c63f878 commit 9dc20f6

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,31 @@ define([
358358
return deferred.promise();
359359
},
360360

361+
/**
362+
* Reload sections on ajax complete
363+
*
364+
* @param {Object} jsonResponse
365+
* @param {Object} settings
366+
*/
367+
onAjaxComplete: function (jsonResponse, settings) {
368+
var sections,
369+
redirects;
370+
371+
if (settings.type.match(/post|put|delete/i)) {
372+
sections = sectionConfig.getAffectedSections(settings.url);
373+
374+
if (sections && sections.length) {
375+
this.invalidate(sections);
376+
redirects = ['redirect', 'backUrl'];
377+
378+
if (_.isObject(jsonResponse) && !_.isEmpty(_.pick(jsonResponse, redirects))) { //eslint-disable-line
379+
return;
380+
}
381+
this.reload(sections, true);
382+
}
383+
}
384+
},
385+
361386
/**
362387
* @param {Object} settings
363388
* @constructor
@@ -376,22 +401,7 @@ define([
376401
* Events listener
377402
*/
378403
$(document).on('ajaxComplete', function (event, xhr, settings) {
379-
var sections,
380-
redirects;
381-
382-
if (settings.type.match(/post|put|delete/i)) {
383-
sections = sectionConfig.getAffectedSections(settings.url);
384-
385-
if (sections) {
386-
customerData.invalidate(sections);
387-
redirects = ['redirect', 'backUrl'];
388-
389-
if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line
390-
return;
391-
}
392-
customerData.reload(sections, true);
393-
}
394-
}
404+
customerData.onAjaxComplete(xhr.responseJSON, settings);
395405
});
396406

397407
/**

dev/tests/js/jasmine/tests/app/code/Magento/Customer/frontend/js/customer-data.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,22 @@ define([
510510
});
511511
});
512512

513+
describe('"onAjaxComplete" method', function () {
514+
it('Should not trigger reload if sections is empty', function () {
515+
var jsonResponse, settings;
516+
517+
jsonResponse = jasmine.createSpy();
518+
spyOn(sectionConfig, 'getAffectedSections').and.returnValue([]);
519+
spyOn(obj, 'reload');
520+
settings = {
521+
type: 'POST',
522+
url: 'http://test.local'
523+
};
524+
obj.onAjaxComplete(jsonResponse, settings);
525+
expect(obj.reload).not.toHaveBeenCalled();
526+
});
527+
});
528+
513529
describe('"Magento_Customer/js/customer-data" method', function () {
514530
it('Should be defined', function () {
515531
expect(obj.hasOwnProperty('Magento_Customer/js/customer-data')).toBeDefined();

0 commit comments

Comments
 (0)