// favourites.js — Gestione cuori preferiti (frontend pubblico) // Richiesto da search.php, search-map.php, salon.php // I cuori nell'HTML devono essere: // e contenere un'icona . (function () { "use strict"; // Delego l'evento sul document: funziona anche per cuori aggiunti // dinamicamente (es. card generate via JS nella mappa) senza riagganciare. document.addEventListener("click", function (e) { var btn = e.target.closest(".js-fav"); if (!btn) return; e.preventDefault(); var shopId = btn.getAttribute("data-id"); if (!shopId) return; // Evita doppi click mentre la richiesta è in volo if (btn.dataset.loading === "1") return; btn.dataset.loading = "1"; var body = new URLSearchParams(); body.append("shop_id", shopId); fetch("favourite_toggle.php", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: body.toString(), }) .then(function (r) { return r.json(); }) .then(function (data) { if (data.need_login) { openFavLoginModal(); return; } if (!data.ok) { console.error("Preferiti:", data.error); return; } // Aggiorna l'aspetto del cuore in base allo stato restituito dal server setFavState(btn, data.favorite); }) .catch(function (err) { console.error("Preferiti: richiesta fallita", err); }) .finally(function () { btn.dataset.loading = "0"; }); }); // Applica lo stato "attivo/inattivo" al cuore. // fa-solid = cuore pieno (preferito), fa-regular = cuore vuoto. function setFavState(btn, isFav) { btn.classList.toggle("is-active", isFav); var icon = btn.querySelector("i"); if (icon) { icon.classList.toggle("fa-solid", isFav); icon.classList.toggle("fa-regular", !isFav); } btn.setAttribute("aria-pressed", isFav ? "true" : "false"); } // --- Modale "Accedi per salvare i preferiti" --- // La creo una sola volta e la riuso. Niente dipendenze: HTML+CSS inline, // così funziona su tutte le pagine anche senza il JS del template Vuexy/Triptes. function openFavLoginModal() { var existing = document.getElementById("favLoginModal"); if (existing) { existing.style.display = "flex"; return; } var overlay = document.createElement("div"); overlay.id = "favLoginModal"; overlay.style.cssText = "position:fixed;inset:0;z-index:9999;display:flex;align-items:center;" + "justify-content:center;background:rgba(15,23,42,.55);backdrop-filter:blur(2px);"; overlay.innerHTML = '
' + "Accedi o registrati per aggiungere questo salone ai preferiti " + "e ritrovarlo con un click." + "
" + '