z2/public/index.html

57 lines
1.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Todo List</h1>
<input type="text" id="todoInput" placeholder="Add a new task">
<button id="addTodoButton">Add</button>
<ul id="todoList"></ul>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const todoInput = document.getElementById('todoInput');
const addTodoButton = document.getElementById('addTodoButton');
const todoList = document.getElementById('todoList');
const fetchTodos = async () => {
const response = await fetch('/api/todos');
const todos = await response.json();
todoList.innerHTML = '';
todos.forEach(todo => {
const li = document.createElement('li');
li.textContent = todo.text;
li.style.backgroundColor = todo.completed ? 'lightgreen' : 'lightcoral';
li.addEventListener('click', async () => {
await fetch(`/api/todos/${todo.id}`, { method: 'DELETE' });
fetchTodos();
});
todoList.appendChild(li);
});
};
addTodoButton.addEventListener('click', async () => {
const text = todoInput.value.trim();
if (text) {
await fetch('/api/todos', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text })
});
todoInput.value = '';
fetchTodos();
}
});
fetchTodos();
});
</script>
</body>
</html>