bookings.js 5.7 KB

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