-
-
Notifications
You must be signed in to change notification settings - Fork 585
Add confirmation step on kit creation (fixes #5566) #5630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rodrigovirgilio
wants to merge
1
commit into
rubyforgood:main
Choose a base branch
from
rodrigovirgilio:add-kit-creation-confirmation-modal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| /** | ||
| * Connects to data-controller="kit-confirmation" on the new/edit Kit form. | ||
| * Shows a preview of the kit's name, value, and item composition when the | ||
| * user clicks Save, before the form is actually submitted. Composed with | ||
| * the (shared, kit-agnostic) duplicate-items controller: confirming here | ||
| * re-submits the form so duplicate-items can run its own check afterward. | ||
| */ | ||
| export default class extends Controller { | ||
| static targets = ["submitButton"] | ||
|
|
||
| openModal(event) { | ||
| const submitter = event.currentTarget | ||
|
|
||
| if (!this.submitButtonTargets.includes(submitter)) return | ||
|
|
||
| event.preventDefault() | ||
|
|
||
| this.showConfirmationModal(submitter) | ||
| } | ||
|
|
||
| collectLineItems() { | ||
| const items = [] | ||
|
|
||
| this.element.querySelectorAll('select[name*="[item_id]"]').forEach(select => { | ||
| const itemId = select.value | ||
| const itemText = select.options[select.selectedIndex]?.text | ||
| const section = select.closest('.line_item_section') | ||
| const quantityInput = section?.querySelector('input[name*="[quantity]"]') | ||
| const quantity = parseInt(quantityInput?.value) || 0 | ||
|
|
||
| if (!itemId || itemText === "Choose an item" || quantity === 0) return | ||
|
|
||
| items.push({ name: itemText, quantity }) | ||
| }) | ||
|
|
||
| return items | ||
| } | ||
|
|
||
| showConfirmationModal(submitter) { | ||
| const name = this.element.querySelector('#kit_name')?.value || '' | ||
| const value = parseFloat(this.element.querySelector('#kit_value_in_dollars')?.value || 0).toFixed(2) | ||
| const items = this.collectLineItems() | ||
|
|
||
| const itemRows = items.map(item => | ||
| `<tr><td>${item.name}</td><td>${item.quantity}</td></tr>` | ||
| ).join('') | ||
|
|
||
| const modalHtml = ` | ||
| <div class="modal fade" id="kitConfirmationModal" tabindex="-1"> | ||
| <div class="modal-dialog modal-lg"> | ||
| <div class="modal-content"> | ||
| <div class="modal-header"> | ||
| <h5 class="modal-title">Kit Creation Confirmation</h5> | ||
| <button type="button" class="close" data-bs-dismiss="modal"> | ||
| <span>×</span> | ||
| </button> | ||
| </div> | ||
| <div class="modal-body"> | ||
| <p class="lead">You are about to create a kit named | ||
| <span class="fw-bolder fst-italic" data-testid="kit-confirmation-name">${name}</span> | ||
| with value | ||
| <span class="fw-bolder fst-italic" data-testid="kit-confirmation-value">$${value}</span> | ||
| </p> | ||
| <table class="table"> | ||
| <thead> | ||
| <tr> | ||
| <th>Item Name</th> | ||
| <th>Quantity</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody>${itemRows}</tbody> | ||
| </table> | ||
| <p>Please confirm that this is the correct composition of the kit. Note: You will | ||
| <span class="text-danger fw-bold">not</span> be able to edit the items contained in the kit.</p> | ||
| </div> | ||
| <div class="modal-footer"> | ||
| <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">No, I need to make changes</button> | ||
| <button type="button" class="btn btn-success" id="kitConfirmationYes">Yes, it's correct</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ` | ||
|
|
||
| document.getElementById('kitConfirmationModal')?.remove() | ||
| document.body.insertAdjacentHTML('beforeend', modalHtml) | ||
|
|
||
| const modal = new bootstrap.Modal(document.getElementById('kitConfirmationModal')) | ||
| modal.show() | ||
|
|
||
| document.getElementById('kitConfirmationYes').addEventListener('click', () => { | ||
| modal.hide() | ||
| this.element.requestSubmit(submitter) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you can't use the existing confirmation controller? Could we beef it up a bit so we could use the same workflow?