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