Simplify AJAX handlers by using page reload
- Use location.reload() instead of DOM manipulation to avoid element lookup errors - More reliable approach when dealing with dynamic content updates
This commit is contained in:
parent
7badf73b50
commit
bf609257c3
|
|
@ -246,30 +246,8 @@ function addSubtask(event, taskId) {
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
// Добавляем подзадачу в список
|
// Обновляем страницу вместо манипуляций с DOM
|
||||||
const list = document.getElementById('subtasks-list');
|
location.reload();
|
||||||
const emptyMessage = list.querySelector('.text-muted.text-center');
|
|
||||||
if (emptyMessage) {
|
|
||||||
emptyMessage.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
const newItem = document.createElement('li');
|
|
||||||
newItem.className = 'list-group-item d-flex align-items-center gap-2';
|
|
||||||
newItem.setAttribute('data-subtask-id', data.subtask_id);
|
|
||||||
newItem.innerHTML = `
|
|
||||||
<input type="checkbox" class="form-check-input subtask-checkbox" onchange="toggleSubtask(${taskId}, ${data.subtask_id})">
|
|
||||||
<span class="flex-grow-1">${title}</span>
|
|
||||||
<button type="button" class="btn btn-outline-danger btn-sm" onclick="deleteSubtask(${taskId}, ${data.subtask_id})" title="Удалить">
|
|
||||||
<i class="fa-solid fa-times"></i>
|
|
||||||
</button>
|
|
||||||
`;
|
|
||||||
list.appendChild(newItem);
|
|
||||||
|
|
||||||
// Очищаем поле ввода
|
|
||||||
input.value = '';
|
|
||||||
|
|
||||||
// Обновляем счётчик
|
|
||||||
updateSubtasksCount();
|
|
||||||
} else {
|
} else {
|
||||||
alert(data.error || 'Ошибка при создании подзадачи');
|
alert(data.error || 'Ошибка при создании подзадачи');
|
||||||
}
|
}
|
||||||
|
|
@ -302,14 +280,13 @@ function toggleSubtask(taskId, subtaskId) {
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
alert(data.error || 'Ошибка');
|
alert(data.error || 'Ошибка');
|
||||||
location.reload();
|
|
||||||
} else {
|
} else {
|
||||||
|
// Обновляем страницу для обновления UI
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Error:', error);
|
console.error('Error:', error);
|
||||||
location.reload();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -329,17 +306,8 @@ function deleteSubtask(taskId, subtaskId) {
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
alert(data.error || 'Ошибка');
|
alert(data.error || 'Ошибка');
|
||||||
} else {
|
} else {
|
||||||
// Удаляем из DOM
|
// Обновляем страницу для обновления UI
|
||||||
const item = document.querySelector(`li[data-subtask-id="${subtaskId}"]`);
|
location.reload();
|
||||||
if (item) {
|
|
||||||
item.remove();
|
|
||||||
updateSubtasksCount();
|
|
||||||
}
|
|
||||||
// Если список пуст, показываем сообщение
|
|
||||||
const list = document.getElementById('subtasks-list');
|
|
||||||
if (list.querySelectorAll('li').length === 0) {
|
|
||||||
list.innerHTML = '<p class="text-muted text-center mb-3">Подзадач пока нет</p>';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue