83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
|
// Nowe zadanie
|
||
|
async function addTask() {
|
||
|
const taskDescription = document.getElementById('new-task').value;
|
||
|
|
||
|
if (!taskDescription) {
|
||
|
alert('Opis zadania nie może być pusty!');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
const response = await fetch('http://localhost:8000/add_task', {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
'Content-Type': 'application/json'
|
||
|
},
|
||
|
body: JSON.stringify({ task_description: taskDescription })
|
||
|
});
|
||
|
|
||
|
if (!response.ok) {
|
||
|
throw new Error('Odpowiedź sieci nie była OK');
|
||
|
}
|
||
|
|
||
|
const result = await response.json();
|
||
|
console.log('Zadanie dodane:', result);
|
||
|
document.getElementById('new-task').value = '';
|
||
|
loadTasks();
|
||
|
} catch (error) {
|
||
|
console.error('Błąd podczas dodawania zadania:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Ładowanie zadania
|
||
|
async function loadTasks() {
|
||
|
try {
|
||
|
const response = await fetch('http://localhost:8000/get_tasks');
|
||
|
|
||
|
if (!response.ok) {
|
||
|
throw new Error('Odpowiedź sieci nie była OK');
|
||
|
}
|
||
|
|
||
|
const tasks = await response.json();
|
||
|
const taskList = document.getElementById('task-list');
|
||
|
taskList.innerHTML = '';
|
||
|
tasks.forEach(task => {
|
||
|
const listItem = document.createElement('li');
|
||
|
listItem.textContent = task.task_description;
|
||
|
|
||
|
// Przycisk do usuwania zadania
|
||
|
//PARADYGMAT STEROWANIA ZDARZENIAMI
|
||
|
const deleteButton = document.createElement('button');
|
||
|
deleteButton.textContent = 'Usuń';
|
||
|
deleteButton.onclick = () => deleteTask(task.id);
|
||
|
|
||
|
listItem.appendChild(deleteButton);
|
||
|
taskList.appendChild(listItem);
|
||
|
});
|
||
|
} catch (error) {
|
||
|
console.error('Błąd podczas ładowania zadań:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Usuwanie zdarzenia
|
||
|
async function deleteTask(taskId) {
|
||
|
try {
|
||
|
const response = await fetch(`http://localhost:8000/delete_task/${taskId}`, {
|
||
|
method: 'DELETE'
|
||
|
});
|
||
|
|
||
|
if (!response.ok) {
|
||
|
throw new Error('Odpowiedź sieci nie była OK');
|
||
|
}
|
||
|
|
||
|
const result = await response.json();
|
||
|
console.log('Zadanie usunięte:', result);
|
||
|
loadTasks();
|
||
|
} catch (error) {
|
||
|
console.error('Błąd podczas usuwania zadania:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
document.addEventListener('DOMContentLoaded', () => {
|
||
|
loadTasks();
|
||
|
});
|