Skip to content

Commit 1a15828

Browse files
Merge pull request #5538 from jparcill/jparcill/duplicate-items-stimulus-controller
Generalize duplicate modal and use it for kits
2 parents 3b24d37 + 5207853 commit 1a15828

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

app/javascript/controllers/audit_duplicates_controller.js renamed to app/javascript/controllers/duplicate_items_controller.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Controller } from "@hotwired/stimulus"
22

33
export default class extends Controller {
4+
static targets = ["itemSubmitButton"]
5+
46
connect() {
57
this.boundHandleSubmit = this.handleSubmit.bind(this)
68
this.element.addEventListener("submit", this.boundHandleSubmit)
@@ -18,11 +20,7 @@ export default class extends Controller {
1820
handleSubmit(event) {
1921
const submitter = event.submitter
2022

21-
if (!submitter?.name) return
22-
if (!submitter.name.includes('save_progress') &&
23-
!submitter.name.includes('confirm_audit')) {
24-
return
25-
}
23+
if (!this.itemSubmitButtonTargets.includes(submitter)) return
2624

2725
event.preventDefault()
2826

app/views/audits/_form.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="box-header with-border">
1414
</div>
1515
<div class="box-body">
16-
<%= simple_form_for @audit, data: { controller: "form-input audit-duplicates" }, html: {class: "storage-location-required"} do |f| %>
16+
<%= simple_form_for @audit, data: { controller: "form-input duplicate-items" }, html: {class: "storage-location-required"} do |f| %>
1717
<%= render partial: "storage_locations/source", object: f, locals: { label: "Storage location", error: "What storage location are you auditing?", include_omitted_items: true } %>
1818
<fieldset style="margin-bottom: 2rem;">
1919
<legend>Items in this audit</legend>
@@ -33,8 +33,9 @@
3333
<div class="card-footer">
3434
<%= submit_button({ text: 'Confirm Audit', name: 'confirm_audit', icon: 'check-circle', align: 'left', size: 'md'}, {
3535
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.",
36+
"duplicate-items-target": "itemSubmitButton"
3637
}) %>
37-
<%= submit_button({text: 'Save Progress', name: 'save_progress', size: 'md'}) %>
38+
<%= submit_button({text: 'Save Progress', name: 'save_progress', size: 'md'}, {"duplicate-items-target": "itemSubmitButton"}) %>
3839
</div>
3940
<% end %>
4041
</div>

app/views/kits/_form.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input" }, html: { class: 'form-horizontal' } do |f| %>
1+
<%= simple_form_for @kit, remote: request.xhr?, data: { controller: "form-input duplicate-items" }, html: { class: 'form-horizontal' } do |f| %>
22
<section class="content">
33
<div class="container-fluid">
44
<div class="row">
@@ -36,7 +36,7 @@
3636
</div>
3737
<div class="card-footer">
3838
<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>
39-
<%= submit_button %>
39+
<%= submit_button({}, {"duplicate-items-target": "itemSubmitButton"}) %>
4040
</div>
4141
</div>
4242
</div>

spec/system/kit_system_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,48 @@
226226
expect(page).to have_content(item.name)
227227
end
228228
end
229+
230+
describe "when duplicate items" do
231+
it "detects duplicate items and shows modal", js: true do
232+
visit new_kit_path
233+
click_link "New Kit"
234+
235+
kit_traits = attributes_for(:kit)
236+
fill_in "Name", with: kit_traits[:name]
237+
find(:css, '#kit_value_in_dollars').set('10.10')
238+
239+
item = Item.last
240+
241+
# Add first entry for the item
242+
select item.name, from: "item_line_items_attributes_0_item_id"
243+
fill_in "item_line_items_attributes_0_quantity", with: "10"
244+
245+
# Add a new line item row
246+
find("[data-form-input-target='addButton']").click
247+
248+
# Add second entry for the same item
249+
within all('.line_item_section').last do
250+
item_select = find('select[name*="[item_id]"]')
251+
select item.name, from: item_select[:id]
252+
quantity_input = find('input[name*="[quantity]"]')
253+
fill_in quantity_input[:id], with: "15"
254+
end
255+
256+
# Try to save - should trigger duplicate detection modal
257+
click_button "Save"
258+
259+
# JavaScript modal should appear
260+
expect(page).to have_css("#duplicateItemsModal", visible: true)
261+
expect(page).to have_content("Multiple Item Entries Detected")
262+
expect(page).to have_content("Merge Items")
263+
expect(page).to have_content("Make Changes")
264+
265+
# Test merge functionality
266+
click_button "Merge Items"
267+
268+
expect(page.find(".alert")).to have_content "Kit created successfully"
269+
expect(page).to have_content(kit_traits[:name])
270+
expect(page).to have_content("25 #{item.name}")
271+
end
272+
end
229273
end

0 commit comments

Comments
 (0)