bookings.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * Import necessary utility functions from utils.js
  3. */
  4. import {fetchRegular, fetchWithToken, getTimeSpan, parseOrRedirect} from "./utils.js";
  5. /**
  6. * Event listener for the window load event.
  7. * Retrieves the appointment ID from the URL query parameters and fetches the appointment details.
  8. */
  9. window.addEventListener("load", async function () {
  10. /**
  11. * Get the URL query parameters
  12. */
  13. const urlParams = new URLSearchParams(window.location.search);
  14. const appointment = urlParams.get('appointment');
  15. /**
  16. * If no appointment ID is found, redirect to the 404 page
  17. */
  18. if (!appointment) {
  19. window.location.replace("/html/404.html");
  20. }
  21. /**
  22. * Fetch the appointment details and bookings
  23. */
  24. await getAppointment(appointment);
  25. await fetchBookings(appointment);
  26. });
  27. /**
  28. * Displays the appointment details in the appointment-info container.
  29. * @param {Object} appointmentData - The appointment data object
  30. */
  31. function displayAppointmentDetails(appointmentData) {
  32. /**
  33. * Retrieve the appointment-info container
  34. */
  35. const appointmentInfo = document.querySelector('.appointment-info');
  36. /**
  37. * Clear existing content
  38. */
  39. while (appointmentInfo.firstChild) {
  40. appointmentInfo.removeChild(appointmentInfo.firstChild);
  41. }
  42. /**
  43. * Create and append the updated appointment content
  44. */
  45. const title = document.createElement('p');
  46. const titleLabel = document.createElement('strong');
  47. titleLabel.textContent = 'Title: ';
  48. title.appendChild(titleLabel);
  49. title.appendChild(document.createTextNode(appointmentData.title));
  50. const description = document.createElement('p');
  51. const descriptionLabel = document.createElement('strong');
  52. descriptionLabel.textContent = 'Description: ';
  53. description.appendChild(descriptionLabel);
  54. description.appendChild(document.createTextNode(appointmentData.description));
  55. const place = document.createElement('p');
  56. const placeLabel = document.createElement('strong');
  57. placeLabel.textContent = 'Place: ';
  58. place.appendChild(placeLabel);
  59. place.appendChild(document.createTextNode(appointmentData.place));
  60. const dueDate = document.createElement('p');
  61. const dueDateLabel = document.createElement('strong');
  62. dueDateLabel.textContent = 'Due Date: ';
  63. dueDate.appendChild(dueDateLabel);
  64. dueDate.appendChild(document.createTextNode(new Date(appointmentData.dueDate).toLocaleDateString()));
  65. const timeSpan = document.createElement('p');
  66. const timeSpanLabel = document.createElement('strong');
  67. timeSpanLabel.textContent = 'Time Span: ';
  68. timeSpan.appendChild(timeSpanLabel);
  69. timeSpan.appendChild(document.createTextNode(getTimeSpan(appointmentData.startDate, appointmentData.endDate)));
  70. /**
  71. * Append all the updated elements
  72. */
  73. appointmentInfo.appendChild(title);
  74. appointmentInfo.appendChild(description);
  75. appointmentInfo.appendChild(place);
  76. appointmentInfo.appendChild(dueDate);
  77. appointmentInfo.appendChild(timeSpan);
  78. }
  79. /**
  80. * Fetches the appointment details from the API.
  81. * @param {string} appointment - The appointment ID
  82. */
  83. async function getAppointment(appointment) {
  84. /**
  85. * Set the request options
  86. */
  87. const options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  88. /**
  89. * Set the query parameters
  90. */
  91. const searchParams = new URLSearchParams({
  92. appointmentId: appointment,
  93. isUser: true
  94. });
  95. /**
  96. * Fetch the appointment details
  97. */
  98. const response = await fetchRegular(`/api/schedule?` + searchParams.toString(), options)
  99. .catch(err => console.error('error:' + err));
  100. /**
  101. * Parse the response and display the appointment details
  102. */
  103. const js = await parseOrRedirect(response);
  104. displayAppointmentDetails(js);
  105. }
  106. /**
  107. * Renders the bookings data in the participants-table-body container.
  108. * @param {Array<Object>} data - The bookings data array
  109. */
  110. function renderBookings(data) {
  111. /**
  112. * Retrieve the participants-table-body container
  113. */
  114. const tableBody = document.getElementById('participants-table-body');
  115. tableBody.innerHTML = ''; // Clear existing rows
  116. /**
  117. * Iterate over the bookings data and create table rows
  118. */
  119. data.forEach(participant => {
  120. /**
  121. * Create a new table row
  122. */
  123. const row = document.createElement('tr');
  124. /**
  125. * Create table cells for first name, last name, and email
  126. */
  127. const firstNameCell = document.createElement('td');
  128. const lastNameCell = document.createElement('td');
  129. const emailCell = document.createElement('td');
  130. /**
  131. * Populate cell content with participant data
  132. */
  133. firstNameCell.textContent = participant.firstname;
  134. lastNameCell.textContent = participant.lastname;
  135. emailCell.textContent = participant.email;
  136. /**
  137. * Append cells to the row
  138. */
  139. row.appendChild(firstNameCell);
  140. row.appendChild(lastNameCell);
  141. row.appendChild(emailCell);
  142. /**
  143. * Append the row to the table body
  144. */
  145. tableBody.appendChild(row);
  146. });
  147. }
  148. /**
  149. * Fetches the bookings data from the API.
  150. * @param {string} appointmentId - The appointment ID
  151. */
  152. async function fetchBookings(appointmentId) {
  153. /**
  154. * Set the query parameters
  155. */
  156. const searchParams = new URLSearchParams({
  157. appointmentId: appointmentId,
  158. });
  159. /**
  160. * Fetch the bookings data
  161. */
  162. const response = await fetchWithToken('/api/users/bookings?' + searchParams.toString());
  163. const js = await parseOrRedirect(response);
  164. renderBookings(js);
  165. }