From 0a77cedaf8e74e49ce15632c02bea2a9c24b30fc Mon Sep 17 00:00:00 2001 From: Jure Grabnar Date: Thu, 27 Jun 2019 22:48:56 +0200 Subject: [PATCH] Fix sorting of external protocols and start showing results --- .../protocols/external_protocols_tab.js | 50 +++++++++++++++++-- .../stylesheets/protocol_management.scss | 5 ++ .../search_protocols_service.rb | 10 ---- .../protocols_io/v3/api_client.rb | 7 ++- .../protocols_io/v3/protocol_normalizer.rb | 1 + .../_list_of_protocol_cards.html.erb | 2 +- .../index/_external_protocols_tab.html.erb | 30 ++--------- config/initializers/constants.rb | 7 ++- 8 files changed, 68 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/protocols/external_protocols_tab.js b/app/assets/javascripts/protocols/external_protocols_tab.js index eebabc86c..f618b59d6 100644 --- a/app/assets/javascripts/protocols/external_protocols_tab.js +++ b/app/assets/javascripts/protocols/external_protocols_tab.js @@ -17,8 +17,7 @@ function applyClickCallbackOnProtocolCards() { }, error: function(_error) { // TODO: we should probably show some alert bubble - $('.empty-preview-panel').show(); - $('.full-preview-panel').hide(); + resetPreviewPanel(); animateSpinner($('.protocol-preview-panel'), false); } }); @@ -27,4 +26,49 @@ function applyClickCallbackOnProtocolCards() { }); } -applyClickCallbackOnProtocolCards(); +// Resets preview to the default state +function resetPreviewPanel() { + $('.empty-preview-panel').show(); + $('.full-preview-panel').hide(); +} + +function setDefaultViewState() { + resetPreviewPanel(); + $('.empty-text').show(); + $('.list-wrapper').hide(); +} + +// Apply AJAX callbacks onto the search box +function applySearchCallback() { + // Submit form on every input in the search box + $('input[name="key"]').off('input').on('input', function() { + $('form.protocols-search-bar').submit(); + }); + + // Submit form when clicking on sort buttons + $('.protocol-sort label').off('click').on('click', function () { + $('form.protocols-search-bar').submit(); + }); + + // Bind ajax calls on the form + $('form.protocols-search-bar').off('ajax:success').off('ajax:error') + .bind('ajax:success', function(evt, data, status, xhr) { + if (data.html) { + resetPreviewPanel(); + $('.empty-text').hide(); + $('.list-wrapper').show(); + + $('.list-wrapper').html(data.html) + applyClickCallbackOnProtocolCards(); + } else { + setDefaultViewState(); + } + }) + .bind("ajax:error", function(evt, xhr, status, error) { + setDefaultViewState(); + + console.log(xhr.responseText); + }); +} + +applySearchCallback(); diff --git a/app/assets/stylesheets/protocol_management.scss b/app/assets/stylesheets/protocol_management.scss index cb02c54e2..e4fab7e0a 100644 --- a/app/assets/stylesheets/protocol_management.scss +++ b/app/assets/stylesheets/protocol_management.scss @@ -150,6 +150,11 @@ font-size: 13px; margin-top: 20px; } + + .list-wrapper { + overflow: auto; + height: 100%; + } } .protocol-preview-panel { diff --git a/app/services/protocol_importers/search_protocols_service.rb b/app/services/protocol_importers/search_protocols_service.rb index f3fd73034..7c71a7a4f 100644 --- a/app/services/protocol_importers/search_protocols_service.rb +++ b/app/services/protocol_importers/search_protocols_service.rb @@ -51,16 +51,6 @@ module ProtocolImporters @errors[:invalid_params][:page_id] = 'Page needs to be positive' end - # try if order_field is ok - if @query_params[:order_field] && CONSTANTS[:available_order_fields].exclude?(@query_params[:order_field]&.to_sym) - @errors[:invalid_params][:order_field] = 'Order field is not ok' - end - - # try if order dir is ok - if @query_params[:order_field] && CONSTANTS[:available_order_dirs].exclude?(@query_params[:order_dir]&.to_sym) - @errors[:invalid_params][:order_dir] = 'Order dir is not ok' - end - # try if endpints exists @errors[:invalid_params][:source_endpoint] = 'Wrong source endpoint' unless endpoint_name&.is_a?(String) diff --git a/app/utilities/protocol_importers/protocols_io/v3/api_client.rb b/app/utilities/protocol_importers/protocols_io/v3/api_client.rb index 441dbd200..a6df7d38f 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/api_client.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/api_client.rb @@ -44,8 +44,13 @@ module ProtocolImporters # Default is 1. def protocol_list(query_params = {}) response = with_handle_network_errors do + sort_mappings = CONSTANTS[:sort_mappings] query = CONSTANTS.dig(:endpoints, :protocols, :default_query_params) - .merge(query_params) + .merge(query_params.except(:sort_by)) + + if sort_mappings[query_params[:sort_by].to_sym] + query = query.merge(sort_mappings[query_params[:sort_by].to_sym]) + end self.class.get('/protocols', query: query) end diff --git a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb index cac909a40..dd43beee2 100644 --- a/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb +++ b/app/utilities/protocol_importers/protocols_io/v3/protocol_normalizer.rb @@ -69,6 +69,7 @@ module ProtocolImporters { id: e[:id], title: e[:title], + source: Constants::PROTOCOLS_IO_V3_API[:source_id], created_on: e[:created_on], authors: e[:authors].map { |a| a[:name] }.join(', '), nr_of_steps: e[:stats][:number_of_steps], diff --git a/app/views/protocol_importers/_list_of_protocol_cards.html.erb b/app/views/protocol_importers/_list_of_protocol_cards.html.erb index 4164c5c72..4e69c4c1c 100644 --- a/app/views/protocol_importers/_list_of_protocol_cards.html.erb +++ b/app/views/protocol_importers/_list_of_protocol_cards.html.erb @@ -1,4 +1,4 @@ -<% protocols.each do |protocol| %> +<% protocols[:protocols].each do |protocol| %> <%= render partial: 'protocol_importers/protocol_card', locals: { protocol: protocol } %>
diff --git a/app/views/protocols/index/_external_protocols_tab.html.erb b/app/views/protocols/index/_external_protocols_tab.html.erb index 582ee16be..d7b6e1ad2 100644 --- a/app/views/protocols/index/_external_protocols_tab.html.erb +++ b/app/views/protocols/index/_external_protocols_tab.html.erb @@ -21,10 +21,11 @@ +
<%= t('protocols.index.external_protocols.sort_by.title') %> -
+
-
- - -
- Banana protocol (click me) -
- -
- Cut run targeted protocol (click me) -
- -
- Error protocol (click me, should default to default screen) -
-
+
diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 12816c31a..c2b8cc11f 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -203,8 +203,11 @@ class Constants base_uri: 'https://www.protocols.io/api/v3/', default_timeout: 10, debug_level: :debug, - available_order_fields: %i(title created_on), - available_order_dirs: %i(asc desc), + sort_mappings: { + alpha: { order_field: :name, order_dir: :asc }, + newest: { order_field: :date, order_dir: :desc }, + oldest: { order_field: :date, order_dir: :asc } + }, endpoints: { protocols: { default_query_params: {