appointments.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 participantCell = document.createElement('td');
  26. const editCell = document.createElement('td');
  27. const deleteCell = document.createElement('td');
  28. const participantATag = document.createElement('a');
  29. const editATag = document.createElement('a');
  30. const deleteButton = document.createElement('button');
  31. participantATag.href = `/html/bookings.html?appointment=${appointment._id}`;
  32. participantATag.innerText = 'participants';
  33. editATag.href = `/html/appointscreator.html?appointment=${appointment._id}`;
  34. editATag.innerText = 'edit';
  35. deleteButton.onclick = async () => {
  36. if (last_clicked !== appointment._id) {
  37. click_counter += 1;
  38. last_clicked = appointment._id;
  39. return;
  40. }
  41. await deleteAppointment(appointment._id);
  42. }
  43. deleteButton.innerText = 'delete';
  44. participantCell.appendChild(participantATag);
  45. editCell.appendChild(editATag);
  46. deleteCell.appendChild(deleteButton);
  47. titleCell.textContent = appointment.title;
  48. descriptionCell.textContent = appointment.description;
  49. dueDateCell.textContent = new Date(appointment.dueDate).toLocaleDateString();
  50. placeCell.textContent = appointment.place;
  51. timeSpanCell.textContent = getTimeSpan(appointment.startDate, appointment.endDate);
  52. timeSpanCell.classList.add('time-span');
  53. // Append cells to the row
  54. row.appendChild(titleCell);
  55. row.appendChild(descriptionCell);
  56. row.appendChild(dueDateCell);
  57. row.appendChild(placeCell);
  58. row.appendChild(timeSpanCell);
  59. row.appendChild(participantCell);
  60. row.appendChild(editCell);
  61. row.appendChild(deleteCell);
  62. // Append the row to the table body
  63. tableBody.appendChild(row);
  64. tableBody.appendChild(row);
  65. });
  66. }
  67. async function fetchWithToken(url, options) {
  68. const authToken = localStorage.getItem('token');
  69. return await fetch(url, {...options, headers: {
  70. 'Authorization': `Bearer ${authToken}`,
  71. 'Content-Type': 'application/json',
  72. }});
  73. }
  74. async function deleteAppointment(appointmentId) {
  75. const response = await fetchWithToken(`/api/users/delete`, {
  76. method: 'DELETE',
  77. body: JSON.stringify({appointmentId: appointmentId})
  78. });
  79. if (response.ok) {
  80. await fetchAppointments();
  81. }
  82. }
  83. async function fetchAppointments() {
  84. const response = await fetchWithToken("/api/users/appointments");
  85. const js = await response.json();
  86. if (!response.ok) {
  87. window.location.replace("/html/404.html");
  88. }
  89. renderAppointments(js);
  90. }
  91. // Call the function to render data
  92. window.onload = async function () {
  93. await fetchAppointments();
  94. }