appointments.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. editATag.href = `/html/appointmentscreator.html?appointment=${appointment._id}`;
  40. shareATag.href = `/html/schedule.html?appointment=${appointment._id}`;
  41. shareATag.innerText = 'share';
  42. editATag.innerText = 'edit';
  43. deleteButton.onclick = async () => {
  44. if (last_clicked !== appointment._id) {
  45. click_counter += 1;
  46. last_clicked = appointment._id;
  47. return;
  48. }
  49. await deleteAppointment(appointment._id);
  50. }
  51. deleteButton.innerText = 'delete';
  52. shareCell.appendChild(shareATag);
  53. participantCell.appendChild(participantATag);
  54. editCell.appendChild(editATag);
  55. deleteCell.appendChild(deleteButton);
  56. titleCell.textContent = appointment.title;
  57. descriptionCell.textContent = appointment.description;
  58. dueDateCell.textContent = new Date(appointment.dueDate).toLocaleDateString();
  59. placeCell.textContent = appointment.place;
  60. timeSpanCell.textContent = getTimeSpan(appointment.startDate, appointment.endDate);
  61. timeSpanCell.classList.add('time-span');
  62. // Append cells to the row
  63. row.appendChild(titleCell);
  64. row.appendChild(descriptionCell);
  65. row.appendChild(dueDateCell);
  66. row.appendChild(placeCell);
  67. row.appendChild(timeSpanCell);
  68. row.appendChild(shareCell);
  69. row.appendChild(participantCell);
  70. row.appendChild(editCell);
  71. row.appendChild(deleteCell);
  72. // Append the row to the table body
  73. tableBody.appendChild(row);
  74. tableBody.appendChild(row);
  75. });
  76. }
  77. async function fetchWithToken(url, options) {
  78. const authToken = localStorage.getItem('token');
  79. return await fetch(url, {...options, headers: {
  80. 'Authorization': `Bearer ${authToken}`,
  81. 'Content-Type': 'application/json',
  82. }});
  83. }
  84. async function deleteAppointment(appointmentId) {
  85. const response = await fetchWithToken(`/api/users/delete`, {
  86. method: 'DELETE',
  87. body: JSON.stringify({appointmentId: appointmentId})
  88. });
  89. if (response.status === 401) {
  90. window.location.replace("/html/auth.html");
  91. return;
  92. }
  93. if (response.ok) {
  94. await fetchAppointments();
  95. }
  96. }
  97. async function fetchAppointments() {
  98. const response = await fetchWithToken("/api/users/appointments");
  99. if (response.status === 401) {
  100. window.location.replace("/html/auth.html");
  101. return;
  102. }
  103. const js = await response.json();
  104. if (!response.ok) {
  105. window.location.replace("/html/404.html");
  106. }
  107. renderAppointments(js);
  108. }