fix: возвращена рабочая версия addEquipment

This commit is contained in:
Nurs 2026-09-16 12:07:10 +00:00
parent 4917f34338
commit cccf382818

View File

@ -367,19 +367,19 @@ function updateCounts() {
}
}
async function addEquipment(event) {
function addEquipment(event) {
event.preventDefault();
const form = event.target;
const formData = new FormData(form);
// Если режим редактирования - обрабатываем отдельно
if (editingId) {
const index = equipment.findIndex(e => e.id === editingId);
if (index !== -1) {
const lastCheck = formData.get('lastCheck');
const period = parseInt(formData.get('period'));
const nextCheck = calculateNextDateValue(lastCheck, period);
if (editingId) {
// Edit existing item
const index = equipment.findIndex(e => e.id === editingId);
if (index !== -1) {
equipment[index] = {
...equipment[index],
name: formData.get('name'),
@ -392,14 +392,20 @@ async function addEquipment(event) {
nextCheck: nextCheck,
note: formData.get('note') || ''
};
await saveEquipment();
saveEquipment();
updateAll();
resetForm();
switchTab('all');
}
editingId = null;
} else {
// Add new item - add to local array first
return;
}
// Добавление нового оборудования (как работало раньше)
const lastCheck = formData.get('lastCheck');
const period = parseInt(formData.get('period'));
const nextCheck = calculateNextDateValue(lastCheck, period);
const newItem = {
id: Date.now(),
name: formData.get('name'),
@ -415,22 +421,10 @@ async function addEquipment(event) {
};
equipment.push(newItem);
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);
}
saveEquipment();
resetForm();
updateAll();
switchTab('all');
}
}
function calculateNextDateValue(lastCheck, periodMonths) {
@ -706,9 +700,9 @@ function escapeHtml(text) {
.replace(/"/g, '"');
}
async async function saveEquipment() {
async function saveEquipment() {
try {
await fetch('api/items', {
fetch('api/items', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(equipment)