feat: Implement persistence and initial project setup

This commit is contained in:
Marco Gallegos
2025-08-12 20:06:49 -06:00
parent 26a4051c32
commit 8b9f5afe96
11 changed files with 3381 additions and 0 deletions

71
ap-pos/storage.js Normal file
View File

@@ -0,0 +1,71 @@
const API_URL = 'http://localhost:3000/api';
export const KEY_DATA = 'movements';
export const KEY_SETTINGS = 'settings';
export const KEY_CLIENTS = 'clients';
/**
* Carga datos desde el servidor.
* @param {string} key La clave que representa el endpoint (e.g., 'clients').
* @param {any} defaultValue El valor a devolver si la carga falla.
* @returns {Promise<any>} Los datos parseados o el valor por defecto.
*/
export async function load(key, defaultValue) {
try {
const response = await fetch(`${API_URL}/${key}`);
if (!response.ok) {
throw new Error(`Network response was not ok for key: ${key}`);
}
const data = await response.json();
// Si el servidor devuelve nulo, usar el valor por defecto para evitar errores.
if (data === null) {
console.warn(`Server returned null for key "${key}", using default value.`);
return defaultValue;
}
return data;
} catch (error) {
console.error(`Error al cargar datos desde el servidor para la clave "${key}"`, error);
return defaultValue;
}
}
/**
* Guarda datos en el servidor.
* @param {string} key La clave que representa el endpoint.
* @param {any} data Los datos a guardar.
*/
export async function save(key, data) {
try {
const response = await fetch(`${API_URL}/${key}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data), // Envuelve los datos en un objeto con la clave principal
});
if (!response.ok) {
throw new Error(`Network response was not ok for key: ${key}`);
}
} catch (error) {
console.error(`Error al guardar datos en el servidor para la clave "${key}"`, error);
}
}
/**
* Elimina un item por su ID.
* @param {string} key La clave que representa el endpoint (e.g., 'clients').
* @param {string} id El ID del item a eliminar.
*/
export async function remove(key, id) {
try {
const response = await fetch(`${API_URL}/${key}/${id}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error(`Network response was not ok for key: ${key} and id: ${id}`);
}
} catch (error) {
console.error(`Error al eliminar el item con id "${id}" desde "${key}"`, error);
}
}