server_photo_s85js/public/js/api.js
2026-08-11 08:45:04 +02:00

111 lines
3.1 KiB
JavaScript

window.AppConfig = window.AppConfig || { ready: false };
// ===============================================
// WAIT FOR CONFIG
// ===============================================
function waitForConfig() {
return new Promise(resolve => {
if (!window.AppConfig) {
window.AppConfig = { ready: false };
}
if (window.AppConfig.ready) {
return resolve();
}
window.addEventListener("config:ready", resolve, { once: true });
});
}
// ===============================================
// API WRAPPER
// ===============================================
async function apiGet(url) {
const token = localStorage.getItem("token");
const res = await fetch(url, {
headers: { "Authorization": "Bearer " + token }
});
const text = await res.text();
try {
return JSON.parse(text);
} catch {
console.error("❌ Risposta non JSON:", text);
throw new Error("Risposta non valida dal server");
}
}
// ===============================================
// ENDPOINT DINAMICO (photos / activities)
// ===============================================
function apiBase() {
return state.mode === "activities" ? "/activities" : "/photos";
}
// ===============================================
// GET ALL ITEMS (FULL LOAD)
// ===============================================
async function getAllItems(user) {
await waitForConfig();
return apiGet(`${apiBase()}?user=${encodeURIComponent(user)}`);
}
// ===============================================
// GET ITEM BY ID
// ===============================================
async function getItemById(id) {
await waitForConfig();
const payload = parseJwt(localStorage.getItem("token"));
const user = payload?.name || "Common";
return apiGet(
`${apiBase()}/byIds?id=${encodeURIComponent(id)}&user=${encodeURIComponent(user)}`
);
}
// ===============================================
// GET CHANGES (INCREMENTAL SYNC)
// ===============================================
async function getChanges(since, user) {
await waitForConfig();
return apiGet(
`${apiBase()}/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`
);
}
// ===============================================
// GET HARD DELETES
// ===============================================
async function getDeletedHard(since, user) {
await waitForConfig();
const json = await apiGet(
`${apiBase()}/deleted_hard?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`
);
return json.deleted || [];
}
// ===============================================
// FETCH ITEMS BY IDS (bulk)
// ===============================================
async function fetchItemsByIds(ids) {
if (!ids || !ids.length) return [];
const qs = ids.map(id => `id=${encodeURIComponent(id)}`).join("&");
const payload = parseJwt(localStorage.getItem("token"));
const user = payload?.name || "Common";
return apiGet(`${apiBase()}/byIds?${qs}&user=${encodeURIComponent(user)}`);
}
// EXPORT
window.getAllItems = getAllItems;
window.getItemById = getItemById;
window.getChanges = getChanges;
window.getDeletedHard = getDeletedHard;
window.fetchItemsByIds = fetchItemsByIds;