fix: исправлено добавление оборудования

This commit is contained in:
Nurs 2026-09-16 12:05:17 +00:00
parent 0602415ba0
commit 4917f34338
2 changed files with 28 additions and 6 deletions

View File

@ -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');
}
}

View File

@ -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 }));
}