appointments.js 4.3 KB

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