backoffice first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
const BASE = import.meta.env.VITE_API_BASE || 'http://localhost:8000/api'
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
||||
...options,
|
||||
})
|
||||
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' }),
|
||||
conferma: (token, dati) =>
|
||||
request(`/carrello/${token}/conferma`, { method: 'POST', body: JSON.stringify(dati) }),
|
||||
}
|
||||
|
||||
const STORAGE_KEY = 'kreios_cart_token'
|
||||
const RETURN_URL_KEY = 'kreios_return_url'
|
||||
|
||||
export function resolveToken() {
|
||||
const url = new URL(window.location.href)
|
||||
const fromUrl = url.searchParams.get('token')
|
||||
const ritorno = url.searchParams.get('ritorno')
|
||||
if (ritorno) {
|
||||
localStorage.setItem(RETURN_URL_KEY, ritorno)
|
||||
}
|
||||
if (fromUrl) {
|
||||
localStorage.setItem(STORAGE_KEY, fromUrl)
|
||||
// pulisci l'URL mantenendo token/ritorno in localStorage
|
||||
url.searchParams.delete('token')
|
||||
url.searchParams.delete('ritorno')
|
||||
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 clearToken() {
|
||||
localStorage.removeItem(STORAGE_KEY)
|
||||
}
|
||||
|
||||
export function formatEuro(value) {
|
||||
return new Intl.NumberFormat('it-IT', { style: 'currency', currency: 'EUR' }).format(value || 0)
|
||||
}
|
||||
Reference in New Issue
Block a user