Skip to content

Commit

Permalink
feat: remove passenger fields
Browse files Browse the repository at this point in the history
TODO: disallow removal of 0th passenger, set max to flight.available_seats
  • Loading branch information
sean-garwood committed Nov 12, 2024
1 parent d0aec92 commit 18fd830
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/javascript/controllers/passenger_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ export default class extends Controller {
newField.innerHTML = newField.innerHTML.replace(/\[[0-9]*\]/g, `[${newIndex}]`).replace(/_[0-9]*_/g, `_${newIndex}_`);
this.fieldsTarget.insertAdjacentElement('beforebegin', newField);
}

remove(event) {
event.preventDefault()
const lastIndex = parseInt(this.fieldsTarget.querySelector('input').name.match(/\d+/)[0]);
// if lastIndex == 0, flash error--we don't want to remove last set of fields
if (lastIndex == 0) {
alert("You must have at least one passenger.");
return;
};

this.fieldsTarget.remove();
}
}
1 change: 1 addition & 0 deletions app/views/bookings/_passenger_info.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</div>
<% end %>
<button type="button" data-action="passenger#add">Add Passenger</button>
<button type="button" data-action="passenger#remove">Remove Passenger</button>
</div>
<%= booking_form.submit "Book Flight" %>
<% end %>

0 comments on commit 18fd830

Please sign in to comment.