creator-appointments.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.onload = async function() {
  11. document.getElementById("appointment-btn").addEventListener('click', async () => {
  12. await createAppointment();
  13. })
  14. }
  15. async function fetchWithToken(url, options) {
  16. const authToken = localStorage.getItem('token');
  17. return await fetch(url, {...options, headers: {
  18. 'Authorization': `Bearer ${authToken}`,
  19. 'Content-Type': 'application/json',
  20. }});
  21. }
  22. async function createAppointment() {
  23. const title = titleInput.value;
  24. const description = descriptionInput.value;
  25. const dueDate = dueDateInput.value;
  26. const startDate = startDateInput.value;
  27. const endDate = endDateInput.value;
  28. const place = placeInput.value;
  29. if (!title || !description || !dueDate || !startDate || !endDate) {
  30. appointmentSuccessParagraph.innerText = "";
  31. appointmentErrorParagraph.innerText = "Not fields filled";
  32. return;
  33. }
  34. const response = await fetchWithToken('/api/users/create', {
  35. method: 'POST',
  36. body: JSON.stringify({
  37. title: title,
  38. description: description,
  39. dueDate: dueDate,
  40. startDate: startDate,
  41. endDate: endDate,
  42. place: place
  43. })
  44. });
  45. const js = await response.json();
  46. if (!response.ok) {
  47. appointmentSuccessParagraph.innerText = "";
  48. appointmentErrorParagraph.innerText = js["message"];
  49. } else {
  50. appointmentErrorParagraph.innerText = "";
  51. appointmentSuccessParagraph.innerText = "Appointment created successfully.";
  52. }
  53. }