diff --git a/client/ListItems/listItemsForm.js b/client/ListItems/listItemsForm.js index 3f947b3..4c2a90e 100644 --- a/client/ListItems/listItemsForm.js +++ b/client/ListItems/listItemsForm.js @@ -2,11 +2,13 @@ import { ListItems } from '../../imports/api/listItems.js' import { Lists } from '../../imports/api/lists.js'; import { Products } from '../../imports/api/products.js'; import { M } from '../lib/assets/materialize.js'; +import { UserLast } from '../../imports/api/userLast.js'; Template.listItemsForm.onCreated(function() { this.subscribe("myListItems", Session.get("listId")); this.subscribe("myLists"); this.subscribe("myProducts"); + this.subscribe("userLastView"); }); Template.listItemsForm.onRendered(function() { @@ -28,9 +30,16 @@ Template.listItemsForm.onRendered(function() { Template.listItemsForm.helpers({ selListName: function() { - let selListId = Session.get("listId"); + let selListId = ""; + if (Session.get("listId")) { + selListId = Session.get("listId"); + } else { + selListId = UserLast.findOne({ view: "List", userId: Meteor.userId() }).viewId; + } let listInfo = Lists.findOne({ _id: selListId }); - return listInfo.listName; + if (listInfo) { + return listInfo.listName; + } }, itemProdName: function() { return Products.find({}); diff --git a/client/ListItems/listItemsTbl.js b/client/ListItems/listItemsTbl.js index 4cc47a1..374c3ba 100644 --- a/client/ListItems/listItemsTbl.js +++ b/client/ListItems/listItemsTbl.js @@ -1,10 +1,12 @@ import { ListItems } from '../../imports/api/listItems.js'; import { M } from '../lib/assets/materialize.js'; +import { UserLast } from '../../imports/api/userLast.js'; Template.listItemsTbl.onCreated(function() { this.autorun( () => { this.subscribe("myListItems", Session.get("listId")); }); + this.subscribe("userLastView"); }); Template.listItemsTbl.onRendered(function() { @@ -13,6 +15,14 @@ Template.listItemsTbl.onRendered(function() { Session.set("showReceivedItems", false); Session.set("searchVal", ""); + + if (Session.get("listId")) { + console.log("got List Id in Session var."); + // no action + } else { + let selListId = UserLast.find({ view: "List" }).listId; + Session.set("listId", selListId); + } }); Template.listItemsTbl.helpers({ diff --git a/client/Lists/listsTbl.js b/client/Lists/listsTbl.js index ff3f07a..1d7a8f0 100644 --- a/client/Lists/listsTbl.js +++ b/client/Lists/listsTbl.js @@ -23,13 +23,19 @@ Template.listsTbl.events({ if (sender.localName == "li" || sender.localName == "span") { let listId = event.currentTarget.id; if (listId == "addList") { - // $('#modalList').modal('open'); + // opens the modal and allows you to add a new List } else { // console.log("listId is: " + listId); Session.set("listId", listId); - Meteor.setTimeout(function() { - FlowRouter.go('/listitems'); - }, 100); + Meteor.call('add.userLast', "List", listId, function(err, result) { + if (err) { + console.log(" ERROR setting user last list id in db: " + err); + } else { + Meteor.setTimeout(function() { + FlowRouter.go('/listitems'); + }, 100); + } + }); } } }, diff --git a/imports/api/userLast.js b/imports/api/userLast.js new file mode 100644 index 0000000..93c12a9 --- /dev/null +++ b/imports/api/userLast.js @@ -0,0 +1,57 @@ +import { Meteor } from 'meteor/meteor'; +import { Mongo } from 'meteor/mongo'; +import { check } from 'meteor/check'; + +export const UserLast = new Mongo.Collection('userLast'); + +UserLast.allow({ + insert: function(userId, doc){ + // if use id exists, allow insert + return !!userId; + }, +}); + +Meteor.methods({ + 'add.userLast' (view, viewId) { + check(view, String); + check(viewId, String); + + if (!this.userId) { + throw new Meteor.Error('Not able to change user view last setting. Make sure you are logged in with valid system administrator credentials.'); + } + + // first let's find out if there's an entry for this user and view, and if so + // we'll just edit that entry with updated information + let userListInfo = UserLast.findOne({ userId: this.userId, view: view }); + if (typeof userListInfo != 'undefined' && userListInfo != "" && userListInfo != null) { + // entry exists, call the edit function instead + Meteor.call('edit.userLast', view, viewId, function(err, result) { + if (err) { + console.log(" ERROR moving user to edit for last view: " + err); + } + }); + } else { + return UserLast.insert({ + userId: this.userId, + view: view, + viewId: viewId, + dateAdded: Date(), + }); + } + }, + 'edit.userLast' (view, viewId) { + check(view, String); + check(viewId, String); + + if (!this.userId) { + throw new Meteor.Error('Not able to change user view last setting. Make sure you are logged in with valid system administrator credentials.'); + } + + return UserLast.update({ view: view, userId: this.userId }, { + $set: { + viewId: viewId, + dateLastUpdate: Date(), + } + }); + } +}); \ No newline at end of file diff --git a/server/publish.js b/server/publish.js index 3c4c6b1..c645127 100644 --- a/server/publish.js +++ b/server/publish.js @@ -10,6 +10,7 @@ import moment from 'moment'; import { TaskItems } from '../imports/api/tasks.js'; import { UserConfig } from '../imports/api/userConfig.js'; import { MenuProdLinks } from '../imports/api/menuProdLinks.js'; +import { UserLast } from '../imports/api/userLast.js'; Meteor.publish("SystemConfig", function() { try { @@ -31,6 +32,14 @@ Meteor.publish('userList', function() { return Meteor.users.find({}); }); +Meteor.publish("userLastView", function() { + try { + return UserLast.find({ userId: this.userId }); + } catch (error) { + console.log(" ---- ERROR pulling user last info: " + error); + } +}); + Meteor.publish("storeInfo", function() { try { return Stores.find({});