Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with item name dropdown selection #4883

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def create
@storage_locations = current_organization.storage_locations.active_locations.alphabetized.select do |storage_loc|
inventory.quantity_for(storage_location: storage_loc.id).positive?
end
if @distribution.storage_location.present?
@item_labels_with_quantities = inventory
.items_for_location(@distribution.storage_location.id, include_omitted: true)
.map(&:to_dropdown_option)
end

flash_error = insufficient_error_message(result.error.message)

Expand Down
12 changes: 12 additions & 0 deletions app/javascript/utils/distributions_and_transfers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function new_option(item, selected) {
return content;
};

// Workaround to refresh item dropdown results for select2.
function rerenderDropdown(element) {
const oldScrollTop = element.data('select2').$results.scrollTop();
element.select2('close').select2('open');
element.data('select2').$results.scrollTop(oldScrollTop);
}

function populate_dropdowns(objects, inventory) {
objects.each(function(_, element) {
const selected = Number(
Expand All @@ -40,6 +47,11 @@ function populate_dropdowns(objects, inventory) {
.remove()
.end()
.append(options);
// If this select element is currently open, the option list is
// now stale and needs to be refreshed.
if ($(element).data('select2')?.isOpen()) {
rerenderDropdown($(element))
}
});
}

Expand Down
4 changes: 4 additions & 0 deletions app/models/view/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Inventory
class ViewInventoryItem < EventTypes::EventItem
attribute :db_item, Types::Nominal::Any
delegate(*Item.column_names.map(&:to_sym), to: :db_item)

def to_dropdown_option
Option.new(id: id, name: "#{name} (#{quantity})")
end
end

attr_accessor :inventory, :organization_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/line_items/_line_item_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<span class="li-name w-100">
<%= field.input :item_id,
disabled: requested.present?,
collection: @items, prompt: "Choose an item",
collection: @item_labels_with_quantities || @items, prompt: "Choose an item",
include_blank: "",
label: false,
input_html: { class: "my-0 line_item_name", "data-controller": "select2" } %>
Expand Down
11 changes: 11 additions & 0 deletions spec/requests/distributions_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@
expect(response).to have_error
end

it "renders #new with item quantities in dropdowns listed" do
create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack')

post distributions_path(distribution: distribution.except(:partner_id), format: :turbo_stream)

expect(response).to have_http_status(400)
expect(flash[:error]).to include("Sorry, we weren't able to save the distribution.")

expect(response.body).to include("Item 1 (0)")
end

it "renders #new on failure with only active items in dropdown" do
create(:item, organization: organization, name: 'Active Item')
create(:item, :inactive, organization: organization, name: 'Inactive Item')
Expand Down
Loading