creator-appointments.js 3.3 KB

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