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