| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- "use strict";
- const appointmentErrorParagraph = document.getElementById("appointment-error");
- const appointmentSuccessParagraph = document.getElementById("appointment-success");
- const titleInput = document.getElementById('title');
- const descriptionInput = document.getElementById('description');
- const dueDateInput = document.getElementById('dueDate');
- const startDateInput = document.getElementById('startDate');
- const endDateInput = document.getElementById('endDate');
- const placeInput = document.getElementById('place');
- window.addEventListener("load", async function() {
- const urlParams = new URLSearchParams(window.location.search);
- appointment = urlParams.get('appointment');
- document.getElementById("appointment-btn").addEventListener('click', async () => {
- if (!appointment) {
- await createAppointment();
- } else {
- // modify appointment
- }
- })
- console.log(titleInput.value);
- });
- async function fetchWithToken(url, options) {
- const authToken = localStorage.getItem('token');
- return await fetch(url, {...options, headers: {
- 'Authorization': `Bearer ${authToken}`,
- 'Content-Type': 'application/json',
- }});
- }
- async function createAppointment() {
- const title = titleInput.value;
- const description = descriptionInput.value;
- const dueDate = dueDateInput.value;
- const startDate = startDateInput.value;
- const endDate = endDateInput.value;
- const place = placeInput.value;
- if (!title || !description || !dueDate || !startDate || !endDate) {
- appointmentSuccessParagraph.innerText = "";
- appointmentErrorParagraph.innerText = "Not fields filled";
- return;
- }
- const response = await fetchWithToken('/api/users/create', {
- method: 'POST',
- body: JSON.stringify({
- title: title,
- description: description,
- dueDate: dueDate,
- startDate: startDate,
- endDate: endDate,
- place: place
- })
- });
- const js = await response.json();
- if (!response.ok) {
- appointmentSuccessParagraph.innerText = "";
- appointmentErrorParagraph.innerText = js["message"];
- } else {
- const path = `/html/schedule.html?appointment=${js["id"]}`;
- appointmentErrorParagraph.innerText = "";
- appointmentSuccessParagraph.innerText = "Appointment created successfully. The share link " +
- "was copied to your clipboard";
- await navigator.clipboard.writeText(`${window.location.origin}${path}`);
- }
- }
|