Skip to content

Commit f3aa5ff

Browse files
committed
Generalize duplicate modal and use for kits
1 parent 604088d commit f3aa5ff

4 files changed

Lines changed: 55 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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,51 @@
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+
# Disable server-side validation to test JS modal
233+
#allow_any_instance_of(Audit).to receive(:line_items_unique_by_item_id)
234+
visit new_kit_path
235+
click_link "New Kit"
236+
237+
kit_traits = attributes_for(:kit)
238+
fill_in "Name", with: kit_traits[:name]
239+
find(:css, '#kit_value_in_dollars').set('10.10')
240+
241+
item = Item.last
242+
243+
# Add first entry for the item
244+
select item.name, from: "item_line_items_attributes_0_item_id"
245+
fill_in "item_line_items_attributes_0_quantity", with: "10"
246+
247+
# Add a new line item row
248+
find("[data-form-input-target='addButton']").click
249+
250+
# Add second entry for the same item
251+
within all('.line_item_section').last do
252+
item_select = find('select[name*="[item_id]"]')
253+
select item.name, from: item_select[:id]
254+
quantity_input = find('input[name*="[quantity]"]')
255+
fill_in quantity_input[:id], with: "15"
256+
end
257+
258+
# Try to save - should trigger duplicate detection modal
259+
click_button "Save"
260+
261+
# JavaScript modal should appear
262+
expect(page).to have_css("#duplicateItemsModal", visible: true)
263+
expect(page).to have_content("Multiple Item Entries Detected")
264+
expect(page).to have_content("Merge Items")
265+
expect(page).to have_content("Make Changes")
266+
267+
268+
# Test merge functionality
269+
click_button "Merge Items"
270+
271+
expect(page.find(".alert")).to have_content "Kit created successfully"
272+
expect(page).to have_content(kit_traits[:name])
273+
expect(page).to have_content("25 #{item.name}")
274+
end
275+
end
229276
end

0 commit comments

Comments
 (0)