2022-08-15 18:07:39 -05:00
|
|
|
import { Products } from '../../../imports/api/products.js';
|
|
|
|
|
import { Stores } from '../../../imports/api/stores.js';
|
|
|
|
|
|
|
|
|
|
Template.prodMgmtForm.onCreated(function() {
|
|
|
|
|
this.subscribe("myProducts");
|
|
|
|
|
this.subscribe("storeInfo");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.prodMgmtForm.onRendered(function() {
|
|
|
|
|
Meteor.setTimeout(function() {
|
|
|
|
|
$('select').formSelect();
|
|
|
|
|
}, 200);
|
|
|
|
|
$('select').formSelect();
|
|
|
|
|
$('.modal').modal();
|
2022-08-23 13:41:21 -05:00
|
|
|
Session.set("prodEditMode", false);
|
2022-08-15 18:07:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.prodMgmtForm.helpers({
|
|
|
|
|
stores: function() {
|
|
|
|
|
return Stores.find({});
|
|
|
|
|
},
|
|
|
|
|
prodNameErr: function() {
|
|
|
|
|
return Session.get("prodNameRed");
|
|
|
|
|
},
|
2022-08-23 13:41:21 -05:00
|
|
|
prodEditMode: function() {
|
|
|
|
|
return Session.get("prodEditMode");
|
|
|
|
|
},
|
2022-08-15 18:07:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.prodMgmtForm.events({
|
|
|
|
|
'click .saveProdMgmt' (event) {
|
|
|
|
|
event.preventDefault();
|
2022-08-23 13:41:21 -05:00
|
|
|
let name = $("#prodName").val();
|
2022-08-15 18:07:39 -05:00
|
|
|
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) {
|
2022-08-15 18:07:39 -05:00
|
|
|
if (err) {
|
|
|
|
|
// console.log(" ERROR: can't add product: " + err);
|
|
|
|
|
} else {
|
|
|
|
|
// console.log(" SUCCESS adding product.");
|
2022-08-16 16:46:14 -05:00
|
|
|
$("#prodName").val("");
|
|
|
|
|
$("#prodStore").val("");
|
2022-08-23 13:41:21 -05:00
|
|
|
$('select').formSelect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'click .editProdMgmt' (event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
let name = $("#prodName").val();
|
|
|
|
|
let prodId = Session.get("prodEditId");
|
2024-07-06 11:47:45 -05:00
|
|
|
let store = $("#prodStore").val();
|
2022-08-23 13:41:21 -05:00
|
|
|
|
|
|
|
|
if (store == null) {
|
|
|
|
|
store = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name == "" || name == null) {
|
|
|
|
|
Session.set("prodNameRed", true);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
2024-07-06 11:47:45 -05:00
|
|
|
Meteor.call('edit.product', prodId, name, store, function(err, result) {
|
2022-08-23 13:41:21 -05:00
|
|
|
if (err) {
|
|
|
|
|
// console.log(" ERROR: can't add product: " + err);
|
|
|
|
|
} else {
|
|
|
|
|
// console.log(" SUCCESS adding product.");
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
$("#prodStore").val("");
|
|
|
|
|
$('select').formSelect();
|
2022-08-24 14:03:00 -05:00
|
|
|
Session.set("prodEditMode", false);
|
2022-08-15 18:07:39 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-08-23 13:41:21 -05:00
|
|
|
'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 (store == null) {
|
|
|
|
|
store = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name == "" || name == null) {
|
|
|
|
|
Session.set("prodNameRed", true);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
if (prodEditMode == true) {
|
2024-07-06 11:47:45 -05:00
|
|
|
Meteor.call('edit.product', prodId, name, store, function(err, result) {
|
2022-08-23 13:41:21 -05:00
|
|
|
if (err) {
|
|
|
|
|
// console.log(" ERROR: can't add product: " + err);
|
|
|
|
|
} else {
|
|
|
|
|
// console.log(" SUCCESS adding product.");
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
$("#prodStore").val("");
|
|
|
|
|
$('select').formSelect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-07-06 11:47:45 -05:00
|
|
|
Meteor.call('add.product', name, store, function(err, result) {
|
2022-08-23 13:41:21 -05:00
|
|
|
if (err) {
|
|
|
|
|
// console.log(" ERROR: can't add product: " + err);
|
|
|
|
|
} else {
|
|
|
|
|
// console.log(" SUCCESS adding product.");
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
$("#prodStore").val("");
|
|
|
|
|
$('select').formSelect();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-08-15 18:07:39 -05:00
|
|
|
'click .cancelProdMgmt' (event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
$("#prodStore").val("");
|
2022-08-23 13:41:21 -05:00
|
|
|
$('select').formSelect();
|
2022-08-15 18:07:39 -05:00
|
|
|
},
|
|
|
|
|
'change #prodStore' (event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
let val = $("#prodStore").val();
|
|
|
|
|
|
|
|
|
|
if (val == "addNewStore") {
|
|
|
|
|
$("#prodStore").val("");
|
|
|
|
|
// open a modal to enter store information.
|
|
|
|
|
$('#modalStore').modal('open');
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-08-16 16:46:14 -05:00
|
|
|
'click .modal-close' (event) {
|
|
|
|
|
$('select').formSelect();
|
|
|
|
|
}
|
2022-08-15 18:07:39 -05:00
|
|
|
});
|