schedule.js 3.0 KB

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