source: frontend/js/formHandler.js@ cd64b06

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

Added info scraping for escape travel

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