schedule.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. window.onload = async function () {
  3. await getAppointment();
  4. }
  5. // Function to format time as HH:MM
  6. function formatTime(date) {
  7. const hours = date.getHours().toString().padStart(2, '0'); // Get hours and ensure two digits
  8. const minutes = date.getMinutes().toString().padStart(2, '0'); // Get minutes and ensure two digits
  9. return `${hours}:${minutes}`;
  10. }
  11. // Function to get time span (from 11:00 to 12:00 format)
  12. function getTimeSpan(start, end) {
  13. const startTime = formatTime(start);
  14. const endTime = formatTime(end);
  15. return `${startTime} to ${endTime}`;
  16. }
  17. async function getAppointment() {
  18. const urlParams = new URLSearchParams(window.location.search);
  19. const appointment = urlParams.get('appointment');
  20. if (!appointment) {
  21. window.location.replace("/html/404.html");
  22. }
  23. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  24. const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
  25. .catch(err => console.error('error:' + err));
  26. if (response.status === 404) {
  27. window.location.replace("/html/404.html");
  28. }
  29. if (response.status === 410) {
  30. window.location.replace("/html/410.html");
  31. }
  32. const js = await response.json();
  33. displayAppointmentDetails(js);
  34. }
  35. function displayAppointmentDetails(appointment) {
  36. // Update the innerHTML of the elements to display the appointment details
  37. document.getElementById('appointment-date').innerText = appointment._id;
  38. document.getElementById('title').innerText = appointment.title;
  39. document.getElementById('description').innerText = appointment.description;
  40. // Format the dates to a more user-friendly format
  41. const dueDate = new Date(appointment.dueDate).toLocaleDateString();
  42. const startDate = new Date(appointment.startDate);
  43. const endDate = new Date(appointment.endDate);
  44. const startDateFormatted = startDate.toLocaleString();
  45. const endDateFormatted = endDate.toLocaleString();
  46. console.log(dueDate);
  47. console.log(startDate);
  48. console.log(endDate);
  49. document.getElementById('due-date').value = dueDate;
  50. document.getElementById('appointment-date').value = startDateFormatted;
  51. document.getElementById('time-span').value = getTimeSpan(startDate, endDate);
  52. // Set place
  53. document.getElementById('place').innerText = appointment.place;
  54. }