2022-08-15 18:07:39 -05:00
|
|
|
import { Products } from '../../../imports/api/products.js';
|
|
|
|
|
import { Stores } from '../../../imports/api/stores.js';
|
2024-07-23 12:01:33 -05:00
|
|
|
import { M } from '../../lib/assets/materialize.js';
|
2022-08-15 18:07:39 -05:00
|
|
|
|
|
|
|
|
Template.prodMgmtForm.onCreated(function() {
|
|
|
|
|
this.subscribe("myProducts");
|
|
|
|
|
this.subscribe("storeInfo");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Template.prodMgmtForm.onRendered(function() {
|
2024-07-23 12:01:33 -05:00
|
|
|
setTimeout(function() {
|
|
|
|
|
var elems = document.querySelectorAll('select');
|
|
|
|
|
var instances = M.FormSelect.init(elems, {});
|
2022-08-15 18:07:39 -05:00
|
|
|
}, 200);
|
2024-07-23 12:01:33 -05:00
|
|
|
|
|
|
|
|
var elemm = document.querySelectorAll('.modal');
|
|
|
|
|
var instancem = M.Modal.init(elemm, {});
|
|
|
|
|
|
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({
|
2024-08-13 10:48:27 -05:00
|
|
|
'click .saveProdMgmt, click .editProdMgmt' (event) {
|
2022-08-23 13:41:21 -05:00
|
|
|
let name = $("#prodName").val();
|
2022-08-15 18:07:39 -05:00
|
|
|
let store = $("#prodStore").val();
|
2022-08-23 13:41:21 -05:00
|
|
|
let prodId = Session.get("prodEditId");
|
2024-08-13 10:48:27 -05:00
|
|
|
let prodEditMode = Session.get("prodEditMode");
|
2022-08-23 13:41:21 -05:00
|
|
|
if (store == null) {
|
|
|
|
|
store = "";
|
|
|
|
|
}
|
|
|
|
|
if (name == "" || name == null) {
|
|
|
|
|
Session.set("prodNameRed", true);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
2024-08-13 10:48:27 -05:00
|
|
|
if (prodEditMode == true) {
|
2025-06-21 07:28:59 -05:00
|
|
|
editProd(prodId, name, store);
|
2022-08-23 13:41:21 -05:00
|
|
|
} else {
|
2025-06-21 07:28:59 -05:00
|
|
|
newProd(name, store);
|
2022-08-23 13:41:21 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-08-09 12:03:49 -05:00
|
|
|
'click #showNoStoreSet' (event) {
|
|
|
|
|
let noStoreSet = $("#showNoStoreSet").prop('checked');
|
2025-07-23 19:44:24 -05:00
|
|
|
// console.log("Clicked: " + noStoreSet);
|
2024-08-09 12:03:49 -05:00
|
|
|
if (noStoreSet == true) {
|
|
|
|
|
Session.set("noStoreSet", true);
|
|
|
|
|
} else {
|
|
|
|
|
Session.set("noStoreSet", false);
|
|
|
|
|
}
|
2025-06-21 07:28:59 -05:00
|
|
|
},
|
2025-08-09 07:21:46 -05:00
|
|
|
'click #showNoList' (event) {
|
|
|
|
|
let notUsedInList = $("#showNoList").prop('checked');
|
|
|
|
|
if (notUsedInList == true) {
|
|
|
|
|
Session.set("noListUsed", true);
|
|
|
|
|
} else {
|
|
|
|
|
Session.set("noListUsed", false);
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-06-21 07:28:59 -05:00
|
|
|
'submit .prodInputForm' (event) {
|
|
|
|
|
|
|
|
|
|
},
|
2024-08-09 12:03:49 -05:00
|
|
|
});
|
2025-06-21 07:28:59 -05:00
|
|
|
|
|
|
|
|
const newProd = async(name, store) => {
|
|
|
|
|
let result = await Meteor.callAsync('add.product', name, store);
|
|
|
|
|
try {
|
|
|
|
|
if (!result) {
|
2025-07-23 19:44:24 -05:00
|
|
|
// console.log("No result when trying to add a new product.");
|
2025-06-21 07:28:59 -05:00
|
|
|
showSnackbar("Unable to add new product.");
|
|
|
|
|
} else {
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
showSnackbar("New Product Added!", "green");
|
|
|
|
|
}
|
|
|
|
|
} catch(error) {
|
|
|
|
|
console.log(" ERROR - couldn't add product: " + error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const editProd = async(prodId, name, store) => {
|
|
|
|
|
let result = await Meteor.callAsync('edit.product', prodId, name, store);
|
|
|
|
|
if (!result) {
|
|
|
|
|
console.log(" ERROR: can't add product: " + err);
|
|
|
|
|
} else {
|
|
|
|
|
$("#prodName").val("");
|
|
|
|
|
showSnackbar("Successfully Edited Product!", "green");
|
|
|
|
|
}
|
|
|
|
|
}
|