diff --git a/script.js b/script.js index 9b8b453..b730af8 100644 --- a/script.js +++ b/script.js @@ -399,7 +399,7 @@ async function addEquipment(event) { } editingId = null; } else { - // Add new item + // Add new item - add to local array first const newItem = { id: Date.now(), name: formData.get('name'), @@ -415,9 +415,20 @@ async function addEquipment(event) { }; equipment.push(newItem); - await saveEquipment(); - resetForm(); updateAll(); + + // Then save to server + try { + await fetch('api/items', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(equipment) + }); + } catch (e) { + console.error('Save error:', e); + } + + resetForm(); switchTab('all'); } } diff --git a/server.js b/server.js index bc00aef..0ca9507 100644 --- a/server.js +++ b/server.js @@ -38,10 +38,21 @@ http.createServer(async (req, res) => { return res.end(JSON.stringify(readItems())); } - // API: save all items + // API: add single item if (req.method === "POST" && url === "/api/items") { - const items = await body(req); - writeItems(items); + const data = await body(req); + const items = readItems(); + + // If data is array, replace all items + if (Array.isArray(data)) { + writeItems(data); + } else { + // If data is single item, add it + const newItem = { id: Date.now(), ...data }; + items.push(newItem); + writeItems(items); + } + res.writeHead(200, { "Content-Type": "application/json" }); return res.end(JSON.stringify({ ok: true })); }