Add support for sqlite
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
// Local SQLite API instead of Firebase
|
||||
|
||||
const API_URL = "/api";
|
||||
|
||||
export async function suscribirLocales(onData, onError) {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/locales`);
|
||||
const data = await response.json();
|
||||
onData(data);
|
||||
// Simulate real-time by polling every 5 seconds
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/locales`);
|
||||
const newData = await res.json();
|
||||
onData(newData);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}, 5000);
|
||||
return () => clearInterval(interval);
|
||||
} catch (err) {
|
||||
onError(err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function suscribirPendientes(onData, onError) {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/pendientes`);
|
||||
const data = await response.json();
|
||||
onData(data);
|
||||
// Simulate real-time by polling every 5 seconds
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/pendientes`);
|
||||
const newData = await res.json();
|
||||
onData(newData);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}, 5000);
|
||||
return () => clearInterval(interval);
|
||||
} catch (err) {
|
||||
onError(err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function enviarPropuesta(local) {
|
||||
const { id: _x, ...datos } = local;
|
||||
const response = await fetch(`${API_URL}/propuestas`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(datos),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function aprobarPropuesta(p) {
|
||||
const response = await fetch(`${API_URL}/aprobar/${p.id}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function rechazarPropuesta(id) {
|
||||
const response = await fetch(`${API_URL}/rechazar/${id}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function deleteLocal(id) {
|
||||
const response = await fetch(`${API_URL}/locales/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function obtenerPalabrasFiltradas() {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/config/filtro_palabras`);
|
||||
const data = await response.json();
|
||||
return data.palabras || [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function guardarPalabrasFiltradas(palabras) {
|
||||
const response = await fetch(`${API_URL}/config/filtro_palabras`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ palabras }),
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export function contienepalabrasProhibidas(texto, lista) {
|
||||
if (!lista?.length) return false;
|
||||
const norm = s => s.toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "");
|
||||
const t = norm(texto);
|
||||
return lista.some(p => t.includes(norm(p)));
|
||||
}
|
||||
|
||||
// Auth helpers (local implementation)
|
||||
let currentUser = null;
|
||||
|
||||
export function escucharAuth(cb) {
|
||||
// Simple localStorage-based auth
|
||||
const user = localStorage.getItem("user");
|
||||
currentUser = user ? JSON.parse(user) : null;
|
||||
cb(currentUser);
|
||||
|
||||
// Listen for storage changes
|
||||
const handleStorageChange = () => {
|
||||
const user = localStorage.getItem("user");
|
||||
currentUser = user ? JSON.parse(user) : null;
|
||||
cb(currentUser);
|
||||
};
|
||||
window.addEventListener("storage", handleStorageChange);
|
||||
return () => window.removeEventListener("storage", handleStorageChange);
|
||||
}
|
||||
|
||||
export async function loginAdmin(email, password) {
|
||||
// Simple email/password check (in production use proper auth)
|
||||
if (email && password) {
|
||||
const user = { email, uid: email };
|
||||
localStorage.setItem("user", JSON.stringify(user));
|
||||
currentUser = user;
|
||||
return user;
|
||||
}
|
||||
throw new Error("Invalid email or password");
|
||||
}
|
||||
|
||||
export async function registrarAdmin(email, password) {
|
||||
return loginAdmin(email, password);
|
||||
}
|
||||
|
||||
export async function cerrarSesion() {
|
||||
localStorage.removeItem("user");
|
||||
currentUser = null;
|
||||
}
|
||||
Reference in New Issue
Block a user