| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- export default {
- rendered: false,
- workout: null,
- pastSessions: null,
- exerciseIndex: 0,
- currentSession: null,
- render: async function(workout){
- this.workout = workout;
- this.pastSessions = await this.getPastSessions(workout.id);
- const previous = localStorage.getItem(workout.id);
- if(previous){
- this.currentSession = JSON.parse(previous);
- this.exerciseIndex = this.currentSession.exerciseIndex;
- }else{
- this.currentSession = this.createNewSession(workout);
- }
- this.buttons();
- this.changeExercise(0);
- },
- getPastSessions: async function(id){
- const sessions = await fetch(`/session/${id}`, {
- method: "GET",
- headers: {
- "Content-Type": "application/json"
- }
- });
- if(sessions.error){
- notify("error", "ERROR: Unable to retrieve past workouts");
- return [];
- }
- return await sessions.json();
- },
- buttons: function(){
- const nextSessionBtn = document.getElementById("nextSessionBtn");
- nextSessionBtn.style.display = "block";
- if(!this.rendered){
- nextSessionBtn.addEventListener("click", ()=>{
- this.currentSession.exercises[this.exerciseIndex].done = true;
- for(let i = this.exerciseIndex + 1; i < this.currentSession.exercises.length; i++){
- if(this.currentSession.exercises[i] && this.currentSession.exercises[i].done) continue;
- this.changeExercise(i);
- break;
- }
- });
- document.getElementById("finishSessionBtn").addEventListener("click", ()=>{this.finish()});
- document.getElementById("sessionAddSet").addEventListener("click", ()=>{this.addSet()});
- document.getElementById("sessionNotesBtn").addEventListener("click", this.displayNotes.bind(this));
- document.getElementById("sessionNotesDone").addEventListener("click", this.closeNote.bind(this));
- document.getElementById("sessionExercisesBtn").addEventListener("click", this.displayExerciseList.bind(this));
- document.getElementById("closeExerciseList").addEventListener("click", ()=>{
- document.getElementById("sessionExercisesList").style.display = "none";
- });
- this.rendered = true;
- }
- },
- displayExerciseList: function(){
- const list = document.getElementById("sessionExercisesList");
- list.style.display = "flex";
- const buttons = list.querySelectorAll(".exerciseListButton");
- for(let i = 0; i < buttons.length; i++){
- buttons[i].parentElement.removeChild(buttons[i]);
- }
- for(let i = 0; i < this.workout.exercises.length; i++){
- const button = document.createElement("button");
- button.classList.add("exerciseListButton");
- if(this.exerciseIndex === i){
- button.textContent = `*${this.workout.exercises[i].name}`;
- }else{
- button.textContent = this.workout.exercises[i].name;
- }
- button.addEventListener("click", ()=>{
- this.changeExercise(i);
- list.style.display = "none";
- });
- if(this.currentSession.exercises[i]?.done){
- button.style.color = "white";
- }
- list.appendChild(button);
- }
- },
- displayNotes: function(){
- const container = document.getElementById("sessionNotesText");
- container.style.display = "flex";
- const textarea = container.querySelector("textarea");
- textarea.value = this.workout.exercises[this.exerciseIndex].notes || "";
- },
- closeNote: function(){
- const exercise = this.workout.exercises.find(e => e._id === this.currentSession.exercises[this.exerciseIndex].exerciseId);
- const newNote = document.getElementById("sessionTextArea").value;
- if(exercise.notes !== newNote){
- fetch(`/workout/${this.workout.id}/note`, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify({
- exercise: this.currentSession.exercises[this.exerciseIndex].exerciseId,
- note: newNote
- })
- })
- .then(r=>r.json())
- .then((response)=>{
- if(response.error){
- notify("error", response.error.message);
- }
- exercise.notes = newNote;
- })
- .catch((err)=>{
- notify("error", "ERROR: unable to save note");
- });
- }
- const noteIcon = document.getElementById("sessionNotesIcon");
- console.log(newNote);
- if(newNote === ""){
- noteIcon.style.display = "none";
- }else{
- noteIcon.style.display = "flex";
- }
- document.getElementById("sessionNotesText").style.display = "none";
- },
- addSet: function(){
- const exercise = this.currentSession.exercises[this.exerciseIndex];
- const template = document.getElementById("weightSet").content.children[0];
- const container = document.getElementById("sessionSets");
- switch(exercise.type){
- case "weights":
- const newSet = {weight: 0, reps: 0};
- const setNumber = container.children.length + 1;
- exercise.sets.push(newSet);
- container.appendChild(this.createWeightSetElement(template, newSet, setNumber));
- break;
- }
- },
- createNewSession: function(workout){
- session = {
- workout: workout.id,
- start: new Date(),
- notes: "",
- exercises: Array(this.workout.exercises.length).fill(null),
- exerciseIndex: this.exerciseIndex
- };
- localStorage.setItem(workout.id, JSON.stringify(session));
- return session;
- },
- changeExercise: function(num){
- this.exerciseIndex = num;
- this.currentSession.exerciseIndex = this.exerciseIndex;
- localStorage.setItem(this.workout.id, JSON.stringify(this.currentSession));
- const nextBtn = document.getElementById("nextSessionBtn");
- if(this.exerciseIndex === this.workout.exercises.length-1){
- nextBtn.style.display = "none";
- }else{
- nextBtn.style.display = "block";
- }
- let exercise = null;
- if(this.currentSession.exercises[this.exerciseIndex]){
- exercise = this.currentSession.exercises[this.exerciseIndex];
- }else{
- const workoutExercise = this.workout.exercises[this.exerciseIndex];
- exercise = {
- exerciseId: workoutExercise._id,
- name: workoutExercise.name,
- type: workoutExercise.type,
- notes: "",
- sets: this.getPastSets(workoutExercise._id),
- done: false
- }
- this.currentSession.exercises[this.exerciseIndex] = exercise;
- }
- document.getElementById("sessionExerciseName").textContent = exercise.name;
- const setsContainer = document.getElementById("sessionSets");
- while(setsContainer.children.length > 0){
- setsContainer.removeChild(setsContainer.firstChild);
- }
- switch(exercise.type){
- case "weights": this.displayWeightSets(exercise.sets, setsContainer);
- }
- const noteIcon = document.getElementById("sessionNotesIcon");
- if(this.workout.exercises[this.exerciseIndex].notes !== ""){
- noteIcon.style.display = "flex";
- }else{
- noteIcon.style.display = "none";
- }
- window.scrollTo(0, 0);
- },
- displayWeightSets: function(sets, container){
- const template = document.getElementById("weightSet").content.children[0];
- for(let i = 0; i < sets.length; i++){
- container.appendChild(this.createWeightSetElement(template, sets[i], i+1));
- }
- },
- createWeightSetElement: function(template, set, num){
- const setElem = template.cloneNode(true);
- setElem.querySelector("h3").textContent = `Set #${num}`;
- const deleteBtn = setElem.querySelector(".weightSetDelete");
- deleteBtn.addEventListener("click", (event)=>{
- this.deleteSet(num, event.target.parentElement);
- localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
- });
- const weightInput = setElem.querySelector(".weightSetWeight");
- if(set.weight !== 0) weightInput.value = set.weight;
- weightInput.addEventListener("input", ()=>{
- set.weight = weightInput.value
- localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
- });
- const repInput = setElem.querySelector(".weightSetReps");
- if(set.reps !== 0) repInput.value = set.reps;
- repInput.addEventListener("input", ()=>{
- set.reps = repInput.value
- localStorage.setItem(this.currentSession.workout, JSON.stringify(this.currentSession));
- });
- return setElem;
- },
- deleteSet: function(num, setElem){
- this.currentSession.exercises[this.exerciseIndex].sets.splice(num-1, 1);
- const container = setElem.parentElement;
- container.removeChild(setElem);
- for(let i = 0; i < container.children.length; i++){
- container.children[i].querySelector("h3").textContent = `Set #${i+1}`;
- }
- },
- getPastSets: function(id){
- const note = document.getElementById("previousSession");
- for(let i = 0; i < this.pastSessions.length; i++){
- for(let j = 0; j < this.pastSessions[i].exercises.length; j++){
- if(this.pastSessions[i].exercises[j]?.exerciseId === id){
- const exercise = this.pastSessions[i].exercises[j];
- const date = new Date(this.pastSessions[i].start);
- note.textContent = `*Data autofilled from ${this.getDate(date)}`;
- return exercise.sets;
- }
- }
- }
- note.textContent = "*No previous workout data";
- return [{weight: 0, reps: 0}, {weight: 0, reps: 0}, {weight: 0, reps: 0}];
- },
- finish: function(){
- this.currentSession.end = new Date();
- fetch(`/session`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(this.currentSession)
- })
- .then(r=>r.json())
- .then((response)=>{
- if(response.error){
- notify("error", response.error.message);
- }else{
- notify("success", "Workout completed and saved");
- localStorage.removeItem(this.currentSession.workout);
- this.workout = null;
- this.exerciseIndex = 0;
- this.pastSessions = null;
- this.currentSession = null;
- changePage("home");
- }
- })
- .catch((err)=>{
- notify("error", "ERROR: unable to save workout to database");
- });
- },
- getDate: function(date){
- return date.toLocaleDateString("en-US", {
- year: "numeric",
- month: "long",
- day: "numeric"
- });
- }
- }
|