ticket-modal.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. class TicketModal {
  2. constructor() {
  3. this.$modal = $("#ticket-modal");
  4. this.modal = bootstrap.Modal.getInstance(this.$modal.get(0));
  5. this.$modal.on("show.bs.modal", event => this.onShow(event));
  6. this.$modal.on("shown.bs.modal", () => this.onShown());
  7. this.$modal.on("hidden.bs.modal", () => this.onHidden());
  8. this.$title = this.$modal.find(".modal-title");
  9. this.$form = this.$modal.find("form");
  10. this.$form.on("submit", event => this.onSubmit(event));
  11. this.$subjectField = this.$form.find("[name=subject]");
  12. this.$locationDropdown = this.$form.find("[name=location_id]");
  13. }
  14. // Modal lifecycle
  15. onShow(event) {
  16. const $element = $(event.relatedTarget);
  17. const data = $element.data();
  18. }
  19. onShown() {
  20. this.$subjectField.trigger("focus");
  21. fetch("/locations/get-options").then(body => {
  22. return body.json()
  23. }).then(response => {
  24. const $options = response.options.map(option => {
  25. const $option = $(`<option value="${option.value}">`);
  26. $option.text(option.display);
  27. return $option;
  28. });
  29. this.$locationDropdown.append($options);
  30. });
  31. }
  32. onHidden() {
  33. this.$form.get(0).reset();
  34. this.$locationDropdown.find("option[value!='']").remove();
  35. }
  36. onSubmit(event) {
  37. event.preventDefault();
  38. $.ajax({
  39. url: "/tickets/create",
  40. type: "POST",
  41. data: new FormData(this.$form.get(0)),
  42. processData: false,
  43. contentType: false,
  44. success: response => {
  45. this.$modal.modal("hide");
  46. window.location = `/tickets/${response.id}`;
  47. }
  48. })
  49. }
  50. }
  51. $(() => new TicketModal());