116 lines
2.9 KiB
JavaScript
116 lines
2.9 KiB
JavaScript
// ===============================
|
|
// state.js — Stato condiviso + API
|
|
// ===============================
|
|
|
|
let localPhotos = [];
|
|
let photoDB = [];
|
|
|
|
// ---- LAST SYNC ----
|
|
function getLastSync() {
|
|
return localStorage.getItem("lastSync");
|
|
}
|
|
|
|
function setLastSync(ts) {
|
|
if (!ts) return;
|
|
localStorage.setItem("lastSync", ts);
|
|
}
|
|
|
|
// ---- STATO FOTO ----
|
|
function getLocalPhotos() {
|
|
return localPhotos;
|
|
}
|
|
|
|
function setLocalPhotos(photos) {
|
|
localPhotos = Array.isArray(photos) ? photos : [];
|
|
photoDB = [...localPhotos];
|
|
}
|
|
|
|
function addPhotoLocal(photo) {
|
|
if (!photo || !photo.id) return;
|
|
const idx = localPhotos.findIndex(p => p.id === photo.id);
|
|
if (idx >= 0) localPhotos[idx] = photo;
|
|
else localPhotos.push(photo);
|
|
}
|
|
|
|
function removePhotoLocal(id) {
|
|
const before = localPhotos.length;
|
|
localPhotos = localPhotos.filter(p => p.id !== id);
|
|
const after = localPhotos.length;
|
|
console.log("[removePhotoLocal] id:", id, "prima:", before, "dopo:", after);
|
|
}
|
|
|
|
function saveLocalState() {
|
|
try {
|
|
localStorage.setItem("photosCache", JSON.stringify(localPhotos));
|
|
} catch (e) {
|
|
console.warn("saveLocalState error:", e);
|
|
}
|
|
}
|
|
|
|
// ---- API ----
|
|
|
|
async function getAllPhotos() {
|
|
const token = localStorage.getItem("token");
|
|
|
|
console.log("[getAllPhotos] Fetch /photos...");
|
|
const res = await fetch("/photos", {
|
|
headers: {
|
|
"Authorization": "Bearer " + token
|
|
}
|
|
});
|
|
|
|
if (!res.ok) throw new Error("Errore getAllPhotos");
|
|
|
|
const data = await res.json();
|
|
const photos = data.photos || data || [];
|
|
console.log("[getAllPhotos] numero foto:", photos.length);
|
|
|
|
setLocalPhotos(photos);
|
|
return photos;
|
|
}
|
|
|
|
async function getPhotoById(id) {
|
|
const token = localStorage.getItem("token");
|
|
|
|
const q = `id=${encodeURIComponent(id)}`;
|
|
const res = await fetch(`/photos/byIds?${q}`, {
|
|
headers: {
|
|
"Authorization": "Bearer " + token
|
|
}
|
|
});
|
|
|
|
if (!res.ok) throw new Error("Errore getPhotoById");
|
|
|
|
const data = await res.json();
|
|
return data.photos || [];
|
|
}
|
|
|
|
|
|
async function getChanges(since, user) {
|
|
const token = localStorage.getItem("token");
|
|
|
|
const url = `/photos/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`;
|
|
console.log("[getChanges] URL:", url);
|
|
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
"Authorization": "Bearer " + token
|
|
}
|
|
});
|
|
|
|
if (!res.ok) throw new Error("Errore getChanges");
|
|
return await res.json();
|
|
}
|
|
|
|
|
|
// ---- EXPORT ----
|
|
window.getLastSync = getLastSync;
|
|
window.setLastSync = setLastSync;
|
|
window.getLocalPhotos = getLocalPhotos;
|
|
window.setLocalPhotos = setLocalPhotos;
|
|
window.addPhotoLocal = addPhotoLocal;
|
|
window.removePhotoLocal = removePhotoLocal;
|
|
window.saveLocalState = saveLocalState;
|
|
window.getAllPhotos = getAllPhotos;
|
|
window.getPhotoById = getPhotoById;
|
|
window.getChanges = getChanges;
|