product-form.js 716 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class ProductForm {
  2. constructor() {
  3. this.$supplierField = $("#supplier-id");
  4. this.$typeField = $("#type");
  5. this.$skuField = $("#sku");
  6. this.$supplierField.on("change", () => this.autoFillSku());
  7. this.$typeField.on("change", () => this.autoFillSku());
  8. this.autoFillSku();
  9. }
  10. get isNew() {
  11. return $("#new").val() === "1";
  12. }
  13. autoFillSku() {
  14. if(this.$skuField.val() && !this.isNew) {
  15. return;
  16. }
  17. $.post("/api/suggest-sku", {
  18. supplier_id: this.$supplierField.val(),
  19. product_type: this.$typeField.val(),
  20. }).then(response => {
  21. if(response.sku) {
  22. this.$skuField.val(response.sku);
  23. }
  24. }).catch(error => {
  25. this.$skuField.val("");
  26. });
  27. }
  28. }
  29. $(() => new ProductForm());