From e38c45314b605299f31e6d8986d410ecc31ddd27 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 10 Jan 2023 13:45:15 +0100 Subject: [PATCH] Update sidebar along with experiment table [SCI-7704] --- app/assets/javascripts/experiments/table.js | 9 +++- app/controllers/experiments_controller.rb | 44 ++++++++++++++----- app/views/experiments/table.html.erb | 4 +- .../sidebar/_archived_my_modules.html.erb | 2 +- app/views/shared/sidebar/_my_modules.html.erb | 2 +- 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/experiments/table.js b/app/assets/javascripts/experiments/table.js index 901554eae..948473178 100644 --- a/app/assets/javascripts/experiments/table.js +++ b/app/assets/javascripts/experiments/table.js @@ -1,4 +1,5 @@ -/* global I18n GLOBAL_CONSTANTS InfiniteScroll initBSTooltips filterDropdown dropdownSelector HelperModule */ +/* global I18n GLOBAL_CONSTANTS InfiniteScroll + initBSTooltips filterDropdown dropdownSelector Sidebar HelperModule */ var ExperimnetTable = { permissions: ['editable', 'archivable', 'restorable', 'moveable'], @@ -528,6 +529,12 @@ var ExperimnetTable = { var dataUrl = $(this.table).data('my-modules-url'); $(this.table).find('.table-row').remove(); this.loadPlaceholder(); + + Sidebar.reload({ + sort: this.myModulesCurrentSort, + view_mode: $('#experimentTable').hasClass('archived') ? 'archived' : '' + }); + $.get(dataUrl, tableParams, (result) => { $(this.table).find('.table-row-placeholder, .table-row-placeholder-divider').remove(); this.appendRows(result.data); diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 3ee231225..4d4897347 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -89,18 +89,7 @@ class ExperimentsController < ApplicationController end def table - view_state = @experiment.current_view_state(current_user) - view_mode = params[:view_mode] || 'active' - @current_sort = view_state.state.dig('my_modules', view_mode, 'sort') || 'atoz' - @project = @experiment.project - @my_modules = if @experiment.archived_branch? - @experiment.my_modules.order(:name) - elsif params[:view_mode] == 'archived' - @experiment.my_modules.archived.order(:name) - else - @experiment.my_modules.active.order(:name) - end @my_module_visible_table_columns = current_user.my_module_visible_table_columns end @@ -396,11 +385,23 @@ class ExperimentsController < ApplicationController end def sidebar + view_state = @experiment.current_view_state(current_user) + view_mode = params[:view_mode].presence || 'active' + default_sort = view_state.state.dig('my_modules', view_mode, 'sort') || 'atoz' + my_modules = if @experiment.archived_branch? + @experiment.my_modules + elsif params[:view_mode] == 'archived' + @experiment.my_modules.archived + else + @experiment.my_modules.active + end + + my_modules = sort_my_modules(my_modules, params[:sort].presence || default_sort) respond_to do |format| format.json do render json: { html: render_to_string( - partial: 'shared/sidebar/my_modules.html.erb', locals: { experiment: @experiment } + partial: 'shared/sidebar/my_modules.html.erb', locals: { experiment: @experiment, my_modules: my_modules } ) } end @@ -596,4 +597,23 @@ class ExperimentsController < ApplicationController table_experiment_path(@experiment, view_mode: params[:view_mode]) end end + + def sort_my_modules(records, sort) + case sort + when 'due_first' + records.order(:due_date) + when 'due_last' + records.order(Arel.sql("COALESCE(due_date, DATE '1900-01-01') DESC")) + when 'atoz' + records.order(:name) + when 'ztoa' + records.order(name: :desc) + when 'archived_old' + records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) ASC')) + when 'archived_new' + records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) DESC')) + else + records + end + end end diff --git a/app/views/experiments/table.html.erb b/app/views/experiments/table.html.erb index 79e23ed12..c870d2ecd 100644 --- a/app/views/experiments/table.html.erb +++ b/app/views/experiments/table.html.erb @@ -5,9 +5,9 @@ <%= content_for :sidebar do %> <% if params[:view_mode] == 'archived' %> - <%= render partial: 'shared/sidebar/archived_my_modules.html.erb', locals: { experiment: @experiment, my_modules: @my_modules } %> + <%= render partial: 'shared/sidebar/archived_my_modules.html.erb', locals: { experiment: @experiment, my_modules: nil } %> <% else %> - <%= render partial: 'shared/sidebar/my_modules.html.erb', locals: { experiment: @experiment, my_modules: @my_modules } %> + <%= render partial: 'shared/sidebar/my_modules.html.erb', locals: { experiment: @experiment, my_modules: nil } %> <% end %> <% end %> diff --git a/app/views/shared/sidebar/_archived_my_modules.html.erb b/app/views/shared/sidebar/_archived_my_modules.html.erb index aceddf93b..654668d1e 100644 --- a/app/views/shared/sidebar/_archived_my_modules.html.erb +++ b/app/views/shared/sidebar/_archived_my_modules.html.erb @@ -12,7 +12,7 @@ <% end %> <% end %> - <% my_modules.each do |my_module| %> + <% my_modules&.each do |my_module| %> - <% my_modules.readable_by_user(current_user).each do |my_module| %> + <% my_modules&.readable_by_user(current_user)&.each do |my_module| %>