Fixing add task calls for async await

This commit is contained in:
Brian McGonagill 2025-07-22 08:34:24 -05:00
parent ca7bcb1a8f
commit febb36d75f
5 changed files with 623 additions and 484 deletions

View file

@ -3,7 +3,7 @@
<form class="row" style="gap: 1em;"> <form class="row" style="gap: 1em;">
<div class="col s12 m6 l4 chips chips-placeholder" id="taskName"> <div class="col s12 m6 l4 chips chips-placeholder" id="taskName">
</div> </div>
<div class="col s12 m6 l4 input-field outlined"> <div class="col s12 m6 l4 input-field">
<select name="taskUser" id="taskUser" class="taskUser"> <select name="taskUser" id="taskUser" class="taskUser">
<option value="" disabled selected>Assign to user...</option> <option value="" disabled selected>Assign to user...</option>
{{#each taskUsers}} {{#each taskUsers}}
@ -11,7 +11,7 @@
{{/each}} {{/each}}
</select> </select>
</div> </div>
<div class="col s12 m6 l4 input-field outlined"> <div class="col s12 m6 l4 input-field">
<input type="text" class="datepicker" id="taskDate" /> <input type="text" class="datepicker" id="taskDate" />
<label for="taskDate">Task Date (multiple entries)</label> <label for="taskDate">Task Date (multiple entries)</label>
<div class="row"> <div class="row">

View file

@ -69,7 +69,6 @@ Template.taskForm.events({
let taskDateErr = false; let taskDateErr = false;
let userInfo; let userInfo;
let actDate = []; let actDate = [];
// console.dir(taskNameArr);
if (taskNameArr == null || taskNameArr == []) { if (taskNameArr == null || taskNameArr == []) {
taskNameErr = true; taskNameErr = true;

View file

@ -33,36 +33,26 @@ Template.myTasksForm.events({
let taskDateArray = Session.get("taskDateArr"); let taskDateArray = Session.get("taskDateArr");
let actDate = []; let actDate = [];
console.dir(taskNameArray);
console.dir(taskDateArray);
if (taskNameArray == null || taskNameArray == [] || taskNameArray == "") { if (taskNameArray == null || taskNameArray == [] || taskNameArray == "") {
taskNameErr = true; taskNameErr = true;
} }
if (taskDateArray == null || taskDateArray == []|| taskDateArray == "") { if (taskDateArray == null || taskDateArray == []|| taskDateArray == "") {
taskDateErr = true; taskDateErr = true;
} else {
for (let i = 0; i < taskDateArray.length; i++) {
// console.log(taskDateArray[i]);
let actDateTask = new Date(taskDateArray[i]);
actDate.push(actDateTask);
}
} }
// console.log("Date Error: " + taskDateErr + " - Name Error: " + taskNameErr); // console.log("Date Error: " + taskDateErr + " - Name Error: " + taskNameErr);
if (taskDateErr == false && taskNameErr == false) { if (taskDateErr == false && taskNameErr == false) {
const addTask = async() => { for (const task of taskNameArray) {
let result = await Meteor.callAsync("add.task", taskNameArray, "self", "selfId", taskDateArray, actDate); for (const date of taskDateArray) {
if (!result) { let actDate = new Date(date);
console.log(" ERROR adding task for self: ");
showSnackbar("Error adding task for self!", "red"); addTask(task.id, date, actDate);
} else {
console.log(" SUCCESS adding task for self.");
Session.set("taskDateArr", []);
$("#myTaskName").val("");
$("#myTaskDate").val("");
showSnackbar("Added Tasks Successfully!", "green");
} }
} }
addTask();
} else { } else {
showSnackbar("Error! Both Task & Date are Required!", "red"); showSnackbar("Error! Both Task & Date are Required!", "red");
} }
@ -94,3 +84,17 @@ Template.myTasksForm.events({
Session.set("taskDateArr", taskDateArr); Session.set("taskDateArr", taskDateArr);
}, },
}); });
const addTask = async(task, date, actDate) => {
let result = await Meteor.callAsync("add.myTask", task, date, actDate);
if (!result) {
console.log(" ERROR adding task for self: ");
showSnackbar("Error adding task for self!", "red");
} else {
console.log(" SUCCESS adding task for self.");
// Session.set("taskDateArr", []);
// $("#myTaskName").val("");
// $("#myTaskDate").val("");
showSnackbar("Added Tasks Successfully!", "green");
}
}

View file

@ -13,7 +13,7 @@ TaskItems.allow({
}); });
Meteor.methods({ Meteor.methods({
'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) { async 'add.task' (taskNameArr, assignedTo, assignedToId, taskDateArr, actDate) {
check(taskNameArr, [Object]); check(taskNameArr, [Object]);
check(assignedTo, String); check(assignedTo, String);
check(taskDateArr, [String]); check(taskDateArr, [String]);
@ -24,40 +24,14 @@ Meteor.methods({
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
} }
let username;
let usInfo;
if (assignedTo == "self") {
const uInfo = async() => {
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) {
console.log("No matching user info found.")
} else {
username = userInfo.profile.fullname;
assignedToId = this.userId;
let usInf = { "username": username, "assignedToId": assignedToId };
console.log("setting username = " + username +", and id = " + assignedToId);
return usInf;
}
}
usInfo = uInfo();
console.dir(usInfo);
} else {
username = assignedTo;
usInfo = { "username": assignedTo, "assignedToId": assignedToId };
console.log("Set username = " + assignedTo);
}
console.log("assignedToId = " + usInfo.assignedToId);
for (let i=0; i < taskDateArr.length; i++) { for (let i=0; i < taskDateArr.length; i++) {
for (let j=0; j < taskNameArr.length; j++) { for (let j=0; j < taskNameArr.length; j++) {
TaskItems.insertAsync({ await TaskItems.insertAsync({
taskName: taskNameArr[j].id, taskName: taskNameArr[j].id,
taskDate: taskDateArr[i], taskDate: taskDateArr[i],
actualDate: actDate[i], actualDate: actDate[i],
assignedTo: usInfo.username, assignedTo: assignedTo,
assignedToId: usInfo.assignedToId, assignedToId: assignedToId,
isComplete: false, isComplete: false,
completedOn: null, completedOn: null,
assignedOn: new Date(), assignedOn: new Date(),
@ -65,9 +39,8 @@ Meteor.methods({
}); });
} }
} }
return;
}, },
'add.mytask' (taskName, assignedTo, assignedToId, taskDate, actDate) { async 'add.myTask' (taskName, taskDate, actDate) {
check(taskName, String); check(taskName, String);
check(taskDate, String); check(taskDate, String);
check(actDate, Date); check(actDate, Date);
@ -76,40 +49,28 @@ Meteor.methods({
throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to add tasks. Make sure you are logged in with valid user credentials.');
} }
let username;
let usInfo;
if (assignedTo == "self") {
const uInfo = async() => {
let userInfo = await Meteor.users.findOneAsync({ _id: this.userId }); let userInfo = await Meteor.users.findOneAsync({ _id: this.userId });
if (!userInfo) { if (!userInfo) {
console.log("No matching user info found.") console.log("No matching user info found.")
} else { } else {
username = userInfo.profile.fullname; try {
assignedToId = this.userId; return await TaskItems.insertAsync({
let usinf = { username: username, assignedToId: assignedToId };
return usinf;
}
}
usInfo = uInfo();
} else {
username = assignedTo;
usInfo = { username: assignedTo, assignedToId: assignedToId };
}
return TaskItems.insertAsync({
taskName: taskName, taskName: taskName,
taskDate: taskDate, taskDate: taskDate,
actualDate: actDate, actualDate: actDate,
assignedTo: usInfo.username, assignedTo: userInfo.profile.fullname,
assignedToId: usInfo.assignedToId, assignedToId: this.userId,
isComplete: false, isComplete: false,
completedOn: null, completedOn: null,
assignedOn: new Date(), assignedOn: new Date(),
assignedBy: this.userId, assignedBy: this.userId,
}); });
} catch(error) {
console.log(" ERROR adding tasksL " + error.message);
}
}
}, },
'edit.task' (taskId, taskName, assignedTo, taskDate) { async 'edit.task' (taskId, taskName, assignedTo, taskDate) {
check(taskId, String); check(taskId, String);
check(taskName, String); check(taskName, String);
check(assignedTo, String); check(assignedTo, String);
@ -119,7 +80,7 @@ Meteor.methods({
throw new Meteor.Error('You are not allowed to edit tasks. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to edit tasks. Make sure you are logged in with valid user credentials.');
} }
return TaskItems.updateAsync({ _id: taskId }, { return await TaskItems.updateAsync({ _id: taskId }, {
$set: { $set: {
taskName: taskName, taskName: taskName,
taskDate: taskDate, taskDate: taskDate,
@ -129,23 +90,23 @@ Meteor.methods({
} }
}); });
}, },
'delete.task' (taskId) { async 'delete.task' (taskId) {
check(taskId, String); check(taskId, String);
if (!this.userId) { if (!this.userId) {
throw new Meteor.Error('You are not allowed to delete tasks. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to delete tasks. Make sure you are logged in with valid user credentials.');
} }
return TaskItems.removeAsync({ _id: taskId }); return await TaskItems.removeAsync({ _id: taskId });
}, },
'markTask.complete' (taskId) { async 'markTask.complete' (taskId) {
check(taskId, String); check(taskId, String);
if (!this.userId) { if (!this.userId) {
throw new Meteor.Error('You are not allowed to mark tasks complete. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to mark tasks complete. Make sure you are logged in with valid user credentials.');
} }
return TaskItems.updateAsync({ _id: taskId }, { return await TaskItems.updateAsync({ _id: taskId }, {
$set: { $set: {
isComplete: true, isComplete: true,
completedOn: new Date(), completedOn: new Date(),
@ -153,14 +114,14 @@ Meteor.methods({
} }
}); });
}, },
'markTask.notComplete' (taskId) { async 'markTask.notComplete' (taskId) {
check(taskId, String); check(taskId, String);
if (!this.userId) { if (!this.userId) {
throw new Meteor.Error('You are not allowed to mark tasks not complete. Make sure you are logged in with valid user credentials.'); throw new Meteor.Error('You are not allowed to mark tasks not complete. Make sure you are logged in with valid user credentials.');
} }
return TaskItems.updateAsync({ _id: taskId }, { return await TaskItems.updateAsync({ _id: taskId }, {
$set: { $set: {
isComplete: false, isComplete: false,
markedUncomplteOn: new Date(), markedUncomplteOn: new Date(),
@ -168,7 +129,7 @@ Meteor.methods({
} }
}); });
}, },
'clean.Tasks' (timeFrame) { async 'clean.Tasks' (timeFrame) {
check(timeFrame, String); check(timeFrame, String);
if (!this.userId) { if (!this.userId) {
@ -202,6 +163,6 @@ Meteor.methods({
break; break;
} }
return TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}}); return await TaskItems.removeAsync({ actualDate: { $lt: new Date((new Date()) - upToDate )}});
} }
}); });

951
package-lock.json generated

File diff suppressed because it is too large Load diff