|
1 | 1 | import $ from 'jquery'; |
2 | 2 |
|
3 | 3 | $(() => { |
4 | | - $("button[name='save_progress']").on('click', function (e) { |
5 | | - |
| 4 | + function checkForDuplicates(e, buttonName) { |
6 | 5 | const form = $(this).closest('form'); |
7 | 6 | const itemCounts = {}; // Will look like: { "2": 3, "5": 1, "12": 2 } |
8 | 7 | const itemNames = {}; // Will look like: { "2": "Item A", "5": "Item B", "12": "Item C" } |
@@ -44,15 +43,23 @@ $(() => { |
44 | 43 |
|
45 | 44 | if (duplicates.length > 0) { |
46 | 45 | // Show modal with duplicate items |
47 | | - showDuplicateModal(duplicates, itemQuantities, form); |
| 46 | + showDuplicateModal(duplicates, itemQuantities, form, buttonName); |
48 | 47 | e.preventDefault(); |
49 | 48 | } else { |
50 | | - // No duplicates, let the form submit normally with the save_progress button |
| 49 | + // No duplicates, let the form submit normally |
51 | 50 | // Don't prevent default - let the button's natural submit behavior work |
52 | 51 | } |
| 52 | + } |
| 53 | + |
| 54 | + $("button[name='save_progress']").on('click', function (e) { |
| 55 | + checkForDuplicates.call(this, e, 'save_progress'); |
| 56 | + }); |
| 57 | + |
| 58 | + $("button[name='confirm_audit']").on('click', function (e) { |
| 59 | + checkForDuplicates.call(this, e, 'confirm_audit'); |
53 | 60 | }); |
54 | 61 |
|
55 | | - function showDuplicateModal(duplicateItems, duplicateQuantities, form) { |
| 62 | + function showDuplicateModal(duplicateItems, duplicateQuantities, form, buttonName) { |
56 | 63 | const itemRows = duplicateItems.map(item => { |
57 | 64 | const entries = duplicateQuantities[item.id] || []; |
58 | 65 | const total = entries.reduce((sum, entry) => sum + entry.qty, 0); |
@@ -114,11 +121,11 @@ $(() => { |
114 | 121 | // Merge duplicate items before submitting |
115 | 122 | mergeDuplicateItems(form); |
116 | 123 |
|
117 | | - // Create a hidden button with save_progress name to ensure proper parameter submission |
118 | | - const hiddenBtn = $('<button type="submit" name="save_progress" style="display:none;">Save Progress</button>'); |
| 124 | + // Create a hidden button with the appropriate name to ensure proper parameter submission |
| 125 | + const hiddenBtn = $(`<button type="submit" name="${buttonName}" style="display:none;"></button>`); |
119 | 126 | form.append(hiddenBtn); |
120 | 127 |
|
121 | | - // Click the hidden button to submit with save_progress parameter |
| 128 | + // Click the hidden button to submit with the correct parameter |
122 | 129 | hiddenBtn.click(); |
123 | 130 | }); |
124 | 131 | } |
|
0 commit comments