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

42 lines
1.2 KiB
Plaintext
Raw Normal View History

Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, growl, _t) ->
{
restrict: 'E'
scope:
show: '='
coupon: '='
templateUrl: '<%= asset_path "shared/_coupon.html" %>'
2016-08-09 10:22:01 +02:00
link: ($scope, element, attributes) ->
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-09 10:22:01 +02:00
# Available status are: 'pending', 'valid', 'invalid'
$scope.status = 'pending'
2016-08-09 10:22:01 +02:00
# Binding for the code inputed
$scope.couponCode = null
##
# Callback to validate the code
##
2016-08-09 10:22:01 +02:00
$scope.validateCode = ->
if $scope.couponCode == ''
$scope.status = 'pending'
$scope.coupon = null
2016-08-09 10:22:01 +02:00
else
Coupon.validate {code: $scope.couponCode}, (res) ->
$scope.status = 'valid'
$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}))
, (err) ->
2016-08-09 10:22:01 +02:00
$scope.status = 'invalid'
$scope.coupon = null
2016-08-09 10:22:01 +02:00
growl.error(_t('unable_to_apply_the_coupon_because_'+err.data.status))
}
]