2016-11-30 10:17:05 +01:00
|
|
|
Application.Directives.directive 'coupon', [ '$rootScope', 'Coupon', '_t', ($rootScope, Coupon, _t) ->
|
2016-08-08 17:09:05 +02:00
|
|
|
{
|
|
|
|
restrict: 'E'
|
|
|
|
scope:
|
|
|
|
show: '='
|
2016-08-09 16:36:14 +02:00
|
|
|
coupon: '='
|
2016-11-24 15:01:35 +01:00
|
|
|
total: '='
|
2016-08-10 11:08:01 +02:00
|
|
|
userId: '@'
|
2016-08-08 17:09:05 +02:00
|
|
|
templateUrl: '<%= asset_path "shared/_coupon.html" %>'
|
2016-08-09 10:22:01 +02:00
|
|
|
link: ($scope, element, attributes) ->
|
2016-08-08 17:09:05 +02:00
|
|
|
|
2016-08-09 10:22:01 +02:00
|
|
|
# Whether code input is shown or not (ie. the link 'I have a coupon' is shown)
|
|
|
|
$scope.code =
|
|
|
|
input: false
|
2016-08-08 17:09:05 +02:00
|
|
|
|
2016-08-09 10:22:01 +02:00
|
|
|
# Available status are: 'pending', 'valid', 'invalid'
|
|
|
|
$scope.status = 'pending'
|
2016-08-08 17:09:05 +02:00
|
|
|
|
2016-11-24 13:58:41 +01:00
|
|
|
# Binding for the code inputed (see the attached template)
|
2016-08-09 10:22:01 +02:00
|
|
|
$scope.couponCode = null
|
2016-08-08 17:09:05 +02:00
|
|
|
|
2016-11-30 10:17:05 +01:00
|
|
|
# Code validation messages
|
|
|
|
$scope.messages = []
|
|
|
|
|
2016-11-24 15:01:35 +01:00
|
|
|
# Re-compute if the code can be applied when the total of the cart changes
|
|
|
|
$scope.$watch 'total', (newValue, oldValue) ->
|
|
|
|
if newValue and newValue != oldValue and $scope.couponCode
|
|
|
|
$scope.validateCode()
|
|
|
|
|
2016-08-08 17:09:05 +02:00
|
|
|
##
|
|
|
|
# Callback to validate the code
|
|
|
|
##
|
2016-08-09 10:22:01 +02:00
|
|
|
$scope.validateCode = ->
|
2016-11-30 10:17:05 +01:00
|
|
|
$scope.messages = []
|
2016-08-09 10:22:01 +02:00
|
|
|
if $scope.couponCode == ''
|
|
|
|
$scope.status = 'pending'
|
2016-08-09 16:36:14 +02:00
|
|
|
$scope.coupon = null
|
2016-08-09 10:22:01 +02:00
|
|
|
else
|
2016-11-24 13:58:41 +01:00
|
|
|
Coupon.validate {code: $scope.couponCode, user_id: $scope.userId, amount: $scope.total}, (res) ->
|
2016-08-09 10:22:01 +02:00
|
|
|
$scope.status = 'valid'
|
2016-08-09 16:36:14 +02:00
|
|
|
$scope.coupon = res
|
2016-11-24 18:05:16 +01:00
|
|
|
if res.type == 'percent_off'
|
2016-11-30 10:17:05 +01:00
|
|
|
$scope.messages.push(type: 'success', message: _t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off}))
|
2016-11-24 18:05:16 +01:00
|
|
|
else
|
2016-11-30 10:17:05 +01:00
|
|
|
$scope.messages.push(type: 'success', message: _t('the_coupon_has_been_applied_you_get_AMOUNT_CURRENCY', {AMOUNT: res.amount_off, CURRENCY: $rootScope.currencySymbol}))
|
2016-08-08 17:09:05 +02:00
|
|
|
, (err) ->
|
2016-08-09 10:22:01 +02:00
|
|
|
$scope.status = 'invalid'
|
2016-08-09 16:36:14 +02:00
|
|
|
$scope.coupon = null
|
2016-11-30 10:17:05 +01:00
|
|
|
$scope.messages.push(type: 'danger', message: _t('unable_to_apply_the_coupon_because_'+err.data.status))
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to remove the message at provided index from the displayed list
|
|
|
|
##
|
|
|
|
$scope.closeMessage = (index) ->
|
|
|
|
$scope.messages.splice(index, 1);
|
2016-08-08 17:09:05 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|