From 140e0b9cb10cc9071f31535cdc96eb371b26fd92 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 15 Jan 2020 14:25:01 +0100 Subject: [PATCH] Refactor #exchangeLoadedVariants --- .../order_cycle_exchanges_controller.js.coffee | 10 +++------- .../panels/exchange_products_panel_header.html.haml | 2 +- app/views/admin/order_cycles/_exchange_form.html.haml | 4 ++-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee index bd692400a6..1a4ab8e79b 100644 --- a/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/controllers/order_cycle_exchanges_controller.js.coffee @@ -8,13 +8,6 @@ angular.module('admin.orderCycles') $scope.productsLoading = -> RequestMonitor.loading - $scope.exchangeLoadedVariants = (exchange) -> - loaded_variants = 0 - angular.forEach $scope.enterprises[exchange.enterprise_id].supplied_products, (product) -> - loaded_variants += product.variants.length - - loaded_variants - $scope.setSelectAllVariantsCheckboxValue = (exchange, totalNumberOfVariants) -> exchange.select_all_variants = $scope.exchangeSelectedVariants(exchange) >= totalNumberOfVariants @@ -55,12 +48,15 @@ angular.module('admin.orderCycles') return if enterprise.last_page_loaded? && enterprise.last_page_loaded >= page enterprise.last_page_loaded = page + enterprise.loaded_variants ?= 0 incoming = true if $scope.view == 'incoming' params = { exchange_id: exchange.id, enterprise_id: exchange.enterprise_id, order_cycle_id: $scope.order_cycle.id, incoming: incoming, page: page} ExchangeProduct.index params, (products, num_of_pages) -> enterprise.num_of_pages = num_of_pages enterprise.supplied_products.push products... + angular.forEach products, (product) -> + enterprise.loaded_variants += product.variants.length $scope.loadMoreExchangeProducts = (exchange) -> $scope.loadExchangeProducts(exchange, $scope.enterprises[exchange.enterprise_id].last_page_loaded + 1) diff --git a/app/assets/javascripts/templates/admin/panels/exchange_products_panel_header.html.haml b/app/assets/javascripts/templates/admin/panels/exchange_products_panel_header.html.haml index a08e2800c9..b5433e1706 100644 --- a/app/assets/javascripts/templates/admin/panels/exchange_products_panel_header.html.haml +++ b/app/assets/javascripts/templates/admin/panels/exchange_products_panel_header.html.haml @@ -1,5 +1,5 @@ .exchange-load-all-variants %div - {{ 'js.admin.panels.exchange_products.variants_loaded' | t:{ num_of_variants_loaded: exchangeLoadedVariants(exchange), total_number_of_variants: exchangeTotalVariants(exchange) } }} + {{ 'js.admin.panels.exchange_products.variants_loaded' | t:{ num_of_variants_loaded: enterprises[exchange.enterprise_id].loaded_variants, total_number_of_variants: exchangeTotalVariants(exchange) } }} %a{ 'ng-click' => 'loadAllExchangeProducts(exchange)', 'ng-show' => 'enterprises[exchange.enterprise_id].last_page_loaded < enterprises[exchange.enterprise_id].num_of_pages' } {{ 'js.admin.panels.exchange_products.load_all_variants' | t }} diff --git a/app/views/admin/order_cycles/_exchange_form.html.haml b/app/views/admin/order_cycles/_exchange_form.html.haml index e6f8b65b97..8ab45003c5 100644 --- a/app/views/admin/order_cycles/_exchange_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_form.html.haml @@ -31,10 +31,10 @@ - if type == 'supplier' %tr.panel-row{ object: "exchange", panels: "{products: 'exchange_products_supplied'}", - locals: "$index,exchangeLoadedVariants,exchangeTotalVariants,order_cycle,exchange,enterprises,setExchangeVariants,selectAllVariants,suppliedVariants,removeDistributionOfVariant,initializeExchangeProductsPanel,loadMoreExchangeProducts,loadAllExchangeProducts,productsLoading", + locals: "$index,exchangeTotalVariants,order_cycle,exchange,enterprises,setExchangeVariants,selectAllVariants,suppliedVariants,removeDistributionOfVariant,initializeExchangeProductsPanel,loadMoreExchangeProducts,loadAllExchangeProducts,productsLoading", colspan: 4 } - if type == 'distributor' %tr.panel-row{ object: "exchange", panels: "{products: 'exchange_products_distributed', tags: 'exchange_tags'}", - locals: "$index,exchangeLoadedVariants,exchangeTotalVariants,order_cycle,exchange,enterprises,setExchangeVariants,incomingExchangeVariantsFor,variantSuppliedToOrderCycle,initializeExchangeProductsPanel,loadMoreExchangeProducts,loadAllExchangeProducts,productsLoading", + locals: "$index,exchangeTotalVariants,order_cycle,exchange,enterprises,setExchangeVariants,incomingExchangeVariantsFor,variantSuppliedToOrderCycle,initializeExchangeProductsPanel,loadMoreExchangeProducts,loadAllExchangeProducts,productsLoading", colspan: 5 }