init
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
define(['jquery'], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $root = $(element);
|
||||
|
||||
$root.on('click', '[data-coupon-tab]', function () {
|
||||
var status = $(this).attr('data-coupon-tab');
|
||||
|
||||
$root.find('[data-coupon-tab]')
|
||||
.removeClass('is-active')
|
||||
.attr('aria-selected', 'false');
|
||||
$(this).addClass('is-active').attr('aria-selected', 'true');
|
||||
$root.find('[data-coupon-panel]').removeClass('is-active').prop('hidden', true);
|
||||
$root.find('[data-coupon-panel="' + status + '"]')
|
||||
.addClass('is-active')
|
||||
.prop('hidden', false);
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,149 @@
|
||||
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 = $('<div></div>', {'class': 'customer-coupon-card__body'}),
|
||||
$conditions = $('<ul></ul>'),
|
||||
$meta = $('<div></div>', {'class': 'customer-coupon-card__meta'}),
|
||||
isApplied = String(coupon.code) === currentCouponCode;
|
||||
|
||||
$item = $('<article></article>', {
|
||||
'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
|
||||
});
|
||||
$('<div></div>', {'class': 'customer-coupon-card__discount'})
|
||||
.text(coupon.discount_label).appendTo($item);
|
||||
$('<h3></h3>').text(coupon.rule_name).appendTo($body);
|
||||
$('<div></div>', {'class': 'customer-coupon-card__code'})
|
||||
.text(coupon.code).appendTo($body);
|
||||
$.each(coupon.conditions || [], function (index, condition) {
|
||||
$('<li></li>').text(condition).appendTo($conditions);
|
||||
});
|
||||
if ($conditions.children().length) {
|
||||
$conditions.appendTo($body);
|
||||
}
|
||||
$('<span></span>')
|
||||
.text(coupon.expires_at
|
||||
? $.mage.__('Valid through') + ' ' + coupon.expires_at
|
||||
: $.mage.__('No expiration date'))
|
||||
.appendTo($meta);
|
||||
if (isApplied) {
|
||||
$('<strong></strong>').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);
|
||||
}
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user