From 4917f3433862f9d667dcb78f7112878f46ab519f Mon Sep 17 00:00:00 2001 From: Nurs Date: Wed, 16 Sep 2026 12:05:17 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D0=B1=D0=BE=D1=80=D1=83=D0=B4?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script.js | 17 ++++++++++++++--- server.js | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) 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 })); }