2016-08-08 17:09:05 +02:00
|
|
|
Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, growl, _t) ->
|
|
|
|
{
|
|
|
|
restrict: 'E'
|
|
|
|
scope:
|
|
|
|
show: '='
|
2016-08-09 16:36:14 +02:00
|
|
|
coupon: '='
|
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-08-09 10:22:01 +02:00
|
|
|
# Binding for the code inputed
|
|
|
|
$scope.couponCode = null
|
2016-08-08 17:09:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Callback to validate the code
|
|
|
|
##
|
2016-08-09 10:22:01 +02:00
|
|
|
$scope.validateCode = ->
|
|
|
|
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
|
|
|
|
Coupon.validate {code: $scope.couponCode}, (res) ->
|
|
|
|
$scope.status = 'valid'
|
2016-08-09 16:36:14 +02:00
|
|
|
$scope.coupon = res
|
2016-08-09 10:22:01 +02:00
|
|
|
growl.success(_t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off}))
|
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-08-09 10:22:01 +02:00
|
|
|
growl.error(_t('unable_to_apply_the_coupon_because_'+err.data.status))
|
2016-08-08 17:09:05 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|