Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["itemSubmitButton"]

connect() {
this.boundHandleSubmit = this.handleSubmit.bind(this)
this.element.addEventListener("submit", this.boundHandleSubmit)
Expand All @@ -18,11 +20,7 @@ export default class extends Controller {
handleSubmit(event) {
const submitter = event.submitter

if (!submitter?.name) return
if (!submitter.name.includes('save_progress') &&
!submitter.name.includes('confirm_audit')) {
return
}
if (!this.itemSubmitButtonTargets.includes(submitter)) return

event.preventDefault()

Expand Down
5 changes: 3 additions & 2 deletions app/views/audits/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="box-header with-border">
</div>
<div class="box-body">
<%= simple_form_for @audit, data: { controller: "form-input audit-duplicates" }, html: {class: "storage-location-required"} do |f| %>
<%= simple_form_for @audit, data: { controller: "form-input duplicate-items" }, html: {class: "storage-location-required"} do |f| %>
<%= render partial: "storage_locations/source", object: f, locals: { label: "Storage location", error: "What storage location are you auditing?", include_omitted_items: true } %>
<fieldset style="margin-bottom: 2rem;">
<legend>Items in this audit</legend>
Expand All @@ -33,8 +33,9 @@
<div class="card-footer">
<%= submit_button({ text: 'Confirm Audit', name: 'confirm_audit', icon: 'check-circle', align: 'left', size: 'md'}, {
confirm: "Are you sure?\n\nPlease note that this audit must also be finalized by someone with organization admin rights before changes to inventory will take place.\n\nWe strongly recommend completing that step before doing any further actions that affect inventory.",
"duplicate-items-target": "itemSubmitButton"
}) %>
<%= submit_button({text: 'Save Progress', name: 'save_progress', size: 'md'}) %>
<%= submit_button({text: 'Save Progress', name: 'save_progress', size: 'md'}, {"duplicate-items-target": "itemSubmitButton"}) %>
</div>
<% end %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/kits/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input" }, html: { class: 'form-horizontal' } do |f| %>
<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input duplicate-items" }, html: { class: 'form-horizontal' } do |f| %>
<section class="content">
<div class="container-fluid">
<div class="row">
Expand Down Expand Up @@ -36,7 +36,7 @@
</div>
<div class="card-footer">
<p><b>NB: You will not be able to change the composition of the kit once saved. Partner visibility and name can be changed via the kit's item.</b></p>
<%= submit_button %>
<%= submit_button({}, {"duplicate-items-target": "itemSubmitButton"}) %>
</div>
</div>
</div>
Expand Down
44 changes: 44 additions & 0 deletions spec/system/kit_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,48 @@
expect(page).to have_content(item.name)
end
end

describe "when duplicate items" do
it "detects duplicate items and shows modal", js: true do
visit new_kit_path
click_link "New Kit"

kit_traits = attributes_for(:kit)
fill_in "Name", with: kit_traits[:name]
find(:css, '#kit_value_in_dollars').set('10.10')

item = Item.last

# Add first entry for the item
select item.name, from: "item_line_items_attributes_0_item_id"
fill_in "item_line_items_attributes_0_quantity", with: "10"

# Add a new line item row
find("[data-form-input-target='addButton']").click

# Add second entry for the same item
within all('.line_item_section').last do
item_select = find('select[name*="[item_id]"]')
select item.name, from: item_select[:id]
quantity_input = find('input[name*="[quantity]"]')
fill_in quantity_input[:id], with: "15"
end

# Try to save - should trigger duplicate detection modal
click_button "Save"

# JavaScript modal should appear
expect(page).to have_css("#duplicateItemsModal", visible: true)
expect(page).to have_content("Multiple Item Entries Detected")
expect(page).to have_content("Merge Items")
expect(page).to have_content("Make Changes")

# Test merge functionality
click_button "Merge Items"

expect(page.find(".alert")).to have_content "Kit created successfully"
expect(page).to have_content(kit_traits[:name])
expect(page).to have_content("25 #{item.name}")
end
end
end
Loading