bookings.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {fetchWithToken, getTimeSpan, parseOrRedirect} 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. async function getAppointment(appointment) {
  53. const options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  54. const response = await fetch(`/api/schedule?isUser=true&appointmentId=${appointment}`, options)
  55. .catch(err => console.error('error:' + err));
  56. const js = await parseOrRedirect(response);
  57. displayAppointmentDetails(js);
  58. }
  59. function renderBookings(data) {
  60. const tableBody = document.getElementById('participants-table-body');
  61. tableBody.innerHTML = ''; // Clear existing rows
  62. data.forEach(participant => {
  63. // Create a new table row
  64. const row = document.createElement('tr');
  65. // Create table cells for first name, last name, and email
  66. const firstNameCell = document.createElement('td');
  67. const lastNameCell = document.createElement('td');
  68. const emailCell = document.createElement('td');
  69. // Populate cell content with participant data
  70. firstNameCell.textContent = participant.firstname;
  71. lastNameCell.textContent = participant.lastname;
  72. emailCell.textContent = participant.email;
  73. // Append cells to the row
  74. row.appendChild(firstNameCell);
  75. row.appendChild(lastNameCell);
  76. row.appendChild(emailCell);
  77. // Append the row to the table body
  78. tableBody.appendChild(row);
  79. });
  80. }
  81. async function fetchBookings(appointmentId) {
  82. const response = await fetchWithToken(`/api/users/bookings?appointmentId=${appointmentId}`);
  83. const js = await parseOrRedirect(response);
  84. renderBookings(js);
  85. }