appointments.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. import { fetchWithToken } 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. });
  9. // Function to format time as HH:MM
  10. function formatTime(date) {
  11. const hours = date.getUTCHours().toString().padStart(2, '0');
  12. const minutes = date.getUTCMinutes().toString().padStart(2, '0');
  13. return `${hours}:${minutes}`;
  14. }
  15. // Function to get the time span (e.g., 09:00 to 10:00)
  16. function getTimeSpan(start, end) {
  17. return `${formatTime(new Date(start))} to ${formatTime(new Date(end))}`;
  18. }
  19. // Render the appointments into the table
  20. function renderAppointments(data) {
  21. const tableBody = document.getElementById('appointment-table-body');
  22. tableBody.innerHTML = '';
  23. data.forEach(appointment => {
  24. const row = document.createElement('tr');
  25. const titleCell = document.createElement('td');
  26. const descriptionCell = document.createElement('td');
  27. const placeCell = document.createElement('td');
  28. const dueDateCell = document.createElement('td');
  29. const timeSpanCell = document.createElement('td');
  30. const shareCell = document.createElement('td');
  31. const participantCell = document.createElement('td');
  32. const editCell = document.createElement('td');
  33. const deleteCell = document.createElement('td');
  34. const shareATag = document.createElement('a');
  35. const participantATag = document.createElement('a');
  36. const editATag = document.createElement('a');
  37. const deleteButton = document.createElement('button');
  38. participantATag.href = `/html/bookings.html?appointment=${appointment._id}`;
  39. participantATag.innerText = 'participants';
  40. participantATag.classList.add('participants-link');
  41. editATag.href = `/html/appointmentscreator.html?appointment=${appointment._id}`;
  42. shareATag.href = `/html/schedule.html?appointment=${appointment._id}`;
  43. shareATag.innerText = 'share';
  44. shareATag.classList.add('share-link');
  45. editATag.innerText = 'edit';
  46. editATag.classList.add('edit-link');
  47. deleteButton.onclick = async () => {
  48. if (last_clicked !== appointment._id) {
  49. click_counter += 1;
  50. last_clicked = appointment._id;
  51. return;
  52. }
  53. await deleteAppointment(appointment._id);
  54. }
  55. deleteButton.innerText = 'delete';
  56. shareCell.appendChild(shareATag);
  57. participantCell.appendChild(participantATag);
  58. editCell.appendChild(editATag);
  59. deleteCell.appendChild(deleteButton);
  60. titleCell.textContent = appointment.title;
  61. descriptionCell.textContent = appointment.description;
  62. dueDateCell.textContent = new Date(appointment.dueDate).toLocaleDateString();
  63. placeCell.textContent = appointment.place;
  64. timeSpanCell.textContent = getTimeSpan(appointment.startDate, appointment.endDate);
  65. timeSpanCell.classList.add('time-span');
  66. // Append cells to the row
  67. row.appendChild(titleCell);
  68. row.appendChild(descriptionCell);
  69. row.appendChild(dueDateCell);
  70. row.appendChild(placeCell);
  71. row.appendChild(timeSpanCell);
  72. row.appendChild(shareCell);
  73. row.appendChild(participantCell);
  74. row.appendChild(editCell);
  75. row.appendChild(deleteCell);
  76. // Append the row to the table body
  77. tableBody.appendChild(row);
  78. tableBody.appendChild(row);
  79. });
  80. }
  81. async function deleteAppointment(appointmentId) {
  82. const response = await fetchWithToken(`/api/users/delete`, {
  83. method: 'DELETE',
  84. body: JSON.stringify({appointmentId: appointmentId})
  85. });
  86. if (response.status === 401) {
  87. window.location.replace("/html/auth.html");
  88. return;
  89. }
  90. if (response.ok) {
  91. await fetchAppointments();
  92. }
  93. }
  94. async function fetchAppointments() {
  95. const response = await fetchWithToken("/api/users/appointments");
  96. if (response.status === 401) {
  97. window.location.replace("/html/auth.html");
  98. return;
  99. }
  100. const js = await response.json();
  101. if (!response.ok) {
  102. window.location.replace("/html/404.html");
  103. }
  104. renderAppointments(js);
  105. }