fix: исправлено добавление оборудования
This commit is contained in:
parent
0602415ba0
commit
4917f34338
17
script.js
17
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');
|
||||
}
|
||||
}
|
||||
|
||||
15
server.js
15
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);
|
||||
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 }));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user