[c164f8f] | 1 | function getUserDetails() {
|
---|
| 2 | return JSON.parse(sessionStorage.getItem('user'));
|
---|
| 3 | }
|
---|
| 4 |
|
---|
| 5 | function deleteAccount() {
|
---|
| 6 | fetch('/delete-account', {
|
---|
| 7 | method: 'POST',
|
---|
| 8 | headers: {
|
---|
| 9 | 'Content-Type': 'application/json'
|
---|
| 10 | },
|
---|
| 11 | body: JSON.stringify(getUserDetails())
|
---|
| 12 | })
|
---|
| 13 | .then(response => response.json())
|
---|
| 14 | .then(data => {
|
---|
| 15 | if (data.success) {
|
---|
| 16 | sessionStorage.clear();
|
---|
| 17 | logout();
|
---|
| 18 | alert('Account deleted successfully');
|
---|
| 19 | } else {
|
---|
| 20 | alert('Failed to delete account: ' + data.message);
|
---|
| 21 | }
|
---|
| 22 | })
|
---|
| 23 | .catch(error => {
|
---|
| 24 | console.error('Error:', error);
|
---|
| 25 | alert('Failed to delete account: ' + error.message);
|
---|
| 26 | });
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | function logout() {
|
---|
| 30 | sessionStorage.clear();
|
---|
| 31 | document.cookie = "sessionId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
---|
| 32 | showLoggedOutState();
|
---|
| 33 | fetch('/account/logout', {
|
---|
| 34 | method: 'POST',
|
---|
| 35 | credentials: 'include'
|
---|
| 36 | });
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | function openAccountDetails() {
|
---|
| 40 | document.getElementById('AccountDetailsOverlay').style.display = 'block';
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | function confirmDelete() {
|
---|
| 44 | document.getElementById('confDel').style.display = 'block';
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | function closeConfirmDelete() {
|
---|
| 48 | document.getElementById('confDel').style.display = 'none';
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | function openLoginPopup() {
|
---|
| 52 | document.getElementById('loginPopup').style.display = 'block';
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | function closeLoginPopup() {
|
---|
| 56 | document.getElementById('loginPopup').style.display = 'none';
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | function openRegisterPopup() {
|
---|
| 60 | document.getElementById('registerPopup').style.display = 'block';
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | function closeRegisterPopup() {
|
---|
| 64 | document.getElementById('registerPopup').style.display = 'none';
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | function showLoggedInState() {
|
---|
| 68 | document.getElementById('loginButton').style.display = 'none';
|
---|
| 69 | document.getElementById('logoutButton').style.display = 'block';
|
---|
| 70 | document.getElementById('savedTripsButton').style.display = 'block';
|
---|
| 71 | document.getElementById('accBtn').style.display = 'block';
|
---|
| 72 |
|
---|
| 73 | var style = document.createElement('style');
|
---|
| 74 | style.innerHTML = '.favBtn { display: block; }';
|
---|
| 75 | document.head.appendChild(style);
|
---|
| 76 |
|
---|
| 77 | if (sessionStorage.getItem('isAdmin') === 'true') {
|
---|
| 78 | document.getElementById('adminPanelButton').style.display = 'block';
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | function showMessage(elementId, type, message) {
|
---|
| 84 | const element = document.getElementById(elementId);
|
---|
| 85 | if (!element) {
|
---|
| 86 | console.error(`Element with id ${elementId} not found`);
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | element.textContent = message;
|
---|
| 90 | element.className = `message ${type}`;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | function showLoggedOutState() {
|
---|
| 95 | document.getElementById('loginButton').style.display = 'block';
|
---|
| 96 | document.getElementById('logoutButton').style.display = 'none';
|
---|
| 97 | document.getElementById('savedTripsButton').style.display = 'none';
|
---|
| 98 | document.getElementById('accBtn').style.display = 'none';
|
---|
| 99 | var style = document.createElement('style');
|
---|
| 100 | style.innerHTML = '.favBtn { display: none; }';
|
---|
| 101 | document.head.appendChild(style);
|
---|
| 102 | document.getElementById('loginResponse').style.display = 'none';
|
---|
| 103 | document.getElementById('adminPanelButton').style.display = 'none';
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | function goToAdminPanel() {
|
---|
| 107 | window.location.href = '/admin-panel';
|
---|
| 108 | }
|
---|
| 109 | function showLoginFeedback(success, message) {
|
---|
| 110 | const loginResponse = document.getElementById('loginResponse');
|
---|
| 111 | loginResponse.textContent = message;
|
---|
| 112 | loginResponse.style.color = success ? 'green' : 'red';
|
---|
| 113 | loginResponse.style.display = 'block';
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | window.onclick = function(event) {
|
---|
| 117 | const loginPopup = document.getElementById('loginPopup');
|
---|
| 118 | const registerPopup = document.getElementById('registerPopup');
|
---|
| 119 | const accountPopup = document.getElementById('AccountDetailsOverlay');
|
---|
| 120 | if (event.target === loginPopup) {
|
---|
| 121 | loginPopup.style.display = 'none';
|
---|
| 122 | }
|
---|
| 123 | if (event.target === registerPopup) {
|
---|
| 124 | registerPopup.style.display = 'none';
|
---|
| 125 | }
|
---|
| 126 | if (event.target === accountPopup) {
|
---|
| 127 | accountPopup.style.display = 'none';
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | document.addEventListener('DOMContentLoaded', function() {
|
---|
| 132 | checkSession();
|
---|
| 133 |
|
---|
| 134 | document.querySelectorAll('.favBtn').forEach(button => {
|
---|
| 135 | button.addEventListener('click', function() {
|
---|
| 136 | const optionId = this.getAttribute('data-option-id');
|
---|
| 137 | saveFavoriteOption(optionId);
|
---|
| 138 | });
|
---|
| 139 | });
|
---|
| 140 |
|
---|
| 141 | document.getElementById('savedTripsButton').addEventListener('click', function() {
|
---|
| 142 | document.getElementById('savedTripsOverlay').style.display = 'block';
|
---|
| 143 | fetchSavedTrips();
|
---|
| 144 | //fetchPriceChanges();
|
---|
| 145 | });
|
---|
| 146 |
|
---|
| 147 | document.querySelector('.close').addEventListener('click', function() {
|
---|
| 148 | document.getElementById('savedTripsOverlay').style.display = 'none';
|
---|
| 149 | });
|
---|
| 150 | });
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | function saveFavoriteOption(optionId) {
|
---|
| 154 | const mail = sessionStorage.getItem('user').replace(/^"|"$/g, '');
|
---|
| 155 | fetch('/save-favorite', {
|
---|
| 156 | method: 'POST',
|
---|
| 157 | headers: {
|
---|
| 158 | 'Content-Type': 'application/json'
|
---|
| 159 | },
|
---|
| 160 | body: JSON.stringify({ optionId: optionId, user: mail })
|
---|
| 161 | })
|
---|
| 162 | .then(response => {
|
---|
| 163 | if (response.ok) {
|
---|
| 164 | return response.json();
|
---|
| 165 | }
|
---|
| 166 | throw new Error('Network response was not ok.');
|
---|
| 167 | })
|
---|
| 168 | .then(data => {
|
---|
| 169 | console.log('Option saved:', data);
|
---|
| 170 | // Update the UI to reflect that the option was saved
|
---|
| 171 | updateFavoriteButtons();
|
---|
| 172 | })
|
---|
| 173 | .catch(error => {
|
---|
| 174 | console.error('Error:', error);
|
---|
| 175 | });
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | function updateFavoriteButtons() {
|
---|
| 179 | const mail = sessionStorage.getItem('user').replace(/^"|"$/g, '');
|
---|
| 180 | fetch('/get-saved-trips', {
|
---|
| 181 | method: 'POST',
|
---|
| 182 | headers: {
|
---|
| 183 | 'Content-Type': 'application/json'
|
---|
| 184 | },
|
---|
| 185 | body: JSON.stringify({ userEmail: mail })
|
---|
| 186 | })
|
---|
| 187 | .then(response => response.json())
|
---|
| 188 | .then(data => {
|
---|
| 189 | // Extract the option IDs of saved trips
|
---|
| 190 | const savedOptionIds = data.savedTrips.map(trip => trip.id);
|
---|
| 191 |
|
---|
| 192 | document.querySelectorAll('.favBtn').forEach(button => {
|
---|
| 193 | const optionId = parseInt(button.getAttribute('data-option-id'), 10);
|
---|
| 194 | if (savedOptionIds.includes(optionId)) {
|
---|
| 195 | button.disabled = true;
|
---|
| 196 | button.textContent = 'Зачувано';
|
---|
| 197 | } else {
|
---|
| 198 | button.disabled = false;
|
---|
| 199 | button.textContent = 'Додадете во омилени';
|
---|
| 200 | }
|
---|
| 201 | });
|
---|
| 202 | })
|
---|
| 203 | .catch(error => {
|
---|
| 204 | console.error('Error fetching saved options:', error);
|
---|
| 205 | });
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | function fetchSavedTrips() {
|
---|
| 209 | const userEmail = sessionStorage.getItem('user').replace(/^"|"$/g, '');
|
---|
| 210 | fetch('/get-saved-trips', {
|
---|
| 211 | method: 'POST',
|
---|
| 212 | headers: {
|
---|
| 213 | 'Content-Type': 'application/json'
|
---|
| 214 | },
|
---|
| 215 | body: JSON.stringify({ userEmail: userEmail })
|
---|
| 216 | })
|
---|
| 217 | .then(response => response.json())
|
---|
| 218 | .then(data => {
|
---|
| 219 |
|
---|
| 220 | const savedTripsList = document.getElementById('savedTripsList');
|
---|
| 221 | savedTripsList.innerHTML = '';
|
---|
| 222 | data.savedTrips.forEach(trip => {
|
---|
| 223 | const tripDiv = document.createElement('div');
|
---|
| 224 | tripDiv.classList.add('option');
|
---|
| 225 |
|
---|
| 226 | const closeButton = document.createElement('button');
|
---|
| 227 | closeButton.classList.add('remove-option');
|
---|
| 228 | closeButton.textContent = '×';
|
---|
| 229 | closeButton.onclick = function() {
|
---|
| 230 | removeFromSaved(trip.id);
|
---|
| 231 | };
|
---|
| 232 | tripDiv.appendChild(closeButton);
|
---|
| 233 |
|
---|
| 234 | const hotelName = document.createElement('p');
|
---|
| 235 | hotelName.textContent = trip.hotelName;
|
---|
| 236 | const country = document.createElement('p');
|
---|
| 237 | country.textContent = trip.country;
|
---|
| 238 | const price = document.createElement('p');
|
---|
| 239 | price.textContent = trip.price + "€";
|
---|
| 240 |
|
---|
| 241 | tripDiv.appendChild(hotelName);
|
---|
| 242 | tripDiv.appendChild(country);
|
---|
| 243 | tripDiv.appendChild(price);
|
---|
| 244 |
|
---|
| 245 | savedTripsList.appendChild(tripDiv);
|
---|
| 246 | });
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | const priceChangesList = document.getElementById('priceChangesList');
|
---|
| 250 | priceChangesList.innerHTML = '';
|
---|
| 251 | const priceChanges = data.savedTrips.filter(trip => trip.priceChanged);
|
---|
| 252 | if (priceChanges.length > 0) {
|
---|
| 253 | priceChanges.forEach(change => {
|
---|
| 254 | const changeDiv = document.createElement('div');
|
---|
| 255 | changeDiv.classList.add('price-change');
|
---|
| 256 | changeDiv.textContent = `Цената на ${change.hotelName} има промена од ${change.price}€ во ${change.newPrice}€`;
|
---|
| 257 | priceChangesList.appendChild(changeDiv);
|
---|
| 258 | });
|
---|
| 259 | } else {
|
---|
| 260 | priceChangesList.textContent = "нема промена на цената.";
|
---|
| 261 | }
|
---|
| 262 | })
|
---|
| 263 | .catch(error => {
|
---|
| 264 | console.error('Error fetching saved trips:', error);
|
---|
| 265 | });
|
---|
| 266 | }
|
---|
| 267 | function removeFromSaved(optionId) {
|
---|
| 268 | const userEmail = sessionStorage.getItem('user').replace(/^"|"$/g, '');
|
---|
| 269 | fetch('/remove-from-saved', {
|
---|
| 270 | method: 'POST',
|
---|
| 271 | headers: {
|
---|
| 272 | 'Content-Type': 'application/json'
|
---|
| 273 | },
|
---|
| 274 | body: JSON.stringify({ optionId: optionId, userEmail: userEmail })
|
---|
| 275 | })
|
---|
| 276 | .then(response => {
|
---|
| 277 | if (response.ok) {
|
---|
| 278 | return response.json();
|
---|
| 279 | }
|
---|
| 280 | throw new Error('Network response was not ok.');
|
---|
| 281 | })
|
---|
| 282 | .then(data => {
|
---|
| 283 | if (data.success) {
|
---|
| 284 | fetchSavedTrips();
|
---|
| 285 | }
|
---|
| 286 | })
|
---|
| 287 | .catch(error => {
|
---|
| 288 | console.error('Error:', error);
|
---|
| 289 | });
|
---|
| 290 | }
|
---|