Make total number of products in exchange work again.

Currently we are just loading the products from the server and count them.
This can be improved easily in two ways:
- we can switch this to a specific product count call to the server so that we dont load all products all the time
- or we paginate the products result and fetch the total_number from the payload of the first page.
This commit is contained in:
luisramos0
2019-11-11 13:21:33 +00:00
parent 2b3bc6d1ff
commit 3223bf930d
6 changed files with 45 additions and 34 deletions

View File

@@ -1,5 +1,23 @@
angular.module('admin.orderCycles').controller 'AdminOrderCycleIncomingCtrl', ($scope, $controller, $location, Enterprise, ocInstance) ->
$controller('AdminOrderCycleExchangesCtrl', {$scope: $scope, ocInstance: ocInstance, $location: $location})
$scope.enterpriseTotalVariants = (enterprise) ->
Enterprise.totalVariants(enterprise)
$scope.view = 'incoming'
$scope.exchangeTotalVariants = (exchange) ->
return unless this.enterprises? && this.enterprises[exchange.enterprise_id]?
enterprise = this.enterprises[exchange.enterprise_id]
return enterprise.numVariants if enterprise.numVariants?
$scope.loadExchangeProducts(this, exchange)
return unless enterprise.supplied_products?
enterprise.numVariants = $scope.countVariants(enterprise.supplied_products)
$scope.countVariants = (products) ->
return 0 unless products
numVariants = 0
for product in products
numVariants += product.variants.length
numVariants

View File

@@ -44,10 +44,16 @@ angular.module('admin.orderCycles')
$scope.removeDistributionOfVariant = (variant_id) ->
OrderCycle.removeDistributionOfVariant(variant_id)
# Load exchange data
initPanel = (scope) ->
Product.index {exchange_id: scope.exchange.id, enterprise_id: scope.exchange.enterprise_id}, (products) ->
scope.enterprises[scope.exchange.enterprise_id].supplied_products = products
# Load exchange products
$scope.loadExchangeProducts = (scope, exchange) ->
return if scope.enterprises[exchange.enterprise_id].supplied_products_fetched?
scope.enterprises[exchange.enterprise_id].supplied_products_fetched = true
Product.index {exchange_id: exchange.id, enterprise_id: exchange.enterprise_id}, (products) ->
scope.enterprises[exchange.enterprise_id].supplied_products = products
initPanel = (scope, exchange) ->
scope.loadExchangeProducts(scope, scope.exchange)
# Register listener to capture first toggle open of the products panel
exchangeProdutsInitialized = []

View File

@@ -35,13 +35,4 @@ angular.module('admin.orderCycles').factory('Enterprise', ($resource) ->
variant.id for variant in product.variants
else
[product.master_id]
totalVariants: (enterprise) ->
numVariants = 0
if enterprise
counts = for product in enterprise.supplied_products
numVariants += if product.variants.length == 0 then 1 else product.variants.length
numVariants
})