magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.48k stars 9.29k forks source link

Cookie Messages is read-only and can not be manipulated #37308

Closed erfanimani closed 1 year ago

erfanimani commented 1 year ago

Summary

Common customizations of Magento include creating a hide or close button for customer messages, and having it automatically disappear after a certain amount of seconds.

The right way to approach this is to modify the application state. In this case it would be necessary for developers to modify the messages array, which should cause the UI to be rerendered by Knockout's dual way data-binding. This is often done through a JS mixin that extends the messages.js UI Component.

At the moment this is not possible. The cookieMessages array is not an observable attribute and the UI does not react to any changes.

(Also see Example 1 in the official documentation: https://knockoutjs.com/documentation/foreach-binding.html)

My suggestion is convert the cookieMessages into an observable attribute, so that developers are able to manipulate the state causing a proper UI rerender.

Examples

messages-mixin.js:

At the moment, manipulating the cookieMesssages attribute does not cause the UI to be updated.

define([
    'Magento_Customer/js/customer-data'
], function (customerData) {
    'use strict';

    return function (target) {
        return target.extend({

            initialize: function () {
                this._super();

                if (this.cookieMessages && this.cookieMessages.length > 0) {
                    setTimeout((function () {
                        this.cookieMessages = [];
                    }).bind(this), 5000);
                }
            },

        });
    }
});

However, in-line with Magento philosophy, we would like the state to be reflected in the UI by using an observable instead. This is expected behaviour:

messages-mixin.js:

define([
    'Magento_Customer/js/customer-data'
], function (customerData) {
    'use strict';

    return function (target) {
        return target.extend({

            initialize: function () {
                this._super();

                if (this.cookieMessages() && this.cookieMessages().length > 0) {
                    setTimeout((function () {
                        this.cookieMessages([]);
                    }).bind(this), 5000);
                }
            },

        });
    }
});

Proposed solution

Convert cookieMessages into an observable. Unfortunately the fix for this will necessarily be backwards incompatible for merchants that have overwritten vendor/magento/module-theme/view/frontend/templates/messages.phtml.

I will create a PR.

edit: https://github.com/magento/magento2/pull/37309

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @erfanimani. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

erfanimani commented 1 year ago

@magento I am working on this

m2-assistant[bot] commented 1 year ago

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Hotel commented 1 year ago

Hello @erfanimani,

Thanks for the report and collaboration!

We agree with your point and saw the attached PR as well. But can you please provide us with some steps to reproduce this issue? It will help us in the fast movement of this issue.

Thanks

erfanimani commented 1 year ago

Hi @engcom-Hotel, could you clarify what you mean? This is a developer/customisation/code improvement — there's no end-user issue associated with this ticket.

engcom-Hotel commented 1 year ago

Hi @erfanimani,

What I mean to say is, in order to confirm the issue we need some proper steps to reproduce this issue. After confirmation on this ticket, a JIRA will create, and that JIRA will process further.

Thanks

erfanimani commented 1 year ago

Hi @engcom-Hotel, I'm not sure how relevant the steps to reproduce are for this ticket, but it would go something like this:

Expected result:

Actual result:

As mentioned, this is not an end user bug but an internal improvement.

engcom-Hotel commented 1 year ago

Hello @erfanimani,

Thanks for the reply and coordination!

We are able to reproduce the issue. In order to reproduce the issue, we have created the mixin. The same has been attached below for reference:

Magz 2.zip

Hence confirming the issue.

Thanks

github-jira-sync-bot commented 1 year ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-8854 is successfully created for this GitHub issue.

m2-assistant[bot] commented 1 year ago

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

ihor-sviziev commented 1 year ago

The issue was fixed in https://github.com/magento/magento2/commit/38b8b516489a6e436177c59f61ec5903cd8552c5 (changes from https://github.com/magento/magento2/pull/37309)