set-checkout-messages.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  3. * @author Mediotype https://www.mediotype.com/
  4. */
  5. define(
  6. [
  7. 'underscore',
  8. 'Magento_Customer/js/customer-data',
  9. 'Magento_Ui/js/model/messageList'
  10. ],
  11. function (_, customerData, messageContainer) {
  12. 'use strict';
  13. /**
  14. * A utility for observing message updates in session storage. It is designed to subscribe to
  15. * customer data updates and forward messages to the appropriate messageList model.
  16. */
  17. return function () {
  18. var typeMap = {
  19. 'success': 'addSuccessMessage',
  20. 'warning': 'addErrorMessage',
  21. 'error': 'addErrorMessage'
  22. },
  23. /**
  24. * Observe message section data changes and forward to the error processor.
  25. * @param {Object} data - The observable payload.
  26. * @return void
  27. */
  28. messageSubscriptionCallback = function (data) {
  29. if ('messages' in data) {
  30. _.each(data.messages, function (message) {
  31. if (message.type in typeMap) {
  32. messageContainer[typeMap[message.type]]({
  33. 'message': message.text
  34. });
  35. }
  36. });
  37. }
  38. };
  39. customerData.get('messages').subscribe(messageSubscriptionCallback);
  40. };
  41. }
  42. );