fix: возвращена рабочая версия addEquipment
This commit is contained in:
parent
4917f34338
commit
cccf382818
76
script.js
76
script.js
@ -367,19 +367,19 @@ function updateCounts() {
|
||||
}
|
||||
}
|
||||
|
||||
async function addEquipment(event) {
|
||||
function addEquipment(event) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
const formData = new FormData(form);
|
||||
|
||||
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) {
|
||||
const lastCheck = formData.get('lastCheck');
|
||||
const period = parseInt(formData.get('period'));
|
||||
const nextCheck = calculateNextDateValue(lastCheck, period);
|
||||
|
||||
equipment[index] = {
|
||||
...equipment[index],
|
||||
name: formData.get('name'),
|
||||
@ -392,45 +392,39 @@ 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
|
||||
const newItem = {
|
||||
id: Date.now(),
|
||||
name: formData.get('name'),
|
||||
category: formData.get('category'),
|
||||
inventoryNumber: formData.get('inventoryNumber'),
|
||||
location: formData.get('location'),
|
||||
responsible: formData.get('responsible'),
|
||||
lastCheck: lastCheck,
|
||||
period: period,
|
||||
nextCheck: nextCheck,
|
||||
note: formData.get('note') || '',
|
||||
status: 'ok'
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
resetForm();
|
||||
switchTab('all');
|
||||
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'),
|
||||
category: formData.get('category'),
|
||||
inventoryNumber: formData.get('inventoryNumber'),
|
||||
location: formData.get('location'),
|
||||
responsible: formData.get('responsible'),
|
||||
lastCheck: lastCheck,
|
||||
period: period,
|
||||
nextCheck: nextCheck,
|
||||
note: formData.get('note') || '',
|
||||
status: 'ok'
|
||||
};
|
||||
|
||||
equipment.push(newItem);
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user