appointments.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. import {fetchWithToken, getTimeSpan, parseOrRedirect} from "./utils.js";
  3. let last_clicked = "";
  4. let click_counter = 0;
  5. // Call the function to render data
  6. window.addEventListener("load", async function () {
  7. await fetchAppointments();
  8. document.getElementById("content").classList.remove("hidden");
  9. });
  10. // Render the appointments into the table
  11. function renderAppointments(data) {
  12. const tableBody = document.getElementById('appointment-table-body');
  13. tableBody.innerHTML = '';
  14. data.forEach(appointment => {
  15. const row = document.createElement('tr');
  16. const titleCell = document.createElement('td');
  17. const descriptionCell = document.createElement('td');
  18. const placeCell = document.createElement('td');
  19. const dueDateCell = document.createElement('td');
  20. const timeSpanCell = document.createElement('td');
  21. const shareCell = document.createElement('td');
  22. const participantCell = document.createElement('td');
  23. const editCell = document.createElement('td');
  24. const deleteCell = document.createElement('td');
  25. const shareATag = document.createElement('a');
  26. const participantATag = document.createElement('a');
  27. const editATag = document.createElement('a');
  28. const deleteButton = document.createElement('button');
  29. participantATag.href = `/html/bookings.html?appointment=${appointment._id}`;
  30. participantATag.innerText = 'participants';
  31. participantATag.classList.add('participants-link');
  32. editATag.href = `/html/appointmentscreator.html?appointment=${appointment._id}`;
  33. shareATag.href = `/html/schedule.html?appointment=${appointment._id}`;
  34. shareATag.innerText = 'share';
  35. shareATag.classList.add('share-link');
  36. editATag.innerText = 'edit';
  37. editATag.classList.add('edit-link');
  38. deleteButton.onclick = async () => {
  39. if (last_clicked !== appointment._id) {
  40. click_counter += 1;
  41. last_clicked = appointment._id;
  42. return;
  43. }
  44. await deleteAppointment(appointment._id);
  45. }
  46. deleteButton.innerText = 'delete';
  47. shareCell.appendChild(shareATag);
  48. participantCell.appendChild(participantATag);
  49. editCell.appendChild(editATag);
  50. deleteCell.appendChild(deleteButton);
  51. titleCell.textContent = appointment.title;
  52. descriptionCell.textContent = appointment.description;
  53. dueDateCell.textContent = new Date(appointment.dueDate).toLocaleDateString();
  54. placeCell.textContent = appointment.place;
  55. timeSpanCell.textContent = getTimeSpan(appointment.startDate, appointment.endDate);
  56. timeSpanCell.classList.add('time-span');
  57. // Append cells to the row
  58. row.appendChild(titleCell);
  59. row.appendChild(descriptionCell);
  60. row.appendChild(dueDateCell);
  61. row.appendChild(placeCell);
  62. row.appendChild(timeSpanCell);
  63. row.appendChild(shareCell);
  64. row.appendChild(participantCell);
  65. row.appendChild(editCell);
  66. row.appendChild(deleteCell);
  67. // Append the row to the table body
  68. tableBody.appendChild(row);
  69. tableBody.appendChild(row);
  70. });
  71. }
  72. async function deleteAppointment(appointmentId) {
  73. const response = await fetchWithToken(`/api/users/delete`, {
  74. method: 'DELETE',
  75. body: JSON.stringify({appointmentId: appointmentId})
  76. });
  77. await parseOrRedirect(response);
  78. if (response.ok) {
  79. await fetchAppointments();
  80. }
  81. }
  82. async function fetchAppointments() {
  83. const response = await fetchWithToken("/api/users/appointments");
  84. const js = await parseOrRedirect(response);
  85. renderAppointments(js);
  86. }