source: frontend/js/formHandler.js@ c164f8f

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

pred-finalna

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[c164f8f]1document.addEventListener('DOMContentLoaded', function() {
2 const form = document.getElementById('form-id'); // Ensure the form has the correct 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
32 if (!isValid) {
33 alert(errorMessage);
34 return;
35 }
36
37 // Show the loading overlay
38 loadingOverlay.style.display = 'flex';
39
40 // Send form data to the server
41 fetch('/submit', {
42 method: 'POST',
43 headers: {
44 'Content-Type': 'application/json'
45 },
46 body: JSON.stringify(data)
47 })
48 .then(response => response.json())
49 .then(data => {
50
51 loadingOverlay.style.display = 'none';
52
53 const dataList = document.getElementById('givenOptions');
54 dataList.innerHTML = '';
55 if (data.length === 0) {
56 const wrapper = document.createElement('div');
57 const text = document.createElement('p');
58 text.textContent = "Не се пронајдени резултати за вашето пребарување!";
59 wrapper.appendChild(text);
60 dataList.appendChild(wrapper);
61 }
62 data.forEach(item => {
63
64 const optionDiv = document.createElement('div');
65 optionDiv.classList.add('option');
66 //Option from
67
68 //image
69 const img = document.createElement('img');
70 img.classList.add('image');
71 img.src = item.imgSrc; // Use item.imageSrc if available
72 optionDiv.appendChild(img);
73 const WrapperDiv = document.createElement('div');
74 optionDiv.appendChild(WrapperDiv);
75 //hotel
76 const nameParagraph = document.createElement('p');
77 nameParagraph.id = 'name';
78 nameParagraph.textContent = item.hotelName;
79 WrapperDiv.appendChild(nameParagraph);
80
81 //Destination
82 const countryParagraph = document.createElement('p');
83 countryParagraph.id = 'country';
84 countryParagraph.style.fontSize = '20px';
85 countryParagraph.textContent = item.country;
86 WrapperDiv.appendChild(countryParagraph);
87 const dateParagraph = document.createElement('h2');
88 dateParagraph.id = 'date';
89 dateParagraph.textContent = item.dateRange;
90 WrapperDiv.appendChild(dateParagraph);
91 //price
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 + "EUR";
98 WrapperDiv.appendChild(priceParagraph);
99
100 // link and button
101 const link = document.createElement('a');
102 link.id = 'link';
103 link.href = item.link; // Use item.link if available
104 link.target = '_blank'; // Open link in new tab
105
106 const button = document.createElement('button');
107 button.classList.add('btn', 'login-button');
108 button.textContent = 'Линк до страна';
109 const btnWrapDiv = document.createElement('div');
110 btnWrapDiv.classList.add('btnWrapper');
111 const favBtn = document.createElement('button');
112 favBtn.classList.add('favBtn','btn');
113 favBtn.id = 'favBtnId';
114 favBtn.textContent = 'Додадете во омилени';
115 favBtn.setAttribute('data-option-id', item.id);
116 favBtn.addEventListener('click', function () {
117 const optionId = this.getAttribute('data-option-id');
118 saveFavoriteOption(optionId);
119 });
120
121 btnWrapDiv.appendChild(link);
122 link.appendChild(button);
123 btnWrapDiv.appendChild(favBtn);
124
125 optionDiv.appendChild(btnWrapDiv);
126 // Append option div to dataList
127 dataList.appendChild(optionDiv);
128 });
129 updateFavoriteButtons();
130 })
131 .catch(error => {
132 console.error('Error fetching data:', error);
133 // Hide the loading overlay in case of error
134 loadingOverlay.style.display = 'none';
135 });
136 });
137});
Note: See TracBrowser for help on using the repository browser.