1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

handle coupons & plans & user change in cart directive

This commit is contained in:
Sylvain 2017-02-20 15:07:14 +01:00
parent 62860a45fc
commit 9b7f74e02d
5 changed files with 97 additions and 39 deletions

View File

@ -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

View File

@ -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

View File

@ -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()
}
]

View File

@ -41,10 +41,13 @@
<cart slot="selectedEvent"
selection-time="selectionTime"
slot-selection-time="selectionTime"
on-update="updateCalendar"
on-outdated="refetchCalendar"
user="ctrl.member"></cart>
user="ctrl.member"
mode-plans="plansAreShown"
plan="selectedPlan"
plan-selection-time="planSelectionTime"></cart>
<uib-alert type="warning m">
<p class="text-sm">

View File

@ -39,22 +39,22 @@
<coupon show="machineSlotsValid() && (!plansAreShown || selectedPlan)" coupon="coupon.applied" total="totalNoCoupon" user-id="{{user.id}}"></coupon>
<span ng-hide="fablabWithoutPlans">
<div ng-if="machineSlotsValid() && !user.subscribed_plan" ng-show="!plansAreShown">
<p class="font-sbold text-base l-h-2x" translate>{{ 'to_benefit_from_attractive_prices' }}</p>
<div><button class="btn btn-warning-full rounded btn-block text-xs" ng-click="showPlans()" translate>{{ 'view_our_subscriptions' }}</button></div>
<p class="font-bold text-base text-u-c text-center m-b-xs" translate>{{ 'or' }}</p>
</div>
<div ng-if="machineSlotsValid() && !user.subscribed_plan" ng-show="!plansAreShown">
<p class="font-sbold text-base l-h-2x" translate>{{ 'to_benefit_from_attractive_prices' }}</p>
<div><button class="btn btn-warning-full rounded btn-block text-xs" ng-click="showPlans()" translate>{{ 'view_our_subscriptions' }}</button></div>
<p class="font-bold text-base text-u-c text-center m-b-xs" translate>{{ 'or' }}</p>
</div>
<div ng-if="selectedPlan">
<div class="m-t-md m-b-sm text-base">{{ 'you_ve_just_selected_a_' | translate }} <br> <span class="font-sbold" translate>{{ '_subscription' }}</span> :</div>
<div class="panel panel-default bg-light m-n">
<div class="panel-body m-b-md">
<div class="font-sbold text-u-c">{{selectedPlan | humanReadablePlanName }}</div>
<div class="text-base">{{ 'cost_of_the_subscription' | translate }} <span class="text-blue">{{selectedPlan.amount | currency}}</span></div>
</div>
</div>
<div ng-if="selectedPlan">
<div class="m-t-md m-b-sm text-base">{{ 'you_ve_just_selected_a_' | translate }} <br> <span class="font-sbold" translate>{{ '_subscription' }}</span> :</div>
<div class="panel panel-default bg-light m-n">
<div class="panel-body m-b-md">
<div class="font-sbold text-u-c">{{selectedPlan | humanReadablePlanName }}</div>
<div class="text-base">{{ 'cost_of_the_subscription' | translate }} <span class="text-blue">{{selectedPlan.amount | currency}}</span></div>
</div>
</span>
</div>
</div>
</span>
</div>