define([ 'jquery', 'Magento_Ui/js/modal/modal', 'Esun_Cart/js/model/coupon-service', 'mage/translate' ], function ($, modal, couponService) { 'use strict'; return function (config, element) { var $modal = $(element), $list = $modal.find('[data-role="coupon-picker-list"]'), $loading = $modal.find('[data-role="coupon-picker-loading"]'), $empty = $modal.find('[data-role="coupon-picker-empty"]'), $message = $modal.find('[data-role="coupon-picker-message"]'); modal({ type: 'popup', title: $.mage.__('My Coupons'), modalClass: 'customer-coupon-picker-modal', responsive: true, innerScroll: true, buttons: [] }, $modal); $(document).on('click.esunCustomerCoupon', '[data-role="open-coupon-picker"]', function (event) { event.preventDefault(); event.stopPropagation(); $modal.modal('openModal'); loadCoupons(); }); $modal.on('click', '[data-role="apply-customer-coupon"]', function () { applyCoupon($(this)); }); $modal.on('keydown', '[data-role="apply-customer-coupon"]', function (event) { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); applyCoupon($(this)); } }); function applyCoupon($item) { var code = String($item.attr('data-coupon-code') || ''); if (!code || $item.attr('aria-disabled') === 'true' || $item.hasClass('is-loading')) { return; } $item.addClass('is-loading').attr('aria-busy', 'true'); showMessage('', false); couponService.apply(code).done(function (response) { if (!response || !response.success) { showMessage(response && response.message ? response.message : $.mage.__('We could not apply this coupon.'), true); return; } $modal.modal('closeModal'); $(document).trigger('esun:customerCouponApplied', [response]); }).fail(function (xhr) { var response = xhr.responseJSON || {}; showMessage(response.message || $.mage.__('We could not apply this coupon.'), true); }).always(function () { $item.removeClass('is-loading').removeAttr('aria-busy'); }); } function loadCoupons() { $list.empty(); $empty.prop('hidden', true); showMessage('', false); $loading.prop('hidden', false); $.ajax({url: config.listUrl, type: 'get', dataType: 'json', cache: false}) .done(function (response) { var coupons = response && response.coupons ? response.coupons : [], currentCouponCode = String(response && response.current_coupon_code || ''); if (!coupons.length) { $empty.prop('hidden', false); return; } $.each(coupons, function (index, coupon) { $list.append(renderCoupon(coupon, currentCouponCode)); }); }) .fail(function (xhr) { var response = xhr.responseJSON || {}; if (xhr.status === 401) { window.location.href = config.loginUrl; return; } showMessage(response.message || $.mage.__('We could not load your coupons.'), true); }) .always(function () { $loading.prop('hidden', true); }); } function renderCoupon(coupon, currentCouponCode) { var $item, $body = $('
', {'class': 'customer-coupon-card__body'}), $conditions = $(''), $meta = $('
', {'class': 'customer-coupon-card__meta'}), isApplied = String(coupon.code) === currentCouponCode; $item = $('
', { 'class': 'customer-coupon-card customer-coupon-picker__item is-available' + (isApplied ? ' is-applied' : ''), role: 'button', tabindex: isApplied ? '-1' : '0', 'aria-disabled': isApplied ? 'true' : 'false', 'data-role': 'apply-customer-coupon', 'data-coupon-code': coupon.code }); $('
', {'class': 'customer-coupon-card__discount'}) .text(coupon.discount_label).appendTo($item); $('

').text(coupon.rule_name).appendTo($body); $('
', {'class': 'customer-coupon-card__code'}) .text(coupon.code).appendTo($body); $.each(coupon.conditions || [], function (index, condition) { $('
  • ').text(condition).appendTo($conditions); }); if ($conditions.children().length) { $conditions.appendTo($body); } $('') .text(coupon.expires_at ? $.mage.__('Valid through') + ' ' + coupon.expires_at : $.mage.__('No expiration date')) .appendTo($meta); if (isApplied) { $('').text($.mage.__('Applied')).appendTo($meta); } $meta.appendTo($body); $body.appendTo($item); return $item; } function showMessage(text, isError) { $message .text(text) .toggleClass('is-error', Boolean(isError)) .prop('hidden', !text); } }; });