ticket-modal.js 2.5 KB

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