sessionHistory.js 2.0 KB

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