1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/assets/javascripts/directives/coupon.js.erb

76 lines
2.7 KiB
Plaintext
Raw Normal View History

/* eslint-disable
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-20 13:44:53 +01:00
Application.Directives.directive('coupon', [ '$rootScope', 'Coupon', '_t', function ($rootScope, Coupon, _t) {
return ({
restrict: 'E',
scope: {
show: '=',
coupon: '=',
total: '=',
userId: '@'
},
templateUrl: '<%= asset_path "shared/_coupon.html" %>',
link ($scope, element, attributes) {
// Whether code input is shown or not (ie. the link 'I have a coupon' is shown)
2016-08-09 10:22:01 +02:00
$scope.code =
2018-11-21 11:08:53 +01:00
{ input: false };
// Available status are: 'pending', 'valid', 'invalid'
2018-11-21 11:08:53 +01:00
$scope.status = 'pending';
// Binding for the code inputed (see the attached template)
2018-11-21 11:08:53 +01:00
$scope.couponCode = null;
// Code validation messages
2018-11-21 11:08:53 +01:00
$scope.messages = [];
// Re-compute if the code can be applied when the total of the cart changes
$scope.$watch('total', function (newValue, oldValue) {
if (newValue && (newValue !== oldValue) && $scope.couponCode) {
2018-11-21 11:08:53 +01:00
return $scope.validateCode();
}
2018-11-21 11:08:53 +01:00
});
2018-11-19 16:17:49 +01:00
/**
* Callback to validate the code
*/
$scope.validateCode = function () {
2018-11-21 11:08:53 +01:00
$scope.messages = [];
if ($scope.couponCode === '') {
2018-11-21 11:08:53 +01:00
$scope.status = 'pending';
return $scope.coupon = null;
} else {
return Coupon.validate({ code: $scope.couponCode, user_id: $scope.userId, amount: $scope.total }, function (res) {
2018-11-21 11:08:53 +01:00
$scope.status = 'valid';
$scope.coupon = res;
if (res.type === 'percent_off') {
2018-11-21 11:08:53 +01:00
return $scope.messages.push({ type: 'success', message: _t('the_coupon_has_been_applied_you_get_PERCENT_discount', { PERCENT: res.percent_off }) });
} else {
2018-11-21 11:08:53 +01:00
return $scope.messages.push({ type: 'success', message: _t('the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY', { AMOUNT: res.amount_off, CURRENCY: $rootScope.currencySymbol }) });
}
}
, function (err) {
2018-11-21 11:08:53 +01:00
$scope.status = 'invalid';
$scope.coupon = null;
return $scope.messages.push({ type: 'danger', message: _t(`unable_to_apply_the_coupon_because_${err.data.status}`) });
});
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Callback to remove the message at provided index from the displayed list
*/
2018-11-21 11:08:53 +01:00
$scope.closeMessage = function (index) { $scope.messages.splice(index, 1); };
}
2018-11-21 11:08:53 +01:00
});
}]);