ticket-modal.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.getField("subject");
  12. this.$locationRow = $("#location-row");
  13. this.$locationDropdown = this.getField("location_id");
  14. this.$saleNumberField = this.getField("sale_number");
  15. this.$aruNumberField = this.getField("aru_number");
  16. this.$noLocationAction = $("#no-location-action");
  17. this.$noLocationAction.on("click", event => {
  18. event.preventDefault();
  19. this.onNoLocation();
  20. });
  21. this.$noLocationRow = $("#no-location-row");
  22. this.$locationDropdown.select2({
  23. theme: "bootstrap-5",
  24. dropdownParent: this.$locationDropdown.closest("div")
  25. });
  26. }
  27. getField(name) {
  28. return this.$form.find(`[name=${name}]`);
  29. }
  30. // Modal lifecycle
  31. onShow(event) {
  32. const $element = $(event.relatedTarget);
  33. const data = $element.data();
  34. const { locationId, saleNumber } = data;
  35. this.locationId = locationId;
  36. if(saleNumber) {
  37. this.$saleNumberField.val(saleNumber);
  38. }
  39. }
  40. onShown() {
  41. this.$subjectField.trigger("focus");
  42. fetch("/locations/get-options").then(body => {
  43. return body.json()
  44. }).then(response => {
  45. const $options = response.options.map(option => {
  46. const $option = $(`<option value="${option.value}">`);
  47. $option.text(option.display);
  48. return $option;
  49. });
  50. this.$locationDropdown.append($options);
  51. if(this.locationId) {
  52. this.$locationDropdown.val(this.locationId);
  53. }
  54. });
  55. }
  56. onHidden() {
  57. this.$form.get(0).reset();
  58. this.$locationDropdown.find("option[value!='']").remove();
  59. this.$locationRow.slideDown("fast").find("select").prop("required", true);
  60. this.$noLocationRow.slideUp("fast").find("select, input").prop("required", false);
  61. }
  62. onNoLocation() {
  63. this.$locationRow.slideUp("fast").find("select, input").prop("required", false);
  64. this.$noLocationRow.slideDown("fast").find("select, input").prop("required", true);
  65. }
  66. onSubmit(event) {
  67. event.preventDefault();
  68. $.ajax({
  69. url: "/tickets/create",
  70. type: "POST",
  71. data: new FormData(this.$form.get(0)),
  72. processData: false,
  73. contentType: false,
  74. success: response => {
  75. this.$modal.modal("hide");
  76. window.location = `/tickets/${response.id}`;
  77. }
  78. })
  79. }
  80. }
  81. $(() => new TicketModal());