@@ -23,16 +23,96 @@ export default class extends Controller {
2323 if ( ! this . itemSubmitButtonTargets . includes ( submitter ) ) return
2424
2525 event . preventDefault ( )
26-
26+
27+ this . showConfirmationModal ( submitter . name )
28+ }
29+
30+ checkForDuplicatesAndSubmit ( buttonName ) {
2731 const duplicates = this . findDuplicates ( )
28-
32+
2933 if ( duplicates . length > 0 ) {
30- this . showModal ( duplicates , submitter . name )
34+ this . showDuplicatesModal ( duplicates , buttonName )
3135 } else {
32- this . submitForm ( submitter . name )
36+ this . submitForm ( buttonName )
3337 }
3438 }
3539
40+ collectLineItems ( ) {
41+ const items = [ ]
42+
43+ this . element . querySelectorAll ( 'select[name*="[item_id]"]' ) . forEach ( select => {
44+ const itemId = select . value
45+ const itemText = select . options [ select . selectedIndex ] ?. text
46+ const section = select . closest ( '.line_item_section' )
47+ const quantityInput = section ?. querySelector ( 'input[name*="[quantity]"]' )
48+ const quantity = parseInt ( quantityInput ?. value ) || 0
49+
50+ if ( ! itemId || itemText === "Choose an item" || quantity === 0 ) return
51+
52+ items . push ( { name : itemText , quantity } )
53+ } )
54+
55+ return items
56+ }
57+
58+ showConfirmationModal ( buttonName ) {
59+ const name = this . element . querySelector ( '#kit_name' ) ?. value || ''
60+ const value = parseFloat ( this . element . querySelector ( '#kit_value_in_dollars' ) ?. value || 0 ) . toFixed ( 2 )
61+ const items = this . collectLineItems ( )
62+
63+ const itemRows = items . map ( item =>
64+ `<tr><td>${ item . name } </td><td>${ item . quantity } </td></tr>`
65+ ) . join ( '' )
66+
67+ const modalHtml = `
68+ <div class="modal fade" id="kitConfirmationModal" tabindex="-1">
69+ <div class="modal-dialog modal-lg">
70+ <div class="modal-content">
71+ <div class="modal-header">
72+ <h5 class="modal-title">Kit Creation Confirmation</h5>
73+ <button type="button" class="close" data-bs-dismiss="modal">
74+ <span>×</span>
75+ </button>
76+ </div>
77+ <div class="modal-body">
78+ <p class="lead">You are about to create a kit named
79+ <span class="fw-bolder fst-italic" data-testid="kit-confirmation-name">${ name } </span>
80+ with value
81+ <span class="fw-bolder fst-italic" data-testid="kit-confirmation-value">$${ value } </span>
82+ </p>
83+ <table class="table">
84+ <thead>
85+ <tr>
86+ <th>Item Name</th>
87+ <th>Quantity</th>
88+ </tr>
89+ </thead>
90+ <tbody>${ itemRows } </tbody>
91+ </table>
92+ <p>Please confirm that this is the correct composition of the kit. Note: You will
93+ <span class="text-danger fw-bold">not</span> be able to edit the items contained in the kit.</p>
94+ </div>
95+ <div class="modal-footer">
96+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No, I need to make changes</button>
97+ <button type="button" class="btn btn-success" id="kitConfirmationYes">Yes, it's correct</button>
98+ </div>
99+ </div>
100+ </div>
101+ </div>
102+ `
103+
104+ document . getElementById ( 'kitConfirmationModal' ) ?. remove ( )
105+ document . body . insertAdjacentHTML ( 'beforeend' , modalHtml )
106+
107+ const modal = new bootstrap . Modal ( document . getElementById ( 'kitConfirmationModal' ) )
108+ modal . show ( )
109+
110+ document . getElementById ( 'kitConfirmationYes' ) . addEventListener ( 'click' , ( ) => {
111+ modal . hide ( )
112+ this . checkForDuplicatesAndSubmit ( buttonName )
113+ } )
114+ }
115+
36116 findDuplicates ( ) {
37117 const itemCounts = { }
38118 const itemData = { }
@@ -59,7 +139,7 @@ export default class extends Controller {
59139 . map ( id => itemData [ id ] )
60140 }
61141
62- showModal ( duplicates , buttonName ) {
142+ showDuplicatesModal ( duplicates , buttonName ) {
63143 const itemRows = duplicates . map ( item => {
64144 const entries = item . entries
65145 const total = entries . reduce ( ( sum , entry ) => sum + entry . quantity , 0 )
0 commit comments