sale-page.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. class SalePage {
  2. static InitForm() {
  3. const $form = $("#sale-form");
  4. $form.on("submit", event => {
  5. event.preventDefault();
  6. SalePage.SaveForm();
  7. })
  8. }
  9. static InitSaleItemSection() {
  10. const $table = $("#sale-items-section table");
  11. const $checkboxes = $table.find("tbody input[type=checkbox]");
  12. const $allCheckbox = $("#sale-items-all-checkbox");
  13. const $batchActions = $(".batch-action");
  14. $allCheckbox.on("change", () => {
  15. const checked = $allCheckbox.prop("checked");
  16. $checkboxes.prop("checked", checked).trigger("change");
  17. });
  18. $checkboxes.on("change", () => {
  19. let batchActionsEnabled = $checkboxes.filter(":checked").length > 0;
  20. $batchActions.prop("disabled", !batchActionsEnabled);
  21. });
  22. $table.find(".btn-delete-sale-item").on("click", event => {
  23. const saleItemId = $(event.currentTarget).data("sale-item-id");
  24. const confirmed = confirm("Are you sure you want to delete this lead item?");
  25. if(confirmed) {
  26. $.post(window.location + "/delete-item", {
  27. sale_item_id: saleItemId
  28. }).then(response => {
  29. toastr.success("Lead item deleted successfully.");
  30. $("#sale-items-section").html(response.html);
  31. SalePage.InitSaleItemSection();
  32. });
  33. }
  34. });
  35. }
  36. static SaveForm() {
  37. const $form = $("#sale-form");
  38. const action = $form.get(0).action;
  39. const data = $form.serialize();
  40. const $submitElements = $form.find("[type=submit]");
  41. $submitElements.prop("disabled", true);
  42. return $.post(action, data).always(() => {
  43. $submitElements.prop("disabled", false);
  44. }).then(() => {
  45. toastr.success("Lead saved successfully.");
  46. });
  47. }
  48. }
  49. $(() => {
  50. SalePage.InitSaleItemSection();
  51. SalePage.InitForm();
  52. });