creator-appointments.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. const appointmentErrorParagraph = document.getElementById("appointment-error");
  3. const appointmentSuccessParagraph = document.getElementById("appointment-success");
  4. const titleInput = document.getElementById('title');
  5. const descriptionInput = document.getElementById('description');
  6. const dueDateInput = document.getElementById('dueDate');
  7. const startDateInput = document.getElementById('startDate');
  8. const endDateInput = document.getElementById('endDate');
  9. const placeInput = document.getElementById('place');
  10. window.addEventListener("load", async function() {
  11. const urlParams = new URLSearchParams(window.location.search);
  12. const appointment = urlParams.get('appointment');
  13. if (appointment) {
  14. // load appointment data
  15. await loadAppointment(appointment);
  16. document.getElementById("appointment-btn").innerText = "Edit Appointment";
  17. document.getElementById("form-title").innerText = "Edit Appointment";
  18. }
  19. document.getElementById("appointment-btn").addEventListener('click', async () => {
  20. if (!appointment) {
  21. await createAppointment();
  22. } else {
  23. // modify appointment
  24. // TODO Add a modify appointment function
  25. }
  26. })
  27. });
  28. async function fetchWithToken(url, options) {
  29. const authToken = localStorage.getItem('token');
  30. return await fetch(url, {...options, headers: {
  31. 'Authorization': `Bearer ${authToken}`,
  32. 'Content-Type': 'application/json',
  33. }});
  34. }
  35. async function displayAppointment(data) {
  36. titleInput.value = data.title;
  37. descriptionInput.value = data.description;
  38. dueDateInput.value = new Date(data.dueDate).toISOString().split('T')[0];
  39. startDateInput.value = new Date(data.startDate).toISOString().slice(0, 16);
  40. endDateInput.value = new Date(data.endDate).toISOString().slice(0, 16);
  41. placeInput.value = data.place;
  42. }
  43. async function loadAppointment(appointment) {
  44. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  45. const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
  46. .catch(err => console.error('error:' + err));
  47. if (response.status === 404) {
  48. window.location.replace("/html/404.html");
  49. }
  50. const js = await response.json();
  51. await displayAppointment(js);
  52. }
  53. async function createAppointment() {
  54. const title = titleInput.value;
  55. const description = descriptionInput.value;
  56. const dueDate = dueDateInput.value;
  57. const startDate = startDateInput.value;
  58. const endDate = endDateInput.value;
  59. const place = placeInput.value;
  60. if (!title || !description || !dueDate || !startDate || !endDate) {
  61. appointmentSuccessParagraph.innerText = "";
  62. appointmentErrorParagraph.innerText = "Not fields filled";
  63. return;
  64. }
  65. const response = await fetchWithToken('/api/users/create', {
  66. method: 'POST',
  67. body: JSON.stringify({
  68. title: title,
  69. description: description,
  70. dueDate: dueDate,
  71. startDate: startDate,
  72. endDate: endDate,
  73. place: place
  74. })
  75. });
  76. const js = await response.json();
  77. if (!response.ok) {
  78. appointmentSuccessParagraph.innerText = "";
  79. appointmentErrorParagraph.innerText = js["message"];
  80. } else {
  81. const path = `/html/schedule.html?appointment=${js["id"]}`;
  82. appointmentErrorParagraph.innerText = "";
  83. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  84. "was copied to your clipboard";
  85. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  86. }
  87. }