diff --git a/app/javascript/controllers/audit_duplicates_controller.js b/app/javascript/controllers/duplicate_items_controller.js
similarity index 97%
rename from app/javascript/controllers/audit_duplicates_controller.js
rename to app/javascript/controllers/duplicate_items_controller.js
index 24c8c0b4e1..f341a20c61 100644
--- a/app/javascript/controllers/audit_duplicates_controller.js
+++ b/app/javascript/controllers/duplicate_items_controller.js
@@ -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)
@@ -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()
diff --git a/app/views/audits/_form.html.erb b/app/views/audits/_form.html.erb
index e2b5b4e187..9a44e4b814 100644
--- a/app/views/audits/_form.html.erb
+++ b/app/views/audits/_form.html.erb
@@ -13,7 +13,7 @@
- <%= 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 } %>
diff --git a/app/views/kits/_form.html.erb b/app/views/kits/_form.html.erb
index e5fe5ccac3..94963c311f 100644
--- a/app/views/kits/_form.html.erb
+++ b/app/views/kits/_form.html.erb
@@ -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| %>
diff --git a/spec/system/kit_system_spec.rb b/spec/system/kit_system_spec.rb
index 840d8df3cb..20d5c93f6b 100644
--- a/spec/system/kit_system_spec.rb
+++ b/spec/system/kit_system_spec.rb
@@ -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