creator-appointments.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. console.log(titleInput.value);
  28. });
  29. async function fetchWithToken(url, options) {
  30. const authToken = localStorage.getItem('token');
  31. return await fetch(url, {...options, headers: {
  32. 'Authorization': `Bearer ${authToken}`,
  33. 'Content-Type': 'application/json',
  34. }});
  35. }
  36. async function displayAppointment(data) {
  37. titleInput.value = data.title;
  38. descriptionInput.value = data.description;
  39. dueDateInput.value = new Date(data.dueDate).toISOString().split('T')[0];
  40. startDateInput.value = new Date(data.startDate).toISOString().slice(0, 16);
  41. endDateInput.value = new Date(data.endDate).toISOString().slice(0, 16);
  42. placeInput.value = data.place;
  43. }
  44. async function loadAppointment(appointment) {
  45. let options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
  46. const response = await fetch(`/api/schedule?appointmentId=${appointment}`, options)
  47. .catch(err => console.error('error:' + err));
  48. if (response.status === 404) {
  49. window.location.replace("/html/404.html");
  50. }
  51. const js = await response.json();
  52. await displayAppointment(js);
  53. }
  54. async function createAppointment() {
  55. const title = titleInput.value;
  56. const description = descriptionInput.value;
  57. const dueDate = dueDateInput.value;
  58. const startDate = startDateInput.value;
  59. const endDate = endDateInput.value;
  60. const place = placeInput.value;
  61. if (!title || !description || !dueDate || !startDate || !endDate) {
  62. appointmentSuccessParagraph.innerText = "";
  63. appointmentErrorParagraph.innerText = "Not fields filled";
  64. return;
  65. }
  66. const response = await fetchWithToken('/api/users/create', {
  67. method: 'POST',
  68. body: JSON.stringify({
  69. title: title,
  70. description: description,
  71. dueDate: dueDate,
  72. startDate: startDate,
  73. endDate: endDate,
  74. place: place
  75. })
  76. });
  77. const js = await response.json();
  78. if (!response.ok) {
  79. appointmentSuccessParagraph.innerText = "";
  80. appointmentErrorParagraph.innerText = js["message"];
  81. } else {
  82. const path = `/html/schedule.html?appointment=${js["id"]}`;
  83. appointmentErrorParagraph.innerText = "";
  84. appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
  85. "was copied to your clipboard";
  86. await navigator.clipboard.writeText(`${window.location.origin}${path}`);
  87. }
  88. }