Show a modal when available stock levels have reduced

This commit is contained in:
Rohan Mitchell
2016-04-08 15:39:25 +10:00
parent 5e39b11c2f
commit 35117f7af4
5 changed files with 49 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, storage)->
Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $rootScope, storage)->
# Handles syncing of current cart/order state to server
new class Cart
dirty: false
@@ -41,21 +41,30 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, storage)->
@update_running = false
compareAndNotifyStockLevels: (stockLevels) =>
scope = $rootScope.$new(true)
scope.variants = []
# TODO: These changes to quantity/max_quantity trigger another cart update, which
# is unnecessary.
for li in @line_items_present()
if !stockLevels[li.variant.id]?
alert "Variant out of stock: #{li.variant.id}"
li.quantity = 0
li.max_quantity = 0 if li.max_quantity?
li.variant.count_on_hand = 0
scope.variants.push li.variant
else
if stockLevels[li.variant.id].quantity < li.quantity
alert "Variant quantity reduced: #{li.variant.id}"
li.quantity = stockLevels[li.variant.id].quantity
scope.variants.push li.variant
if stockLevels[li.variant.id].max_quantity < li.max_quantity
alert "Variant max_quantity reduced: #{li.variant.id}"
li.max_quantity = stockLevels[li.variant.id].max_quantity
scope.variants.push li.variant
li.variant.count_on_hand = stockLevels[li.variant.id].on_hand
if scope.variants.length > 0
$modal.open(templateUrl: "out_of_stock.html", scope: scope, windowClass: 'out-of-stock-modal')
popQueue: =>
@update_enqueued = false
@scheduleUpdate()

View File

@@ -0,0 +1,10 @@
%h3 Reduced stock available
%p While you've been shopping, the stock levels for one or more of the products in your cart have reduced. Here's what's changed:
%p{'ng-repeat' => "v in variants"}
%em {{ v.name_to_display }} - {{ v.unit_to_display }}
%span{'ng-if' => "v.count_on_hand == 0"}
is now out of stock.
%span{'ng-if' => "v.count_on_hand > 0"}
now only has {{ v.count_on_hand }} remaining.