bookings.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { fetchWithToken } from "./utils.js";
  2. "use strict";
  3. window.addEventListener("load", async function () {
  4. const urlParams = new URLSearchParams(window.location.search);
  5. const appointment = urlParams.get('appointment');
  6. if (!appointment) {
  7. window.location.replace("/html/404.html");
  8. }
  9. await getAppointment(appointment);
  10. await fetchBookings(appointment);
  11. });
  12. function displayAppointmentDetails(appointmentData) {
  13. // Retrieve the container
  14. const appointmentInfo = document.querySelector('.appointment-info');
  15. // Clear existing content
  16. while (appointmentInfo.firstChild) {
  17. appointmentInfo.removeChild(appointmentInfo.firstChild);
  18. }
  19. // Create and append the updated appointment content
  20. const title = document.createElement('p');
  21. const titleLabel = document.createElement('strong');
  22. titleLabel.textContent = 'Title: ';
  23. title.appendChild(titleLabel);
  24. title.appendChild(document.createTextNode(appointmentData.title));
  25. const description = document.createElement('p');
  26. const descriptionLabel = document.createElement('strong');
  27. descriptionLabel.textContent = 'Description: ';
  28. description.appendChild(descriptionLabel);
  29. description.appendChild(document.createTextNode(appointmentData.description));
  30. const place = document.createElement('p');
  31. const placeLabel = document.createElement('strong');
  32. placeLabel.textContent = 'Place: ';
  33. place.appendChild(placeLabel);
  34. place.appendChild(document.createTextNode(appointmentData.place));
  35. const dueDate = document.createElement('p');
  36. const dueDateLabel = document.createElement('strong');
  37. dueDateLabel.textContent = 'Due Date: ';
  38. dueDate.appendChild(dueDateLabel);
  39. dueDate.appendChild(document.createTextNode(new Date(appointmentData.dueDate).toLocaleDateString()));
  40. const timeSpan = document.createElement('p');
  41. const timeSpanLabel = document.createElement('strong');
  42. timeSpanLabel.textContent = 'Time Span: ';
  43. timeSpan.appendChild(timeSpanLabel);
  44. timeSpan.appendChild(document.createTextNode(getTimeSpan(appointmentData.startDate, appointmentData.endDate)));
  45. // Append all the updated elements
  46. appointmentInfo.appendChild(title);
  47. appointmentInfo.appendChild(description);
  48. appointmentInfo.appendChild(place);
  49. appointmentInfo.appendChild(dueDate);
  50. appointmentInfo.appendChild(timeSpan);
  51. }
  52. // Helper function to format the time span
  53. function getTimeSpan(startDate, endDate) {
  54. const start = new Date(startDate).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  55. const end = new Date(endDate).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  56. return `${start} to ${end}`;
  57. }
  58. async function getAppointment(appointment) {
  59. const options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  60. const response = await fetch(`/api/schedule?isUser=true&appointmentId=${appointment}`, options)
  61. .catch(err => console.error('error:' + err));
  62. if (response.status === 404) {
  63. window.location.replace("/html/404.html");
  64. }
  65. const js = await response.json();
  66. displayAppointmentDetails(js);
  67. }
  68. function renderBookings(data) {
  69. const tableBody = document.getElementById('participants-table-body');
  70. tableBody.innerHTML = ''; // Clear existing rows
  71. data.forEach(participant => {
  72. // Create a new table row
  73. const row = document.createElement('tr');
  74. // Create table cells for first name, last name, and email
  75. const firstNameCell = document.createElement('td');
  76. const lastNameCell = document.createElement('td');
  77. const emailCell = document.createElement('td');
  78. // Populate cell content with participant data
  79. firstNameCell.textContent = participant.firstname;
  80. lastNameCell.textContent = participant.lastname;
  81. emailCell.textContent = participant.email;
  82. // Append cells to the row
  83. row.appendChild(firstNameCell);
  84. row.appendChild(lastNameCell);
  85. row.appendChild(emailCell);
  86. // Append the row to the table body
  87. tableBody.appendChild(row);
  88. });
  89. }
  90. async function fetchBookings(appointmentId) {
  91. const response = await fetchWithToken(`/api/users/bookings?appointmentId=${appointmentId}`);
  92. if (response.status === 401) {
  93. window.location.replace("/html/auth.html");
  94. return;
  95. }
  96. const js = await response.json();
  97. if (!response.ok) {
  98. window.location.replace("/html/404.html");
  99. }
  100. renderBookings(js);
  101. }