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(data => {
|
||||
if (data.success) {
|
||||
// Добавляем подзадачу в список
|
||||
const list = document.getElementById('subtasks-list');
|
||||
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();
|
||||
// Обновляем страницу вместо манипуляций с DOM
|
||||
location.reload();
|
||||
} else {
|
||||
alert(data.error || 'Ошибка при создании подзадачи');
|
||||
}
|
||||
|
|
@ -302,14 +280,13 @@ function toggleSubtask(taskId, subtaskId) {
|
|||
.then(data => {
|
||||
if (!data.success) {
|
||||
alert(data.error || 'Ошибка');
|
||||
location.reload();
|
||||
} else {
|
||||
// Обновляем страницу для обновления UI
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -329,17 +306,8 @@ function deleteSubtask(taskId, subtaskId) {
|
|||
if (!data.success) {
|
||||
alert(data.error || 'Ошибка');
|
||||
} else {
|
||||
// Удаляем из DOM
|
||||
const item = document.querySelector(`li[data-subtask-id="${subtaskId}"]`);
|
||||
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>';
|
||||
}
|
||||
// Обновляем страницу для обновления UI
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue