source: src/main/resources/static/js/calendar.js@ 743de55

Last change on this file since 743de55 was 743de55, checked in by macagaso <gasoskamarija@…>, 6 weeks ago

Initial commit

  • Property mode set to 100644
File size: 6.1 KB
Line 
1let calendar = document.querySelector('.calendar')
2
3const month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
4function showModal() {
5 const modal = document.getElementById('confirmation-modal');
6 modal.classList.add('modal');
7 modal.style.display = 'block';
8}
9document.getElementById("cancel-booking").addEventListener("click", function() {
10 window.location.reload();
11});
12
13document.getElementById('book-button').addEventListener('click', () => {
14 if (!window.selectedTime) {
15 alert('Please select an appointment time.');
16 return;
17 }
18 else{
19 document.getElementById("last-check").innerHTML=window.selectedTime;
20 showModal();
21 }
22
23});
24
25document.getElementById("confirm-booking").addEventListener("click",function(){
26
27 const termData={
28 term: window.selectedTime,
29 couponCode: document.getElementById("coupon-type").value,
30 medicalCondition:document.getElementById("medical-condition").value
31 };
32 fetch('/api/requests/book', {
33 method: 'POST',
34 headers: {
35 'Content-Type': 'application/json'
36 },
37 body: JSON.stringify(termData)
38 })
39 .then(response => {
40 if (response.ok) {
41 alert('Appointment booked successfully!');
42 document.getElementById('confirmation-modal').style.display = 'none';
43 window.selectedTime = null;
44 document.getElementById('book-button').disabled = true;
45 } else {
46 alert('Failed to book the appointment.');
47 }
48 })
49 .catch(error => {
50 console.error('Error:', error);
51 alert('An error occurred while booking the appointment.');
52 });
53});
54
55isLeapYear = (year) => {
56 return (year % 4 === 0 && year % 100 !== 0 && year % 400 !== 0) || (year % 100 === 0 && year % 400 ===0)
57}
58
59getFebDays = (year) => {
60 return isLeapYear(year) ? 29 : 28
61}
62function fetchFreeOrPendingAppointments(date) {
63 fetch(`/api/appointments/free?date=${date}`)
64 .then(response => response.json())
65 .then(data => {
66 console.log(data);
67 displayFreeAppointments(data);
68 })
69 .catch(error => console.error('Error fetching free appointments:', error));
70}
71function displayFreeAppointments(appointments) {
72 const container = document.getElementById('hourly-terms');
73 container.innerHTML = ''; // Clear previous appointments
74
75 appointments.forEach(appointment => {
76 const appointmentDiv = document.createElement('div');
77 appointmentDiv.classList.add('appointment-item');
78 appointmentDiv.style.border = '1px solid black';
79 appointmentDiv.style.padding = '20px';
80 appointmentDiv.style.display = 'inline-block';
81 appointmentDiv.style.marginRight = '10px';
82
83 const appointmentDate = new Date(appointment.term);
84 const timeOptions = { hour: '2-digit', minute: '2-digit', hour12: false };
85 const dateOptions = { year: 'numeric', month: '2-digit', day: '2-digit' };
86 const formattedTime = appointmentDate.toLocaleTimeString([], timeOptions);
87 const formattedDate=appointmentDate.toLocaleDateString('en-CA', dateOptions);
88
89 appointmentDiv.textContent = formattedTime;
90 appointmentDiv.dataset.time = formattedDate+" "+formattedTime; // Store full date-time
91
92 appointmentDiv.addEventListener('click', () => {
93 window.selectedTime = appointmentDiv.dataset.time;
94 document.getElementById('book-button').disabled = false;
95 });
96
97 container.appendChild(appointmentDiv);
98 });
99}
100
101generateCalendar = (month, year) => {
102
103 let calendar_days = calendar.querySelector('.calendar-days')
104 let calendar_header_year = calendar.querySelector('#year')
105
106 let days_of_month = [31, getFebDays(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
107
108 calendar_days.innerHTML = ''
109
110 let currDate = new Date()
111 if (!month) month = currDate.getMonth()
112 if (!year) year = currDate.getFullYear()
113
114 let curr_month = `${month_names[month]}`
115 month_picker.innerHTML = curr_month
116 calendar_header_year.innerHTML = year
117
118
119 let first_day = new Date(year, month, 1)
120
121 for (let i = 0; i <= days_of_month[month] + first_day.getDay() - 1; i++) {
122 let day = document.createElement('div')
123 if (i >= first_day.getDay()) {
124 day.classList.add('calendar-day-hover')
125 day.innerHTML = i - first_day.getDay() + 1;
126 day.innerHTML += `<span></span>
127 <span></span>
128 <span></span>
129 <span></span>`;
130 let selectedDate = `${year}-${(month + 1).toString().padStart(2, '0')}-${(i - first_day.getDay() + 1).toString().padStart(2, '0')}`;
131 day.addEventListener('click', () => fetchFreeOrPendingAppointments(selectedDate));
132
133 if (i - first_day.getDay() + 1 === currDate.getDate() && year === currDate.getFullYear() && month === currDate.getMonth()) {
134 day.classList.add('curr-date')
135 }
136 }
137 calendar_days.appendChild(day)
138 }
139}
140
141let month_list = calendar.querySelector('.month-list')
142
143month_names.forEach((e, index) => {
144 let month = document.createElement('div')
145 month.innerHTML = `<div data-month="${index}">${e}</div>`
146 month.querySelector('div').onclick = () => {
147 month_list.classList.remove('show')
148 curr_month.value = index
149 generateCalendar(index, curr_year.value)
150 }
151 month_list.appendChild(month)
152})
153
154let month_picker = calendar.querySelector('#month-picker')
155
156month_picker.onclick = () => {
157 month_list.classList.add('show')
158}
159
160let currDate = new Date()
161
162let curr_month = {value: currDate.getMonth()}
163let curr_year = {value: currDate.getFullYear()}
164
165generateCalendar(curr_month.value, curr_year.value)
166
167document.querySelector('#prev-year').onclick = () => {
168 --curr_year.value
169 generateCalendar(curr_month.value, curr_year.value)
170}
171
172document.querySelector('#next-year').onclick = () => {
173 ++curr_year.value
174 generateCalendar(curr_month.value, curr_year.value)
175}
176
177
178
Note: See TracBrowser for help on using the repository browser.