backoffice first commit
This commit is contained in:
+101
-10
@@ -19,24 +19,115 @@ window.THREE = THREE; // Used by APP Scripts.
|
||||
window.OrbitControls = OrbitControls; // Used by APP Scripts.
|
||||
window.GLTFLoader = GLTFLoader; // Used by APP Scripts.
|
||||
|
||||
// --- Integrazione backend preventivi ---
|
||||
// Base URL dell'API Laravel: impostata in build (import.meta.env.VITE_PREVENTIVI_API_BASE, da
|
||||
// .env/.env.production — vedi root del repo), sovrascrivibile a runtime con window.PREVENTIVI_API_BASE
|
||||
// senza dover rebuildare (es. per un hotfix). Nessuna delle due impostata → fallback dev locale.
|
||||
const PREVENTIVI_API_BASE =
|
||||
(typeof window.PREVENTIVI_API_BASE === 'string' && window.PREVENTIVI_API_BASE) ||
|
||||
import.meta.env.VITE_PREVENTIVI_API_BASE ||
|
||||
'http://localhost:8000/api';
|
||||
const CART_TOKEN_KEY = 'kreios_cart_token';
|
||||
|
||||
// URL dell'app cliente (checkout): richiesto all'API invece di essere indovinato, perché la
|
||||
// porta Vite dell'app cliente può cambiare (es. se 5199 è già occupata). Fallback locale se
|
||||
// il backend non è raggiungibile.
|
||||
let appClienteUrl =
|
||||
(typeof window.APP_CLIENTE_URL === 'string' && window.APP_CLIENTE_URL) ||
|
||||
'http://localhost:5199';
|
||||
|
||||
const configReady = (async () => {
|
||||
try {
|
||||
const res = await fetch(`${PREVENTIVI_API_BASE}/config`, {
|
||||
headers: { Accept: 'application/json' },
|
||||
});
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
if (data.frontend_cliente_url) appClienteUrl = data.frontend_cliente_url;
|
||||
} catch (e) {
|
||||
// Backend non raggiungibile: resta il fallback locale.
|
||||
}
|
||||
})();
|
||||
|
||||
let invioPreventivoInCorso = false;
|
||||
|
||||
// Mostra/aggiorna il link "Vai al preventivo" (markup fisso in index.html, sopra la cart-container).
|
||||
window.mostraLinkPreventivo = (token, count) => {
|
||||
const link = document.getElementById('vai-al-preventivo');
|
||||
if (!link) return;
|
||||
// L'app cliente non conosce a priori l'URL del configuratore (può girare su domini/porte
|
||||
// diverse in dev/prod): lo passiamo qui come parametro, così il link "torna al configuratore"
|
||||
// nella pagina di conferma può essere generato dinamicamente invece di essere hardcoded.
|
||||
const ritorno = encodeURIComponent(window.location.href);
|
||||
link.href = `${appClienteUrl}/?token=${encodeURIComponent(token)}&ritorno=${ritorno}`;
|
||||
link.textContent = count > 0 ? `Vai al preventivo (${count})` : 'Vai al preventivo';
|
||||
link.classList.remove('d-none');
|
||||
};
|
||||
|
||||
// Ripristina il link se esiste già un preventivo in corso da una sessione precedente.
|
||||
const ripristinaLinkPreventivo = async () => {
|
||||
const token = localStorage.getItem(CART_TOKEN_KEY);
|
||||
if (!token) return;
|
||||
try {
|
||||
await configReady;
|
||||
const res = await fetch(`${PREVENTIVI_API_BASE}/carrello/${encodeURIComponent(token)}`, {
|
||||
headers: { Accept: 'application/json' },
|
||||
});
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
if (data.carrello?.items?.length) {
|
||||
window.mostraLinkPreventivo(token, data.carrello.items.length);
|
||||
}
|
||||
} catch (e) {
|
||||
// Backend non raggiungibile: non blocca il configuratore.
|
||||
}
|
||||
};
|
||||
ripristinaLinkPreventivo();
|
||||
|
||||
$(() => {
|
||||
|
||||
$('.cart-button').on('click', async () => {
|
||||
|
||||
if($('.cart-button').hasClass('disabled'))
|
||||
if ($('.cart-button').hasClass('disabled') || invioPreventivoInCorso)
|
||||
return;
|
||||
const img = await window.generaImagine();
|
||||
const data = {
|
||||
payload: window.configurazione,
|
||||
img: img,
|
||||
qty: parseInt($('.quantity-input').val()),
|
||||
fn: 'aggiungi_prodotto'
|
||||
};
|
||||
|
||||
invioPreventivoInCorso = true;
|
||||
try {
|
||||
const params = ParamsParser.getQueryParams();
|
||||
const img = await window.generaImagine();
|
||||
const conf = window.configurazione || {};
|
||||
const inner = conf.configurazione || {};
|
||||
|
||||
window.parent.postMessage(data, "https://www.infissionline.it");
|
||||
window.parent.postMessage(data, "https://infissionline.test");
|
||||
const body = {
|
||||
token: localStorage.getItem(CART_TOKEN_KEY) || undefined,
|
||||
sistema: params.sistema,
|
||||
type: params.type,
|
||||
nome: inner.nome,
|
||||
larghezza: parseInt(inner.width) || null,
|
||||
altezza: parseInt(inner.height) || null,
|
||||
qty: parseInt($('.quantity-input').val()) || 1,
|
||||
prezzo: conf.prezzo,
|
||||
payload: inner,
|
||||
img: img,
|
||||
};
|
||||
|
||||
await configReady;
|
||||
const res = await fetch(`${PREVENTIVI_API_BASE}/carrello/aggiungi`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok) throw new Error('Errore nel salvataggio del preventivo');
|
||||
|
||||
const data = await res.json();
|
||||
localStorage.setItem(CART_TOKEN_KEY, data.token);
|
||||
window.mostraLinkPreventivo(data.token, data.carrello.items.length);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
alert('Si è verificato un errore nell\'aggiunta al preventivo. Riprova.');
|
||||
} finally {
|
||||
invioPreventivoInCorso = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user