148 lines
4.8 KiB
JavaScript
148 lines
4.8 KiB
JavaScript
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
|
|
import { Workouts } from '../../../../imports/api/workouts';
|
|
import { WorkoutLog } from '../../../../imports/api/workoutLog';
|
|
import { LogEntry } from '../../../../imports/api/logEntry';
|
|
import dayjs from 'dayjs';
|
|
|
|
Template.logEntry.onCreated(function() {
|
|
this.subscribe("myWorkoutRoutines");
|
|
this.subscribe("myWorkoutLog");
|
|
this.subscribe("myLogEntries");
|
|
});
|
|
|
|
Template.logEntry.onRendered(function() {
|
|
Session.set("exId", "");
|
|
Session.set("exIdSelected", false);
|
|
Session.set("setNo", 0);
|
|
Session.set("addSet", false);
|
|
Session.set("logId", "f");
|
|
});
|
|
|
|
Template.logEntry.helpers({
|
|
workoutLog: function() {
|
|
return WorkoutLog.find({});
|
|
},
|
|
exerciseToLog: function() {
|
|
let exId = Session.get("exerciseLogId");
|
|
return WorkoutLog.findOneAsync({ _id: exId });
|
|
},
|
|
exIdSelected: function() {
|
|
return Session.get("exIdSelected");
|
|
},
|
|
setNo: function() {
|
|
return Session.get("setNo");
|
|
},
|
|
addSet: function() {
|
|
return Session.get("addSet");
|
|
},
|
|
setsRecorded: async() => {
|
|
let logId = Session.get("logId");
|
|
const logInfo = await LogEntry.findOneAsync({ _id: logId });
|
|
|
|
if (logInfo) {
|
|
return logInfo && logInfo.sets ? logInfo.sets : [];
|
|
}
|
|
},
|
|
logIdSet: function() {
|
|
let logId = Session.get("logId");
|
|
if (logId != "f") {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
},
|
|
enteredWeight: function() {
|
|
return Session.get("enteredWeight");
|
|
},
|
|
prevWorkout: async() => {
|
|
let exerciseName = Session.get("exerciseName");
|
|
let logs = await LogEntry.findOneAsync({ exerciseName: exerciseName }, { sort: { _id: -1 } });
|
|
let todayDateTime = new Date();
|
|
let today = dayjs(todayDateTime).format('YYYY-MM-DD');
|
|
if (logs) {
|
|
if (logs.dateAdded != today) {
|
|
return logs;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
},
|
|
});
|
|
|
|
Template.logEntry.events({
|
|
'change #selectExerciseToLog' (e) {
|
|
let exerciseNameAndId = $("#selectExerciseToLog").val();
|
|
let splitExNameId = exerciseNameAndId.split(' | ');
|
|
let exerciseId = splitExNameId[0];
|
|
let exerciseName = splitExNameId[1];
|
|
|
|
Session.set("setNo", 0);
|
|
Session.set("exerciseName", exerciseName);
|
|
Session.set("exerciseLogId", exerciseId);
|
|
Session.set("exIdSelected", true);
|
|
Session.set("logId", "f");
|
|
},
|
|
'click #addASet' (e) {
|
|
let setNo = Session.get("setNo");
|
|
let logId = Session.get("logId");
|
|
setNo = setNo+=1;
|
|
Session.set("setNo", setNo);
|
|
Session.set("addSet", true);
|
|
let exerciseNameAndId = $("#selectExerciseToLog").val();
|
|
let splitExNameId = exerciseNameAndId.split(' | ');
|
|
let exerciseId = splitExNameId[0];
|
|
let exerciseName = splitExNameId[1];
|
|
let weight = $("#exerciseWeight").val();
|
|
let weightNo = Number(weight);
|
|
|
|
|
|
if (logId == "f") {
|
|
Session.set('enteredWeight', weight);
|
|
const addLogEntry = async() => {
|
|
try {
|
|
const result = await Meteor.callAsync('add.logEntry', exerciseId, exerciseName, weightNo);
|
|
if (!result) {
|
|
console.log(" UNABLE to add log entry.");
|
|
showSnackbar("Unable to add Log Entry!", "red");
|
|
} else {
|
|
Session.set("logId", result);
|
|
showSnackbar("Log Started!", "green");
|
|
}
|
|
} catch (error) {
|
|
console.log(" ERROR adding log entry: " + error);
|
|
}
|
|
}
|
|
addLogEntry();
|
|
}
|
|
},
|
|
'click #saveSet' (e) {
|
|
let logId = Session.get("logId");
|
|
let setNo = Session.get("setNo");
|
|
let reps = $("#addRepsVal_" + setNo).val();
|
|
|
|
let sets = Number(setNo);
|
|
let repCount = Number(reps);
|
|
|
|
// add meteor call to add this set to the log entry.
|
|
const addSetToLog = async() => {
|
|
try {
|
|
const result = await Meteor.callAsync('add.setToLog', logId, sets, repCount);
|
|
if (!result) {
|
|
console.log(" UNABLE to add set to log entry.");
|
|
showSnackbar("Set NOT added to log!", "red");
|
|
} else {
|
|
showSnackbar("Set Added Successfully!", "green");
|
|
$("#addRepsVal_" + setNo).val("");
|
|
Session.set("addSet", false);
|
|
}
|
|
} catch (error) {
|
|
console.log(" ERROR adding set to log entry: " + error);
|
|
}
|
|
}
|
|
addSetToLog();
|
|
},
|
|
});
|
|
|
|
|