source: frontend/js/formHandler.js@ 1c51912

Last change on this file since 1c51912 was 1c51912, checked in by Kristijan <kristijanzafirovski26@…>, 5 days ago

Added details for magelan

  • Property mode set to 100644
File size: 6.8 KB
Line 
1document.addEventListener('DOMContentLoaded', function() {
2 const form = document.getElementById('form-id');
3 const loadingOverlay = document.getElementById('loadingOverlay');
4
5 form.addEventListener('submit', function(event) {
6 event.preventDefault(); // Prevent the default form submission
7
8 // Collect form data
9 const formData = new FormData(form);
10 const data = {};
11 let isValid = true;
12 let errorMessage = "";
13
14 // Validate form data
15 formData.forEach((value, key) => {
16 data[key] = value;
17 if (key === "departureDate") {
18 const today = new Date();
19 const departureDate = new Date(value);
20 if (departureDate <= today) {
21 isValid = false;
22 errorMessage += "Датумот на поаѓање е грешен!.\n";
23 }
24 }
25 if (key === "destination" && !value) {
26 isValid = false;
27 errorMessage += "Внесете дестинација.\n";
28 }
29 });
30
31 if (!isValid) {
32 alert(errorMessage);
33 return;
34 }
35
36 loadingOverlay.style.display = 'flex';
37
38 // Send form data to the server
39 fetch('/submit', {
40 method: 'POST',
41 headers: {
42 'Content-Type': 'application/json'
43 },
44 body: JSON.stringify(data)
45 })
46 .then(response => response.json())
47 .then(data => {
48 loadingOverlay.style.display = 'none';
49
50 const dataList = document.getElementById('givenOptions');
51 dataList.innerHTML = '';
52 if (data.length === 0) {
53 const wrapper = document.createElement('div');
54 const text = document.createElement('p');
55 text.textContent = "Не се пронајдени резултати за вашето пребарување!";
56 wrapper.appendChild(text);
57 dataList.appendChild(wrapper);
58 }
59 data.forEach(item => {
60
61 const optionDiv = document.createElement('div');
62 optionDiv.classList.add('option');
63
64 //image
65 const img = document.createElement('img');
66 img.classList.add('image');
67 img.src = item.imgSrc;
68 optionDiv.appendChild(img);
69 const WrapperDiv = document.createElement('div');
70 optionDiv.appendChild(WrapperDiv);
71 //hotel
72 const nameParagraph = document.createElement('p');
73 nameParagraph.id = 'name';
74 nameParagraph.textContent = item.hotelName;
75 WrapperDiv.appendChild(nameParagraph);
76 //Destination
77 const countryParagraph = document.createElement('p');
78 countryParagraph.id = 'country';
79 countryParagraph.style.fontSize = '20px';
80 countryParagraph.textContent = item.country;
81 WrapperDiv.appendChild(countryParagraph);
82 const dateParagraph = document.createElement('h2');
83 dateParagraph.id = 'date';
84 dateParagraph.textContent = item.dateRange;
85 WrapperDiv.appendChild(dateParagraph);
86 const peopleParaghraph = document.createElement('p');
87 peopleParaghraph.id = 'numPeople';
88 if(item.numPeople === 1){
89 peopleParaghraph.textContent = item.numPeople + " лице";
90 }
91 else peopleParaghraph.textContent = item.numPeople + " лица";
92 WrapperDiv.appendChild(peopleParaghraph);
93 //price
94 const priceHeading = document.createElement('h1');
95 priceHeading.textContent = 'Цена:';
96 WrapperDiv.appendChild(priceHeading);
97 const priceParagraph = document.createElement('h2');
98 priceParagraph.id = 'price';
99 priceParagraph.textContent = item.price + "EUR";
100 WrapperDiv.appendChild(priceParagraph);
101
102
103 const infoDiv = document.createElement('div');
104 const typeRoom = document.createElement('p');
105 typeRoom.textContent = "Тип на соба: " + item.type;
106 infoDiv.appendChild(typeRoom);
107 const plan = document.createElement('p');
108 plan.textContent = "Услуга: " + item.board;
109 infoDiv.appendChild(plan);
110 const amenities = document.createElement('p');
111 if(item.amenities.length === 0){
112 amenities.textContent = "Нема информации од агенцијата за вклучените поволности. ";
113 }else amenities.textContent = "Поволности: " + item.amenities;
114 infoDiv.appendChild(amenities);
115 optionDiv.appendChild(infoDiv);
116
117
118 // link and button
119 const link = document.createElement('a');
120 link.id = 'link';
121 link.href = item.link; // Use item.link if available
122 link.target = '_blank'; // Open link in new tab
123
124 const button = document.createElement('button');
125 button.classList.add('btn', 'login-button');
126 button.textContent = 'Линк до страна';
127 const btnWrapDiv = document.createElement('div');
128 btnWrapDiv.classList.add('btnWrapper');
129 const favBtn = document.createElement('button');
130 favBtn.classList.add('favBtn','btn');
131 favBtn.id = 'favBtnId';
132 favBtn.textContent = 'Додадете во омилени';
133 favBtn.setAttribute('data-option-id', item.id);
134 favBtn.addEventListener('click', function () {
135 const optionId = this.getAttribute('data-option-id');
136 saveFavoriteOption(optionId);
137 });
138
139 btnWrapDiv.appendChild(link);
140 link.appendChild(button);
141 btnWrapDiv.appendChild(favBtn);
142
143 optionDiv.appendChild(btnWrapDiv);
144 dataList.appendChild(optionDiv);
145 });
146 updateFavoriteButtons();
147 })
148 .catch(error => {
149 console.error('Error fetching data:', error);
150 loadingOverlay.style.display = 'none';
151 });
152 });
153});
Note: See TracBrowser for help on using the repository browser.