appointments.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. import {fetchWithToken, getTimeSpan, parseOrRedirect} from "./utils.js";
  3. let last_clicked = "";
  4. let click_counter = 0;
  5. /**
  6. * Adds an event listener to window load to fetch and render appointments.
  7. */
  8. window.addEventListener("load", async function () {
  9. await fetchAppointments();
  10. document.getElementById("content").classList.remove("hidden");
  11. });
  12. /**
  13. * Renders appointment data into the table body of an HTML table.
  14. * @param {Array<Object>} data - Array of appointment objects containing title, description, dueDate, place, startDate, and endDate.
  15. */
  16. function renderAppointments(data) {
  17. const tableBody = document.getElementById('appointment-table-body');
  18. tableBody.innerHTML = '';
  19. data.forEach(appointment => {
  20. const row = createAppointmentRow(appointment);
  21. // Append the row to the table body
  22. tableBody.appendChild(row);
  23. });
  24. }
  25. /**
  26. * Creates a row in the appointment table for each appointment.
  27. * @param {Object} appointment - Appointment object containing title, description, dueDate, place, startDate, and endDate.
  28. * @returns {HTMLTableRowElement} The newly created table row element.
  29. */
  30. function createAppointmentRow(appointment) {
  31. const row = document.createElement('tr');
  32. const titleCell = document.createElement('td');
  33. const descriptionCell = document.createElement('td');
  34. const placeCell = document.createElement('td');
  35. const dueDateCell = document.createElement('td');
  36. const timeSpanCell = document.createElement('td');
  37. const shareCell = document.createElement('td');
  38. const participantCell = document.createElement('td');
  39. const editCell = document.createElement('td');
  40. const deleteCell = document.createElement('td');
  41. const shareATag = document.createElement('a');
  42. const participantATag = document.createElement('a');
  43. const editATag = document.createElement('a');
  44. const deleteButton = document.createElement('button');
  45. const searchParams = new URLSearchParams({
  46. appointment: appointment._id,
  47. });
  48. participantATag.href = '/html/bookings.html?' + searchParams.toString();
  49. participantATag.innerText = 'participants';
  50. participantATag.classList.add('participants-link');
  51. editATag.href = '/html/appointmentscreator.html?' + searchParams.toString();
  52. shareATag.href = '/html/schedule.html?' + searchParams.toString();
  53. shareATag.innerText = 'share';
  54. shareATag.classList.add('share-link');
  55. editATag.innerText = 'edit';
  56. editATag.classList.add('edit-link');
  57. deleteButton.onclick = async () => {
  58. if (last_clicked !== appointment._id) {
  59. click_counter += 1;
  60. last_clicked = appointment._id;
  61. return;
  62. }
  63. await deleteAppointment(appointment._id);
  64. }
  65. deleteButton.innerText = 'delete';
  66. shareCell.appendChild(shareATag);
  67. participantCell.appendChild(participantATag);
  68. editCell.appendChild(editATag);
  69. deleteCell.appendChild(deleteButton);
  70. titleCell.textContent = appointment.title;
  71. descriptionCell.textContent = appointment.description;
  72. dueDateCell.textContent = new Date(appointment.dueDate).toLocaleDateString();
  73. placeCell.textContent = appointment.place;
  74. timeSpanCell.textContent = getTimeSpan(appointment.startDate, appointment.endDate);
  75. timeSpanCell.classList.add('time-span');
  76. // Append cells to the row
  77. row.appendChild(titleCell);
  78. row.appendChild(descriptionCell);
  79. row.appendChild(dueDateCell);
  80. row.appendChild(placeCell);
  81. row.appendChild(timeSpanCell);
  82. row.appendChild(shareCell);
  83. row.appendChild(participantCell);
  84. row.appendChild(editCell);
  85. row.appendChild(deleteCell);
  86. // Append the row to the table body
  87. return row;
  88. }
  89. /**
  90. * Creates a row in the appointment table for each appointment.
  91. * @param {String} appointmentId - AppointmentID
  92. * @returns {HTMLTableRowElement} The newly created table row element.
  93. */
  94. async function deleteAppointment(appointmentId) {
  95. const response = await fetchWithToken(`/api/users/delete`, {
  96. method: 'DELETE',
  97. body: JSON.stringify({appointmentId: appointmentId})
  98. });
  99. await parseOrRedirect(response);
  100. if (response.ok) {
  101. await fetchAppointments();
  102. }
  103. }
  104. async function fetchAppointments() {
  105. const response = await fetchWithToken("/api/users/appointments");
  106. const js = await parseOrRedirect(response);
  107. if (js.length === 0) return;
  108. renderAppointments(js);
  109. }