get_my/client/AdminMgmt/ProductMgmt/prodMgmtForm.js

163 lines
5.1 KiB
JavaScript
Raw Normal View History

import { Products } from '../../../imports/api/products.js';
import { Stores } from '../../../imports/api/stores.js';
Template.prodMgmtForm.onCreated(function() {
this.subscribe("myProducts");
this.subscribe("storeInfo");
this.subscribe("myCategories");
this.subscribe("myLocations");
});
Template.prodMgmtForm.onRendered(function() {
Meteor.setTimeout(function() {
$('select').formSelect();
}, 200);
$('select').formSelect();
$('.modal').modal();
Session.set("prodEditMode", false);
});
Template.prodMgmtForm.helpers({
stores: function() {
return Stores.find({});
},
prodNameErr: function() {
return Session.get("prodNameRed");
},
prodEditMode: function() {
return Session.get("prodEditMode");
},
});
Template.prodMgmtForm.events({
'click .saveProdMgmt' (event) {
event.preventDefault();
let name = $("#prodName").val();
let store = $("#prodStore").val();
if (store == null) {
store = "";
}
if (name == "" || name == null) {
Session.set("prodNameRed", true);
return;
} else {
2024-07-06 11:13:35 -05:00
Meteor.call('add.product', name, store, function(err, result) {
if (err) {
// console.log(" ERROR: can't add product: " + err);
} else {
// console.log(" SUCCESS adding product.");
$("#prodName").val("");
$("#prodStore").val("");
$('select').formSelect();
}
});
}
},
'click .editProdMgmt' (event) {
event.preventDefault();
let name = $("#prodName").val();
2024-07-06 11:13:35 -05:00
let location = $("#prodLocation").val();
let prodId = Session.get("prodEditId");
if (cat == null) {
cat = "";
}
if (location == null) {
location = "";
}
if (store == null) {
store = "";
}
if (name == "" || name == null) {
Session.set("prodNameRed", true);
return;
} else {
Meteor.call('edit.product', prodId, name, cat, store, location, function(err, result) {
if (err) {
// console.log(" ERROR: can't add product: " + err);
} else {
// console.log(" SUCCESS adding product.");
$("#prodName").val("");
$("#prodStore").val("");
$('select').formSelect();
Session.set("prodEditMode", false);
}
});
}
},
'keypress form.prodInputForm' (event) {
// event.preventDefault();
if (event.which === 13) {
let name = $("#prodName").val();
let store = $("#prodStore").val();
let prodId = Session.get("prodEditId");
let prodEditMode = Session.get("prodEditMode");
console.log(" ---- got the submit event for products.");
if (cat == null) {
cat = "";
}
if (location == null) {
location = "";
}
if (store == null) {
store = "";
}
if (name == "" || name == null) {
Session.set("prodNameRed", true);
return;
} else {
if (prodEditMode == true) {
Meteor.call('edit.product', prodId, name, cat, store, location, function(err, result) {
if (err) {
// console.log(" ERROR: can't add product: " + err);
} else {
// console.log(" SUCCESS adding product.");
$("#prodName").val("");
$("#prodStore").val("");
$('select').formSelect();
}
});
} else {
Meteor.call('add.product', name, cat, store, location, function(err, result) {
if (err) {
// console.log(" ERROR: can't add product: " + err);
} else {
// console.log(" SUCCESS adding product.");
$("#prodName").val("");
$("#prodStore").val("");
$('select').formSelect();
}
});
}
}
}
},
'click .cancelProdMgmt' (event) {
event.preventDefault();
$("#prodName").val("");
$("#prodStore").val("");
$('select').formSelect();
},
'change #prodStore' (event) {
event.preventDefault();
let val = $("#prodStore").val();
if (val == "addNewStore") {
$("#prodStore").val("");
// open a modal to enter store information.
$('#modalStore').modal('open');
}
},
'click .modal-close' (event) {
$('select').formSelect();
}
});