source: frontend/js/formHandler.js@ 53bad7e

Last change on this file since 53bad7e was 53bad7e, checked in by Kristijan <kristijanzafirovski26@…>, 6 days ago

dodadeno informacii za broj na lugje

  • Property mode set to 100644
File size: 6.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(); // 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
77 //Destination
78 const countryParagraph = document.createElement('p');
79 countryParagraph.id = 'country';
80 countryParagraph.style.fontSize = '20px';
81 countryParagraph.textContent = item.country;
82 WrapperDiv.appendChild(countryParagraph);
83 const dateParagraph = document.createElement('h2');
84 dateParagraph.id = 'date';
85 dateParagraph.textContent = item.dateRange;
86 WrapperDiv.appendChild(dateParagraph);
87 const peopleParaghraph = document.createElement('p');
88 peopleParaghraph.id = 'numPeople';
89 if(item.numPeople === 1){
90 peopleParaghraph.textContent = item.numPeople + " лице";
91 }
92 else peopleParaghraph.textContent = item.numPeople + " лица";
93 WrapperDiv.appendChild(peopleParaghraph);
94
95 const infoDiv = document.createElement('div');
96
97
98
99
100 //price
101 const priceHeading = document.createElement('h1');
102 priceHeading.textContent = 'Цена:';
103 WrapperDiv.appendChild(priceHeading);
104 const priceParagraph = document.createElement('h2');
105 priceParagraph.id = 'price';
106 priceParagraph.textContent = item.price + "EUR";
107 WrapperDiv.appendChild(priceParagraph);
108
109
110
111 // link and button
112 const link = document.createElement('a');
113 link.id = 'link';
114 link.href = item.link; // Use item.link if available
115 link.target = '_blank'; // Open link in new tab
116
117 const button = document.createElement('button');
118 button.classList.add('btn', 'login-button');
119 button.textContent = 'Линк до страна';
120 const btnWrapDiv = document.createElement('div');
121 btnWrapDiv.classList.add('btnWrapper');
122 const favBtn = document.createElement('button');
123 favBtn.classList.add('favBtn','btn');
124 favBtn.id = 'favBtnId';
125 favBtn.textContent = 'Додадете во омилени';
126 favBtn.setAttribute('data-option-id', item.id);
127 favBtn.addEventListener('click', function () {
128 const optionId = this.getAttribute('data-option-id');
129 saveFavoriteOption(optionId);
130 });
131
132 btnWrapDiv.appendChild(link);
133 link.appendChild(button);
134 btnWrapDiv.appendChild(favBtn);
135
136 optionDiv.appendChild(btnWrapDiv);
137 dataList.appendChild(optionDiv);
138 });
139 updateFavoriteButtons();
140 })
141 .catch(error => {
142 console.error('Error fetching data:', error);
143 loadingOverlay.style.display = 'none';
144 });
145 });
146});
Note: See TracBrowser for help on using the repository browser.