From 23320104c533ed8b9c22bdab59fe89321f8cb04f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 8 Aug 2016 12:08:09 +0200 Subject: [PATCH] ability to edit coupons --- .../controllers/admin/coupons.coffee | 78 ++++++++++++++++--- app/assets/javascripts/router.coffee.erb | 3 + .../templates/admin/coupons/_form.html.erb | 22 ++++-- .../templates/admin/coupons/edit.html.erb | 2 +- config/locales/app.admin.fr.yml | 4 + 5 files changed, 88 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/controllers/admin/coupons.coffee b/app/assets/javascripts/controllers/admin/coupons.coffee index ff3f5c0af..30d0f1451 100644 --- a/app/assets/javascripts/controllers/admin/coupons.coffee +++ b/app/assets/javascripts/controllers/admin/coupons.coffee @@ -5,10 +5,6 @@ Application.Controllers.controller "NewCouponController", ["$scope", "$state",'Coupon', 'growl', '_t' , ($scope, $state, Coupon, growl, _t) -> - - - ### PUBLIC SCOPE ### - ## Values for the coupon currently created $scope.coupon = active: true @@ -17,17 +13,22 @@ Application.Controllers.controller "NewCouponController", ["$scope", "$state",'C $scope.datePicker = format: Fablab.uibDateFormat opened: false # default: datePicker is not shown + minDate: moment().toDate() options: startingDay: Fablab.weekStartingDay + + ## - # Shows the validity limit datepicker + # Shows/hides the validity limit datepicker # @param $event {Object} jQuery event object ## - $scope.openDatePicker = ($event) -> + $scope.toggleDatePicker = ($event) -> $event.preventDefault() $event.stopPropagation() - $scope.datePicker.opened = true + $scope.datePicker.opened = !$scope.datePicker.opened + + ## # Callback to save the new coupon in $scope.coupon and redirect the user to the listing page @@ -41,14 +42,67 @@ Application.Controllers.controller "NewCouponController", ["$scope", "$state",'C ] + + + ## # Controller used in the coupon edition page ## -Application.Controllers.controller "EditCouponController", ["$scope", 'Coupon', '_t' -, ($scope, Coupon, _t) -> - - +Application.Controllers.controller "EditCouponController", ["$scope", "$state", 'Coupon', 'couponPromise', '_t' +, ($scope, $state, Coupon, couponPromise, _t) -> ### PUBLIC SCOPE ### - $scope.test = 'edit' + + + + ## Coupon to edit + $scope.coupon = couponPromise + + ## Default parameters for AngularUI-Bootstrap datepicker (used for coupon validity limit selection) + $scope.datePicker = + format: Fablab.uibDateFormat + opened: false # default: datePicker is not shown + minDate: moment().toDate() + options: + startingDay: Fablab.weekStartingDay + + + + ## + # Shows/hides the validity limit datepicker + # @param $event {Object} jQuery event object + ## + $scope.toggleDatePicker = ($event) -> + $event.preventDefault() + $event.stopPropagation() + $scope.datePicker.opened = !$scope.datePicker.opened + + + + ## + # Callback to save the coupon's changes to the API + ## + $scope.updateCoupon = -> + Coupon.update {id: $scope.coupon.id}, coupon: $scope.coupon, (coupon) -> + $state.go('app.admin.pricing') + , (err)-> + growl.error(_t('unable_to_update_the_coupon_an_error_occurred')) + console.error(err) + + + + ### PRIVATE SCOPE ### + + ## + # Kind of constructor: these actions will be realized first when the controller is loaded + ## + initialize = -> + # parse the date if any + if (couponPromise.valid_until) + $scope.coupon.valid_until = moment(couponPromise.valid_until).toDate() + + + + ## !!! MUST BE CALLED AT THE END of the controller + initialize() ] \ No newline at end of file diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 82fff8d5b..c51dcaa78 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -807,6 +807,9 @@ angular.module('application.router', ['ui.router']). templateUrl: '<%= asset_path "admin/coupons/edit.html" %>' controller: 'EditCouponController' resolve: + couponPromise: ['Coupon', '$stateParams', (Coupon, $stateParams) -> + Coupon.get({id: $stateParams.id}).$promise + ] translations: [ 'Translations', (Translations) -> Translations.query(['app.admin.coupons_edit', 'app.shared.coupon']).$promise ] diff --git a/app/assets/templates/admin/coupons/_form.html.erb b/app/assets/templates/admin/coupons/_form.html.erb index 156efd241..f9a1f97fd 100644 --- a/app/assets/templates/admin/coupons/_form.html.erb +++ b/app/assets/templates/admin/coupons/_form.html.erb @@ -38,14 +38,20 @@
- +
+ + + + +
diff --git a/app/assets/templates/admin/coupons/edit.html.erb b/app/assets/templates/admin/coupons/edit.html.erb index 498e01dab..94b56aa69 100644 --- a/app/assets/templates/admin/coupons/edit.html.erb +++ b/app/assets/templates/admin/coupons/edit.html.erb @@ -31,7 +31,7 @@
diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index ff1465542..b80423eeb 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -162,6 +162,10 @@ fr: add_a_coupon: "Ajouter un code promotionnel" unable_to_create_the_coupon_an_error_occurred: "Impossible de créer le code promotionnel : une erreur est survenue." + coupons_edit: + # mettre à jour un code promotionnel + unable_to_update_the_coupon_an_error_occurred: "Impossible de mettre à jour le code promotionnel : une erreur est survenue." + plans: new: # ajouter une formule d'abonnement sur la plate-forme