Paginate exchange products results

This commit breaks the OC page when there are more than 100 products in an exchange
This commit is contained in:
luisramos0
2019-11-18 14:51:34 +00:00
parent 9451f1b66d
commit d5e42ee1e5
3 changed files with 35 additions and 13 deletions

View File

@@ -11,5 +11,5 @@ angular.module('admin.orderCycles').factory('ExchangeProduct', ($resource) ->
index: (params={}, callback=null) ->
ExchangeProductResource.index params, (data) =>
@loaded = true
(callback || angular.noop)(data)
(callback || angular.noop)(data.products)
})

View File

@@ -1,6 +1,9 @@
# This controller lists products that can be added to an exchange
module Api
class ExchangeProductsController < Api::BaseController
DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 100
skip_authorization_check only: [:index]
# If exchange_id is present in the URL:
@@ -17,20 +20,17 @@ module Api
load_data_from_other_params
end
render_products
render_paginated_products products
end
private
def render_products
products = ExchangeProductsRenderer.
def products
ExchangeProductsRenderer.
new(@order_cycle, spree_current_user).
exchange_products(@incoming, @enterprise)
render json: products,
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
order_cycle: @order_cycle,
status: :ok
exchange_products(@incoming, @enterprise).
page(params[:page] || DEFAULT_PAGE).
per(params[:per_page] || DEFAULT_PER_PAGE)
end
def load_data_from_exchange
@@ -51,5 +51,27 @@ module Api
end
@incoming = params[:incoming]
end
def render_paginated_products(products)
serializer = ActiveModel::ArraySerializer.new(
products,
each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer,
order_cycle: @order_cycle
)
render text: {
products: serializer,
pagination: pagination_data(products)
}.to_json
end
def pagination_data(results)
{
results: results.total_count,
pages: results.num_pages,
page: (params[:page] || DEFAULT_PAGE).to_i,
per_page: (params[:per_page] || DEFAULT_PER_PAGE).to_i
}
end
end
end

View File

@@ -17,7 +17,7 @@ module Api
exchange = order_cycle.exchanges.incoming.first
spree_get :index, exchange_id: exchange.id
expect(json_response.first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
expect(json_response["products"].first["supplier_name"]).to eq exchange.variants.first.product.supplier.name
end
end
@@ -27,8 +27,8 @@ module Api
spree_get :index, exchange_id: exchange.id
suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
expect(suppliers).to include json_response.first["supplier_name"]
expect(suppliers).to include json_response.second["supplier_name"]
expect(suppliers).to include json_response["products"].first["supplier_name"]
expect(suppliers).to include json_response["products"].second["supplier_name"]
end
end
end