sessionHistory.js 2.1 KB

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