appointments.js 4.4 KB

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