Skip to content

Commit

Permalink
[#3090] remove hard-coded pre-check validation path
Browse files Browse the repository at this point in the history
  • Loading branch information
danielabar committed Jun 1, 2024
1 parent 1e8a882 commit b22fe0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions app/javascript/controllers/distribution_confirmation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { Controller } from "@hotwired/stimulus"
* Displays a confirmation modal with the details of the Distribution form.
* Launched when the user clicks Save from the Distribution Form.
* First runs a "pre-check" on the form data to a validation endpoint.
* If the pre-check passes, it shows the modal.
* First runs a "pre-check" on the form data to a validation endpoint,
* which is specified in the controller's `preCheckPathValue` property.
* If the pre-check passes, it shows the modal. Because the confirmation modal should only be shown
* when the form data can pass initial validation.
* If the pre-check fails, it submits the form to the server for full validation and render with the errors.
* Shows the user what they're about to submit, including items and quantities to distribute.
* The modal shows the user what they're about to submit, including items and quantities to distribute.
* If the user clicks the "Yes..." button from the modal, it submits the form.
* If the user clicks the "No..." button from the modal, it closes and user remains on the same url.
*/
Expand All @@ -26,6 +28,10 @@ export default class extends Controller {
"tbody"
]

static values = {
preCheckPath: String
}

openModal(event) {
event.preventDefault();
this.debugFormData();
Expand All @@ -34,7 +40,7 @@ export default class extends Controller {
const formObject = this.buildNestedObject(formData);

// TODO: #3090 - does fetch work in FF and Safari?
fetch("/distributions/validate.json", {
fetch(this.preCheckPathValue, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -57,6 +63,7 @@ export default class extends Controller {
.catch((error) => {
// Something went wrong trying to talk to server validation endpoint
// In this case, just submit the form as if the user had clicked Save.
// NICE TO HAVE: Send to bugsnag but need to install/configure https://www.npmjs.com/package/@bugsnag/js
console.log(`=== DistributionConfirmationController ERROR ${error}`);
this.formTarget.requestSubmit();
});
Expand Down
4 changes: 3 additions & 1 deletion app/views/distributions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
<!-- form start -->
<div class="card-body">
<!-- Default box -->
<div class="box" data-controller="distribution-confirmation">
<div class="box"
data-controller="distribution-confirmation"
data-distribution-confirmation-pre-check-path-value="<%= validate_distributions_path(format: :json) %>">
<%= render 'form', distribution: @distribution, date_place_holder: Time.zone.now.end_of_day %>
</div><!-- /.box -->
</div>
Expand Down

0 comments on commit b22fe0c

Please sign in to comment.