88 lines
3.1 KiB
JavaScript
88 lines
3.1 KiB
JavaScript
const BASE = import.meta.env.VITE_API_BASE || 'http://localhost:8000/api'
|
|
|
|
async function request(path, options = {}) {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
...options,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
...options.headers,
|
|
},
|
|
})
|
|
const data = await res.json().catch(() => ({}))
|
|
if (!res.ok) {
|
|
const err = new Error(data.message || 'Errore di rete')
|
|
err.status = res.status
|
|
err.errors = data.errors
|
|
throw err
|
|
}
|
|
return data
|
|
}
|
|
|
|
export const api = {
|
|
getCarrello: (token) => request(`/carrello/${token}`),
|
|
rimuoviItem: (token, itemId) => request(`/carrello/${token}/item/${itemId}`, { method: 'DELETE' }),
|
|
// Se l'utente era loggato nel configuratore, passiamo il suo token al backend così
|
|
// il preventivo viene agganciato al suo record cliente esistente invece di crearne
|
|
// uno nuovo (è lo stesso token usato per precompilare l'anagrafica).
|
|
conferma: (token, dati) => {
|
|
const headers = {}
|
|
const clienteToken = resolveClienteToken()
|
|
if (clienteToken) {
|
|
headers.Authorization = `Bearer ${clienteToken}`
|
|
}
|
|
return request(`/carrello/${token}/conferma`, { method: 'POST', body: JSON.stringify(dati), headers })
|
|
},
|
|
// Dati anagrafici del cliente loggato nel configuratore, per precompilare il checkout.
|
|
me: (clienteToken) =>
|
|
request('/auth/me', { headers: { Authorization: `Bearer ${clienteToken}` } }),
|
|
}
|
|
|
|
const STORAGE_KEY = 'kreios_cart_token'
|
|
const RETURN_URL_KEY = 'kreios_return_url'
|
|
const CLIENTE_TOKEN_KEY = 'kreios_cliente_token'
|
|
|
|
export function resolveToken() {
|
|
const url = new URL(window.location.href)
|
|
const fromUrl = url.searchParams.get('token')
|
|
const ritorno = url.searchParams.get('ritorno')
|
|
const clienteToken = url.searchParams.get('cliente_token')
|
|
if (ritorno) {
|
|
localStorage.setItem(RETURN_URL_KEY, ritorno)
|
|
}
|
|
// Token del cliente loggato nel configuratore (vedi AuthGate.getToken() in starter.js),
|
|
// usato per precompilare l'anagrafica nel checkout. Assente se non era loggato.
|
|
if (clienteToken) {
|
|
localStorage.setItem(CLIENTE_TOKEN_KEY, clienteToken)
|
|
}
|
|
if (fromUrl) {
|
|
localStorage.setItem(STORAGE_KEY, fromUrl)
|
|
// pulisci l'URL mantenendo token/ritorno/cliente_token in localStorage
|
|
url.searchParams.delete('token')
|
|
url.searchParams.delete('ritorno')
|
|
url.searchParams.delete('cliente_token')
|
|
window.history.replaceState({}, '', url.pathname + url.search)
|
|
return fromUrl
|
|
}
|
|
return localStorage.getItem(STORAGE_KEY)
|
|
}
|
|
|
|
// URL del configuratore da cui arriva il carrello: passato dinamicamente come parametro
|
|
// (vedi mostraLinkPreventivo in starter.js), non hardcoded, perché il configuratore può girare
|
|
// su domini/porte diverse tra dev e produzione.
|
|
export function resolveReturnUrl() {
|
|
return localStorage.getItem(RETURN_URL_KEY)
|
|
}
|
|
|
|
export function resolveClienteToken() {
|
|
return localStorage.getItem(CLIENTE_TOKEN_KEY)
|
|
}
|
|
|
|
export function clearToken() {
|
|
localStorage.removeItem(STORAGE_KEY)
|
|
}
|
|
|
|
export function formatEuro(value) {
|
|
return new Intl.NumberFormat('it-IT', { style: 'currency', currency: 'EUR' }).format(value || 0)
|
|
}
|