From 9b7f74e02d79b1a40cf8733110eabd643a14d0e2 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 20 Feb 2017 15:07:14 +0100 Subject: [PATCH] handle coupons & plans & user change in cart directive --- .../controllers/application.coffee.erb | 5 +- .../controllers/machines.coffee.erb | 22 ++---- .../javascripts/directives/cart.coffee.erb | 74 +++++++++++++++++-- .../templates/machines/reserve.html.erb | 7 +- app/assets/templates/shared/_cart.html.erb | 28 +++---- 5 files changed, 97 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/controllers/application.coffee.erb b/app/assets/javascripts/controllers/application.coffee.erb index 683c2e3ff..b7bb70b48 100644 --- a/app/assets/javascripts/controllers/application.coffee.erb +++ b/app/assets/javascripts/controllers/application.coffee.erb @@ -250,8 +250,9 @@ Application.Controllers.controller 'ApplicationController', ["$rootScope", "$sco # shorthands - $scope.isAuthenticated = Auth.isAuthenticated; - $scope.isAuthorized = AuthService.isAuthorized; + $scope.isAuthenticated = Auth.isAuthenticated + $scope.isAuthorized = AuthService.isAuthorized + $rootScope.login = $scope.login diff --git a/app/assets/javascripts/controllers/machines.coffee.erb b/app/assets/javascripts/controllers/machines.coffee.erb index a3d71fd75..b32298462 100644 --- a/app/assets/javascripts/controllers/machines.coffee.erb +++ b/app/assets/javascripts/controllers/machines.coffee.erb @@ -309,6 +309,8 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat ## will store the user's plan if he choosed to buy one $scope.selectedPlan = null + $scope.planSelectionTime = null + ## array of fullCalendar events. Slots where the user want to book $scope.eventsReserved = [] @@ -568,22 +570,12 @@ Application.Controllers.controller "ReserveMachineController", ["$scope", "$stat # @param plan {Object} the plan to subscribe ## $scope.selectPlan = (plan) -> - if $scope.isAuthenticated() - angular.forEach $scope.eventsReserved, (machineSlot)-> - angular.forEach $scope.ctrl.member.machine_credits, (credit)-> - if credit.machine_id = machineSlot.machine.id - credit.hours_used = 0 - machineSlot.machine.is_reduced_amount = false - - if $scope.selectedPlan != plan - $scope.selectedPlan = plan - else - $scope.selectedPlan = null - updateCartPrice() + $scope.planSelectionTime = new Date() + # toggle selected plan + if $scope.selectedPlan != plan + $scope.selectedPlan = plan else - $scope.login null, -> - $scope.selectedPlan = plan - updateCartPrice() + $scope.selectedPlan = null diff --git a/app/assets/javascripts/directives/cart.coffee.erb b/app/assets/javascripts/directives/cart.coffee.erb index 34526a937..1e120ca45 100644 --- a/app/assets/javascripts/directives/cart.coffee.erb +++ b/app/assets/javascripts/directives/cart.coffee.erb @@ -10,15 +10,18 @@ UNAVAILABLE_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::MACHINE_IS_RESERVED_BY_ BOOKED_SLOT_BORDER_COLOR = '<%= AvailabilityHelper::IS_RESERVED_BY_CURRENT_USER %>' -Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Price', '_t', ($rootScope, dialogs, growl, Price, _t) -> +Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Auth', 'Price', '_t', ($rootScope, dialogs, growl, Auth, Price, _t) -> { restrict: 'E' scope: slot: '=' - selectionTime: '=' + slotSelectionTime: '=' onUpdate: '=' onOutdated: '=' user: '=' + modePlans: '=' + plan: '=' + planSelectionTime: '=' templateUrl: '<%= asset_path "shared/_cart.html" %>' link: ($scope, element, attributes) -> @@ -51,10 +54,6 @@ Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Pr applied: null - # What the binded slot - $scope.$watch 'selectionTime', (newValue, oldValue) -> - if newValue != oldValue - slotSelectionChanged() ## # Add the provided slot to the shopping cart (state transition from free to 'about to be reserved') @@ -97,6 +96,49 @@ Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Pr + ## + # Checks that every selected slots were added to the shopping cart. Ie. will return false if + # any checked slot was not validated by the user. + ## + $scope.machineSlotsValid = -> + isValid = true + angular.forEach $scope.eventsReserved, (m)-> + isValid = false if !m.isValid + isValid + + + + ## + # Switch the user's view from the reservation agenda to the plan subscription + ## + $scope.showPlans = -> + $scope.modePlans = true + + + + ### PRIVATE SCOPE ### + + ## + # Kind of constructor: these actions will be realized first when the directive is loaded + ## + initialize = -> + # What the binded slot + $scope.$watch 'slotSelectionTime', (newValue, oldValue) -> + if newValue != oldValue + slotSelectionChanged() + $scope.$watch 'user', (newValue, oldValue) -> + if newValue != oldValue + slotSelectionChanged() + $scope.$watch 'planSelectionTime', (newValue, oldValue) -> + if newValue != oldValue + planSelectionChanged() + # watch when a coupon is applied to re-compute the total price + $scope.$watch 'coupon.applied', (newValue, oldValue) -> + unless newValue == null and oldValue == null + updateCartPrice() + + + ## # Callback triggered when the selected slot changed ## @@ -163,6 +205,21 @@ Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Pr updateCartPrice() + ## + # Callback triggered when the selected slot changed + ## + planSelectionChanged = -> + if Auth.isAuthenticated() + if $scope.selectedPlan != $scope.plan + $scope.selectedPlan = $scope.plan + else + $scope.selectedPlan = null + updateCartPrice() + else + $rootScope.login null, -> + $scope.selectedPlan = $scope.plan + updateCartPrice() + ## # Update the total price of the current selection/reservation @@ -225,6 +282,11 @@ Application.Directives.directive 'cart', [ '$rootScope', 'dialogs', 'growl', 'Pr offered: slot.offered || false reservation + + + + ## !!! MUST BE CALLED AT THE END of the directive + initialize() } ] diff --git a/app/assets/templates/machines/reserve.html.erb b/app/assets/templates/machines/reserve.html.erb index 037f05aff..d09209daa 100644 --- a/app/assets/templates/machines/reserve.html.erb +++ b/app/assets/templates/machines/reserve.html.erb @@ -41,10 +41,13 @@ + user="ctrl.member" + mode-plans="plansAreShown" + plan="selectedPlan" + plan-selection-time="planSelectionTime">

diff --git a/app/assets/templates/shared/_cart.html.erb b/app/assets/templates/shared/_cart.html.erb index a659f3f9b..aabe51fda 100644 --- a/app/assets/templates/shared/_cart.html.erb +++ b/app/assets/templates/shared/_cart.html.erb @@ -39,22 +39,22 @@ -

-

{{ 'to_benefit_from_attractive_prices' }}

-
-

{{ 'or' }}

-
+
+

{{ 'to_benefit_from_attractive_prices' }}

+
+

{{ 'or' }}

+
-
-
{{ 'you_ve_just_selected_a_' | translate }}
{{ '_subscription' }} :
-
-
-
{{selectedPlan | humanReadablePlanName }}
-
{{ 'cost_of_the_subscription' | translate }} {{selectedPlan.amount | currency}}
-
-
+
+
{{ 'you_ve_just_selected_a_' | translate }}
{{ '_subscription' }} :
+
+
+
{{selectedPlan | humanReadablePlanName }}
+
{{ 'cost_of_the_subscription' | translate }} {{selectedPlan.amount | currency}}
- +
+
+