EmicoEcommerce / Magento2Tweakwise-archived

Magento 2 module for Tweakwise integration
Other
9 stars 25 forks source link

_bindPopChangeHandler event.state is null on initial load of category page #233

Closed Thokoop closed 2 years ago

Thokoop commented 2 years ago

Issue Brief

Sometimes when using the browser back button, the product grid in the category doesn't get updated.

After debugging we noticed on initial load of a category, there is no ajax call being done yet, therefore the event.state is not yet updated through the 'this._updateState(response);' function in navigation-form.js:198. So when going back using the browser button event.state is empty in '_bindPopChangeHandler: function(){' causing the content not to be updated.

A possible workaround/fix we found is adding this to the _hookEvents function within the if (this.options.ajaxFilters) { in navigation-form.js:30

if (this.options.ajaxFilters) {
    if (!$('.filter-actions').length) {
        $.ajax({
            url: this.options.ajaxEndpoint,
            data: this._getFilterParameters(),
            cache: true,
            success: function (response) {
                this._updateState(response);
            }.bind(this),
            error: function (jqXHR, errorStatus) {
                if (errorStatus !== 'abort') {
                    // Something went wrong, try to navigate to the selected filter
                    this._defaultHandler(event);
                }
            }.bind(this)
        });
    }

    this._bindPopChangeHandler();
}

The if (!$('.filter-actions').length) { statement is just a test, it's probably better to set a flag to determine if event.state has been set at least once. So the idea is that this._updateState(response); should be triggered at least once to make sure there is always a state to go back to. Hopefully it will give at least some idea on how/where to fix it.

Environment

Steps to reproduce

  1. Go to a category (having ajax enabled)
  2. Apply just one filter, do not trigger another ajax call (navigation or an additional filter)
  3. Go back using the browser back button

Actual result

  1. Product grid in the category is not updated with the 'original' state, instead it still shows the 'filtered' result

Expected result