diff --git a/app/assets/javascripts/directives/coupon.coffee.erb b/app/assets/javascripts/directives/coupon.coffee.erb new file mode 100644 index 000000000..4509f1020 --- /dev/null +++ b/app/assets/javascripts/directives/coupon.coffee.erb @@ -0,0 +1,41 @@ +Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, growl, _t) -> + { + restrict: 'E' + scope: + show: '=' + templateUrl: '<%= asset_path "shared/_coupon.html" %>' + link: (scope, element, attributes) -> + + # default: do not enable code input + scope.input = false + + # default: coupon code is not valid + scope.valid = false + + # for dirty checking and request response waiting + scope.pending = true + + # binding for the code inputed + scope.couponCode = null + + + scope.closeAlert = (index) -> + scope.alerts.splice(index, 1); + + ## + # Callback to validate the code + ## + scope.validateCode = -> + unless scope.couponCode == '' + Coupon.validate {code: scope.couponCode}, (res) -> + scope.valid = true + growl.success(res.percent_off+' % de réduction') + console.info(res) + , (err) -> + scope.valid = false + growl.error(_t(err.data.status)) + console.error(err) + } +] + + diff --git a/app/assets/javascripts/services/coupon.coffee b/app/assets/javascripts/services/coupon.coffee index 8c13f0b21..d016e5646 100644 --- a/app/assets/javascripts/services/coupon.coffee +++ b/app/assets/javascripts/services/coupon.coffee @@ -5,4 +5,7 @@ Application.Services.factory 'Coupon', ["$resource", ($resource)-> {id: "@id"}, update: method: 'PUT' + validate: + method: 'POST' + url: '/api/coupons/validate' ] diff --git a/app/assets/templates/machines/reserve.html.erb b/app/assets/templates/machines/reserve.html.erb index b61a953e5..91407ba76 100644 --- a/app/assets/templates/machines/reserve.html.erb +++ b/app/assets/templates/machines/reserve.html.erb @@ -78,6 +78,8 @@
{{ 'remove_this_slot' }}
+ +

{{ 'to_benefit_from_attractive_prices' }}

diff --git a/app/assets/templates/shared/_coupon.html.erb b/app/assets/templates/shared/_coupon.html.erb new file mode 100644 index 000000000..1f277fdd7 --- /dev/null +++ b/app/assets/templates/shared/_coupon.html.erb @@ -0,0 +1,20 @@ +
+ J'ai un code promo ! + +
+ +
+ + + + + +
+
+
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a298accd6..98ee3f190 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,9 +69,8 @@ Rails.application.routes.draw do resources :prices, only: [:index, :update] do post 'compute', on: :collection end - resources :coupons do - post 'validate' => 'coupons#validate' - end + resources :coupons + post 'coupons/validate' => 'coupons#validate' resources :trainings_pricings, only: [:index, :update]