schedule.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. let appointment;
  3. const errorParagraph = document.getElementById('appointment-error');
  4. const successParagraph = document.getElementById('appointment-success');
  5. window.addEventListener("load", async function () {
  6. document.getElementById("btn-attend").addEventListener("click", async () => {
  7. await attendEvent();
  8. })
  9. const urlParams = new URLSearchParams(window.location.search);
  10. appointment = urlParams.get('appointment');
  11. if (!appointment) {
  12. window.location.replace("/html/404.html");
  13. }
  14. await getAppointment();
  15. });
  16. // Function to format time as HH:MM
  17. function formatTime(date) {
  18. const hours = date.getHours().toString().padStart(2, '0'); // Get hours and ensure two digits
  19. const minutes = date.getMinutes().toString().padStart(2, '0'); // Get minutes and ensure two digits
  20. return `${hours}:${minutes}`;
  21. }
  22. // Function to get time span (from 11:00 to 12:00 format)
  23. function getTimeSpan(start, end) {
  24. const startTime = formatTime(start);
  25. const endTime = formatTime(end);
  26. return `${startTime} to ${endTime}`;
  27. }
  28. async function getAppointment() {
  29. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  30. const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
  31. .catch(err => console.error('error:' + err));
  32. if (response.status === 404) {
  33. window.location.replace("/html/404.html");
  34. }
  35. if (response.status === 410) {
  36. window.location.replace("/html/410.html");
  37. }
  38. const js = await response.json();
  39. displayAppointmentDetails(js);
  40. }
  41. function displayAppointmentDetails(appointment) {
  42. // Update the innerHTML of the elements to display the appointment details
  43. document.getElementById('appointment-date').innerText = appointment._id;
  44. document.getElementById('title').innerText = appointment.title;
  45. document.getElementById('description').innerText = appointment.description;
  46. // Format the dates to a more user-friendly format
  47. const dueDate = new Date(appointment.dueDate).toLocaleDateString();
  48. const startDate = new Date(appointment.startDate);
  49. const endDate = new Date(appointment.endDate);
  50. const startDateFormatted = startDate.toLocaleString();
  51. const endDateFormatted = endDate.toLocaleString();
  52. console.log(dueDate);
  53. console.log(startDate);
  54. console.log(endDate);
  55. document.getElementById('due-date').value = dueDate;
  56. document.getElementById('appointment-date').value = startDateFormatted;
  57. document.getElementById('time-span').value = getTimeSpan(startDate, endDate);
  58. // Set place
  59. document.getElementById('place').innerText = appointment.place;
  60. }
  61. async function attendEvent() {
  62. const email = document.getElementById('email').value;
  63. const firstname = document.getElementById('firstname').value;
  64. const lastname = document.getElementById('lastname').value;
  65. if (!email || !firstname || !lastname) {
  66. errorParagraph.innerText = "Fill in all fields.";
  67. return;
  68. }
  69. const response = await fetch("/api/schedule/create",
  70. {
  71. method: 'POST',
  72. headers: {'Content-Type': 'application/json'},
  73. body: JSON.stringify({
  74. appointment: appointment,
  75. email: email,
  76. firstname: firstname,
  77. lastname: lastname
  78. })
  79. });
  80. const js = await response.json();
  81. if (response.status !== 200) {
  82. errorParagraph.innerText = js["message"];
  83. successParagraph.innerText = "";
  84. } else {
  85. errorParagraph.innerText = "";
  86. successParagraph.innerText = "Registered successfully.";
  87. }
  88. }