open-assets/client/Locations/locations.js
2026-01-27 12:42:20 -06:00

92 lines
No EOL
3.1 KiB
JavaScript

import { Roles } from 'meteor/roles';
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
import { Locations } from '../../imports/api/locations.js';
import { LocationTypes } from '../../imports/api/locationTypes.js';
Template.locations.onCreated(function () {
this.subscribe("LocationTypes");
this.subscribe("Locations");
});
Template.locations.onRendered(function () {
Session.set("isChild", false);
});
Template.locations.helpers({
isChild: function () {
return Session.get("isChild");
},
locations: function () {
return Locations.find({});
},
locTypes: function () {
return LocationTypes.find({});
}
});
Template.locations.events({
'change #locationIsChild'(event) {
console.log("Clicked.");
let isChild = $("#locationIsChild").prop('checked');
if (isChild) {
Session.set("isChild", true);
} else {
Session.set("isChild", false);
}
},
'click #saveLocation'(event) {
let locName = $("#locationName").val();
let locDesc = $("#locationDesc").val();
let locType = $("#locationType").val();
let isChild = $("#locationIsChild").prop("checked");
let parentLocation = $("#parentLocation").val();
let parentInfo = [];
let parentName = "";
let parentId = "";
let locTypeParts = locType.split(' | ');
let locTypeName = locTypeParts[0];
let locTypeId = locTypeParts[1];
if (isChild == true) {
// split the parent location name details
if (parentLocation == "" || parentLocation == null) {
showSnackbar("Parent Location is Required!", "red");
return;
} else {
parentInfo = parentLocation.split(' | ');
parentName = parentInfo[0];
parentId = parentInfo[1];
}
}
if (locName == null || locName == "") {
showSnackbar("Location Name is Required!", "red");
return;
} else if (locType == "" || locType == null) {
showSnackbar("Location Type is Required!", "red");
return;
} else {
let addLocation = async () => {
try {
const result = await Meteor.callAsync('add.location', locName, locDesc, locTypeName, locTypeId, isChild, parentName, parentId);
if (!result) {
showSnackbar("Could not add location!", "red");
return;
} else {
showSnackbar("Location Successfuly Added!", "green");
$("#locationName").val("");
$("#locationDesc").val("");
$("#locationType").val("");
$("#isChild").prop('checked', false);
$("#parentLocation").val("");
}
} catch (error) {
console.log(error);
}
}
addLocation();
}
}
});