Skip to content

Commit

Permalink
Refactor to avoid extra JS usage
Browse files Browse the repository at this point in the history
  • Loading branch information
LeticiaErrandonea committed Jan 25, 2024
1 parent 550e217 commit 48973a3
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 57 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/app/user_product_access_list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
margin-bottom: 0px;
}
}

.inline {
display: inline;
}
2 changes: 1 addition & 1 deletion app/controllers/product_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def index
def search
@product_users = all_product_users(params[:search]).paginate(page: params[:page], per_page: USERS_PER_PAGE)

@search_action_performed = params[:search].present?
@search_action_performed = params[:search]

render layout: false
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/product_users/_export_csv.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= form_tag polymorphic_path([current_facility, @product, :users], format: :csv), method: :get, class: "search_form inline" do
= hidden_field_tag :email, current_user.email, disabled: true
= hidden_field_tag :format, params[:format], disabled: true

= link_to t("reports.product_users.export"), url_for(format: :csv), class: "js--exportSearchResults pull-right", data: { form: ".search_form" }
59 changes: 29 additions & 30 deletions app/views/product_users/_table.html.haml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
#result
%table.table.table-striped.table-hover
%thead
%table.table.table-striped.table-hover
%thead
%tr
%th= t(".actions")
%th= User.human_attribute_name(:name)
%th= t(".date_added")
%th= User.human_attribute_name(:username)
%th= User.human_attribute_name(:email)
- if local_assigns[:f]
%th= ProductAccessGroup.model_name.human
%tbody
- @product_users.each do |product_user|
- user = product_user.user
%tr
%th= t(".actions")
%th= User.human_attribute_name(:name)
%th= t(".date_added")
%th= User.human_attribute_name(:username)
%th= User.human_attribute_name(:email)
- if local_assigns[:f]
%th= ProductAccessGroup.model_name.human
%tbody
- @product_users.each do |product_user|
- user = product_user.user
%tr
%td.centered
= link_to t("shared.remove"),
[current_facility, @product, user],
method: :delete,
data: { confirm: text("confirm_removal", product_type: @product.model_name.human.downcase) }
%td.centered
= link_to t("shared.remove"),
[current_facility, @product, user],
method: :delete,
data: { confirm: text("confirm_removal", product_type: @product.model_name.human.downcase) }

%td= link_to(user.last_first_name, [current_facility, user])
%td= month_day_year(product_user.approved_at)
%td= user.username
%td= user.email
- if local_assigns[:f]
%td
= f.fields_for :product_users, product_user, index: product_user.id do |pu|
= pu.select :product_access_group_id,
options_from_collection_for_select(@product.product_access_groups, "id", "name", product_user.product_access_group_id),
include_blank: true
%td= link_to(user.last_first_name, [current_facility, user])
%td= month_day_year(product_user.approved_at)
%td= user.username
%td= user.email
- if local_assigns[:f]
%td
= f.fields_for :product_users, product_user, index: product_user.id do |pu|
= pu.select :product_access_group_id,
options_from_collection_for_select(@product.product_access_groups, "id", "name", product_user.product_access_group_id),
include_blank: true

= will_paginate(@product_users)
= will_paginate(@product_users)
41 changes: 30 additions & 11 deletions app/views/product_users/_users_results.html.haml
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
- if @product_users.nil?
- elsif @product_users.empty?
-if @search_action_performed
:javascript
$(document).ready(function() {
$("form#ajax_form").submit(submitAjaxForm);
})

#result
- if @product_users.present? || @search_action_performed
-# There are users to search for or the search action was performed
= form_tag polymorphic_path([:search, current_facility, @product, :users]), id: "ajax_form", class: "inline", method: :get do
= label_tag :access_list_search, text("search_form.label")
= text_field_tag :search, nil, size: 50, class: "search-query", id: "access_list_search", value: @search_action_performed
= submit_tag text("search_form.button"), class: "btn"

- if @product_users.nil?
- # Access list not required for this product
- elsif @product_users.empty? && @search_action_performed
- # search results returned empty
%p.notice= text("no_results")
- else
- elsif @product_users.empty?
- # no users have access to this product, no search was performed
%p.notice= text("empty_access_list")
- else
- if @product.has_product_access_groups?
= form_for @product, url: [current_facility, @product, :update_restrictions], method: :put do |f|
= render "table", f: f
= f.submit text("update", plural_label: ProductAccessGroup.model_name.human.pluralize),
class: ["btn", "btn-primary"]
- else
= render "table"
- if @search_action_performed.blank?
-# Search action wasn't performed or it was performed with the empty string
= render "export_csv"

- if @product.has_product_access_groups?
= form_for @product, url: [current_facility, @product, :update_restrictions], method: :put do |f|
= render "table", f: f
= f.submit text("update", plural_label: ProductAccessGroup.model_name.human.pluralize),
class: ["btn", "btn-primary"]
- else
= render "table"
12 changes: 0 additions & 12 deletions app/views/product_users/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,4 @@
= text("import_button")
%span{ style:"display:none" }= f.file_field :file, onchange: "form.submit()"

- if @product_users.present?
= form_tag polymorphic_path([current_facility, @product, :users], format: :csv), method: :get, class: "search_form" do
= hidden_field_tag :email, current_user.email, disabled: true
= hidden_field_tag :format, params[:format], disabled: true

= link_to t("reports.product_users.export"), url_for(format: :csv), class: "js--exportSearchResults pull-right", data: { form: ".search_form", action: "export_csv" }

- if @product_users.present? || @search_action_performed
= form_tag polymorphic_path([:search, current_facility, @product, :users]), id: "ajax_form", method: :get, data: { "hide-target": "data-action='export_csv'" } do
= label_tag :access_list_search, text("search_form.label")
= text_field_tag :search, nil, size: 50, class: "search-query", id: "access_list_search"
= submit_tag text("search_form.button"), class: "btn"
= render "users_results"
6 changes: 3 additions & 3 deletions config/locales/views/admin/en.product_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ en:
reservation or purchase. Users can be approved individually or a list can be imported.
When importing, create a CSV file with a single column titled ‘Username’
(!sso_id_downcase! or email). New users are added to the existing access list.
search_form:
button: Search
label: Search by name, or username
users_results:
no_results: No users found.
empty_access_list: No users are currently approved.
search_form:
button: Search
label: Search by name, or username

table:
confirm_removal: |
Expand Down

0 comments on commit 48973a3

Please sign in to comment.