| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import {fetchRegular, fetchWithToken, getTimeSpan, parseOrRedirect} from "./utils.js";
- "use strict";
- window.addEventListener("load", async function () {
- const urlParams = new URLSearchParams(window.location.search);
- const appointment = urlParams.get('appointment');
- if (!appointment) {
- window.location.replace("/html/404.html");
- }
- await getAppointment(appointment);
- await fetchBookings(appointment);
- });
- function displayAppointmentDetails(appointmentData) {
- // Retrieve the container
- const appointmentInfo = document.querySelector('.appointment-info');
- // Clear existing content
- while (appointmentInfo.firstChild) {
- appointmentInfo.removeChild(appointmentInfo.firstChild);
- }
- // Create and append the updated appointment content
- const title = document.createElement('p');
- const titleLabel = document.createElement('strong');
- titleLabel.textContent = 'Title: ';
- title.appendChild(titleLabel);
- title.appendChild(document.createTextNode(appointmentData.title));
- const description = document.createElement('p');
- const descriptionLabel = document.createElement('strong');
- descriptionLabel.textContent = 'Description: ';
- description.appendChild(descriptionLabel);
- description.appendChild(document.createTextNode(appointmentData.description));
- const place = document.createElement('p');
- const placeLabel = document.createElement('strong');
- placeLabel.textContent = 'Place: ';
- place.appendChild(placeLabel);
- place.appendChild(document.createTextNode(appointmentData.place));
- const dueDate = document.createElement('p');
- const dueDateLabel = document.createElement('strong');
- dueDateLabel.textContent = 'Due Date: ';
- dueDate.appendChild(dueDateLabel);
- dueDate.appendChild(document.createTextNode(new Date(appointmentData.dueDate).toLocaleDateString()));
- const timeSpan = document.createElement('p');
- const timeSpanLabel = document.createElement('strong');
- timeSpanLabel.textContent = 'Time Span: ';
- timeSpan.appendChild(timeSpanLabel);
- timeSpan.appendChild(document.createTextNode(getTimeSpan(appointmentData.startDate, appointmentData.endDate)));
- // Append all the updated elements
- appointmentInfo.appendChild(title);
- appointmentInfo.appendChild(description);
- appointmentInfo.appendChild(place);
- appointmentInfo.appendChild(dueDate);
- appointmentInfo.appendChild(timeSpan);
- }
- async function getAppointment(appointment) {
- const options = {method: 'GET', headers: {'User-Agent': 'insomnia/10.0.0'}};
- const searchParams = new URLSearchParams({
- appointmentId: appointment,
- isUser: true
- });
- const response = await fetchRegular(`/api/schedule?` + searchParams.toString(), options)
- .catch(err => console.error('error:' + err));
- const js = await parseOrRedirect(response);
- displayAppointmentDetails(js);
- }
- function renderBookings(data) {
- const tableBody = document.getElementById('participants-table-body');
- tableBody.innerHTML = ''; // Clear existing rows
- data.forEach(participant => {
- // Create a new table row
- const row = document.createElement('tr');
- // Create table cells for first name, last name, and email
- const firstNameCell = document.createElement('td');
- const lastNameCell = document.createElement('td');
- const emailCell = document.createElement('td');
- // Populate cell content with participant data
- firstNameCell.textContent = participant.firstname;
- lastNameCell.textContent = participant.lastname;
- emailCell.textContent = participant.email;
- // Append cells to the row
- row.appendChild(firstNameCell);
- row.appendChild(lastNameCell);
- row.appendChild(emailCell);
- // Append the row to the table body
- tableBody.appendChild(row);
- });
- }
- async function fetchBookings(appointmentId) {
- const searchParams = new URLSearchParams({
- appointmentId: appointmentId,
- });
- const response = await fetchWithToken('/api/users/bookings?' + searchParams.toString());
- const js = await parseOrRedirect(response);
- renderBookings(js);
- }
|