sessionHistory.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. export default {
  2. rendered: false,
  3. workout: null,
  4. render: function(workout){
  5. if(workout){
  6. this.workout = workout;
  7. this.populateSessions(workout);
  8. document.getElementById("sessionHistoryTitle").textContent = workout.name;
  9. }
  10. if(!this.rendered){
  11. this.buttons(workout);
  12. this.rendered = true;
  13. }
  14. },
  15. buttons: function(workout){
  16. document.getElementById("sessionHistoryClose").addEventListener("click", ()=>{
  17. changePage("workoutMenu", workout);
  18. });
  19. },
  20. populateSessions: function(workout){
  21. fetch(`/session/${workout.id}/all`, {
  22. method: "GET",
  23. headers: {
  24. "Content-Type": "application/json"
  25. }
  26. })
  27. .then(r=>r.json())
  28. .then((response)=>{
  29. if(response.error){
  30. notify("error", response.error.message);
  31. }else{
  32. this.showSessions(response);
  33. }
  34. })
  35. .catch((err)=>{
  36. console.log(err);
  37. notify("error", "ERROR: unable to load your data");
  38. });
  39. },
  40. showSessions: function(sessions){
  41. const container = document.getElementById("sessionHistoryItems");
  42. while(container.children.length > 0){
  43. container.removeChild(container.firstChild);
  44. }
  45. for(let i = 0; i < sessions.length; i++){
  46. container.appendChild(this.createSessionButton(sessions[i]));
  47. }
  48. const button = document.createElement("button");
  49. button.classList.add("button");
  50. button.addEventListern
  51. },
  52. createSessionButton: function(session){
  53. const button = document.createElement("button");
  54. button.classList.add("button");
  55. button.textContent = this.dateFormat(new Date(session.start));
  56. button.addEventListener("click", ()=>{
  57. changePage("sessionData", {
  58. session: session,
  59. workout: this.workout
  60. });
  61. });
  62. return button;
  63. },
  64. dateFormat: function(date){
  65. return date.toLocaleDateString("en-us", {
  66. year: "numeric",
  67. month: "long",
  68. day: "numeric"
  69. });
  70. }
  71. }