From d1c1a614a9a4880dfc4e2e8f59554fe6130f8171 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 9 Aug 2016 10:22:01 +0200 Subject: [PATCH] i18n coupon directive --- .../javascripts/directives/coupon.coffee.erb | 38 +++++++++---------- app/assets/javascripts/router.coffee.erb | 3 +- app/assets/templates/shared/_coupon.html.erb | 13 ++++--- app/models/coupon.rb | 1 + config/locales/app.shared.en.yml | 12 +++++- config/locales/app.shared.fr.yml | 12 +++++- 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/directives/coupon.coffee.erb b/app/assets/javascripts/directives/coupon.coffee.erb index 4509f1020..d71d9081a 100644 --- a/app/assets/javascripts/directives/coupon.coffee.erb +++ b/app/assets/javascripts/directives/coupon.coffee.erb @@ -4,37 +4,33 @@ Application.Directives.directive 'coupon', [ 'Coupon', 'growl', '_t', (Coupon, g scope: show: '=' templateUrl: '<%= asset_path "shared/_coupon.html" %>' - link: (scope, element, attributes) -> + link: ($scope, element, attributes) -> - # default: do not enable code input - scope.input = false + # Whether code input is shown or not (ie. the link 'I have a coupon' is shown) + $scope.code = + input: false - # default: coupon code is not valid - scope.valid = false + # Available status are: 'pending', 'valid', 'invalid' + $scope.status = 'pending' - # for dirty checking and request response waiting - scope.pending = true - - # binding for the code inputed - scope.couponCode = null + # 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) + $scope.validateCode = -> + if $scope.couponCode == '' + $scope.status = 'pending' + else + Coupon.validate {code: $scope.couponCode}, (res) -> + $scope.status = 'valid' + growl.success(_t('the_coupon_has_been_applied_you_get_PERCENT_discount', {PERCENT: res.percent_off})) , (err) -> - scope.valid = false - growl.error(_t(err.data.status)) - console.error(err) + $scope.status = 'invalid' + growl.error(_t('unable_to_apply_the_coupon_because_'+err.data.status)) } ] diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index c51dcaa78..f08acea6c 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -363,7 +363,8 @@ angular.module('application.router', ['ui.router']). ] translations: [ 'Translations', (Translations) -> Translations.query(['app.logged.machines_reserve', 'app.shared.plan_subscribe', 'app.shared.member_select', - 'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', 'app.shared.wallet']).$promise + 'app.shared.stripe', 'app.shared.valid_reservation_modal', 'app.shared.confirm_modify_slot_modal', + 'app.shared.wallet', 'app.shared.coupon_input']).$promise ] .state 'app.admin.machines_edit', url: '/machines/:id/edit' diff --git a/app/assets/templates/shared/_coupon.html.erb b/app/assets/templates/shared/_coupon.html.erb index 1f277fdd7..6512730fa 100644 --- a/app/assets/templates/shared/_coupon.html.erb +++ b/app/assets/templates/shared/_coupon.html.erb @@ -1,8 +1,8 @@
- J'ai un code promo ! + {{ ' i_have_a_coupon' }} -
- +
+
- - - + + + +
diff --git a/app/models/coupon.rb b/app/models/coupon.rb index eba7fe0a8..7ccf64f63 100644 --- a/app/models/coupon.rb +++ b/app/models/coupon.rb @@ -11,6 +11,7 @@ class Coupon < ActiveRecord::Base validates :percent_off, presence: true validates :percent_off, :inclusion => 0..100 validates :validity_per_user, presence: true + validates :validity_per_user, inclusion: { in: %w(once forever) } def safe_destroy if self.invoices.size == 0 diff --git a/config/locales/app.shared.en.yml b/config/locales/app.shared.en.yml index a5137b029..35edb24f4 100644 --- a/config/locales/app.shared.en.yml +++ b/config/locales/app.shared.en.yml @@ -348,4 +348,14 @@ en: valid_until: "Valid until (included)" max_usages: "Maximum usages allowed" max_usages_must_be_equal_or_greater_than_0: "The maximum usages allowed must be greater than 0." - enabled: "Active" \ No newline at end of file + enabled: "Active" + + coupon_input: + # coupon (input zone for users) + i_have_a_coupon: "I have a coupon!" + code_: "Code:" + the_coupon_has_been_applied_you_get_PERCENT_discount: "The coupon has been applied. You get {{PERCENT}}% discount." # angular interpolation + unable_to_apply_the_coupon_because_disabled: "Unable to apply the coupon: this code was disabled." + unable_to_apply_the_coupon_because_expired: "Unable to apply the coupon: this code has expired." + unable_to_apply_the_coupon_because_sold_out: "Unable to apply the coupon: this code reached its quota." + unable_to_apply_the_coupon_because_rejected: "This code does not exists." \ No newline at end of file diff --git a/config/locales/app.shared.fr.yml b/config/locales/app.shared.fr.yml index 15837e6d8..1ac4f2af4 100644 --- a/config/locales/app.shared.fr.yml +++ b/config/locales/app.shared.fr.yml @@ -348,4 +348,14 @@ fr: valid_until: "Valable jusqu'au (inclus)" max_usages: "Nombre maximum d'utilisations autorisées" max_usages_must_be_equal_or_greater_than_0: "Le nombre d'utilisations maximum doit être supérieur ou égal à 0." - enabled: "Activé" \ No newline at end of file + enabled: "Activé" + + coupon_input: + # code promotionnel (zone de saisie pour les utilisateurs) + i_have_a_coupon: "J'ai un code promo !" + code_: "Code :" + the_coupon_has_been_applied_you_get_PERCENT_discount: "Le code promo a bien été appliqué. Vous bénéficiez d'une remise de {{PERCENT}} %." # angular interpolation + unable_to_apply_the_coupon_because_disabled: "Impossible d'appliquer la réduction : ce code promo a été désactivé." + unable_to_apply_the_coupon_because_expired: "Impossible d'appliquer la réduction : ce code promo a expiré." + unable_to_apply_the_coupon_because_sold_out: "Impossible d'appliquer la réduction : ce code promo a atteint son quota." + unable_to_apply_the_coupon_because_rejected: "Ce code promo n'existe pas." \ No newline at end of file