|
| 1 | +const API_BASE = 'http://localhost:8080/api'; |
| 2 | + |
| 3 | +const todoForm = document.getElementById('todo-form'); |
| 4 | +const todoInput = document.getElementById('todo-input'); |
| 5 | +const todoList = document.getElementById('todo-list'); |
| 6 | +const statusDiv = document.getElementById('status'); |
| 7 | +const initBtn = document.getElementById('init-btn'); |
| 8 | + |
| 9 | +// Show status message |
| 10 | +function showStatus(message, isError = false) { |
| 11 | + statusDiv.textContent = message; |
| 12 | + statusDiv.className = 'status ' + (isError ? 'error' : 'success'); |
| 13 | + setTimeout(() => { |
| 14 | + statusDiv.textContent = ''; |
| 15 | + statusDiv.className = 'status'; |
| 16 | + }, 3000); |
| 17 | +} |
| 18 | + |
| 19 | +// API helpers |
| 20 | +async function api(endpoint, options = {}) { |
| 21 | + const res = await fetch(`${API_BASE}${endpoint}`, { |
| 22 | + headers: { 'Content-Type': 'application/json' }, |
| 23 | + ...options, |
| 24 | + }); |
| 25 | + if (!res.ok && res.status !== 204) { |
| 26 | + const error = await res.json().catch(() => ({ error: res.statusText })); |
| 27 | + throw new Error(error.error || error.message || 'Request failed'); |
| 28 | + } |
| 29 | + if (res.status === 204) return null; |
| 30 | + return res.json(); |
| 31 | +} |
| 32 | + |
| 33 | +// Initialize database |
| 34 | +async function initDatabase() { |
| 35 | + try { |
| 36 | + initBtn.disabled = true; |
| 37 | + await api('/init', { method: 'POST' }); |
| 38 | + showStatus('Database initialized!'); |
| 39 | + loadTodos(); |
| 40 | + } catch (err) { |
| 41 | + showStatus('Init failed: ' + err.message, true); |
| 42 | + } finally { |
| 43 | + initBtn.disabled = false; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Load all todos |
| 48 | +async function loadTodos() { |
| 49 | + try { |
| 50 | + todoList.classList.add('loading'); |
| 51 | + const todos = await api('/todos'); |
| 52 | + renderTodos(todos); |
| 53 | + } catch (err) { |
| 54 | + showStatus('Failed to load: ' + err.message, true); |
| 55 | + } finally { |
| 56 | + todoList.classList.remove('loading'); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Render todos |
| 61 | +function renderTodos(todos) { |
| 62 | + todoList.innerHTML = todos.map(todo => ` |
| 63 | + <li class="todo-item ${todo.completed ? 'completed' : ''}" data-id="${todo.id}"> |
| 64 | + <input type="checkbox" ${todo.completed ? 'checked' : ''} onchange="toggleTodo(${todo.id}, this.checked)"> |
| 65 | + <span class="title">${escapeHtml(todo.title)}</span> |
| 66 | + <button class="delete-btn" onclick="deleteTodo(${todo.id})">Delete</button> |
| 67 | + </li> |
| 68 | + `).join(''); |
| 69 | +} |
| 70 | + |
| 71 | +// Escape HTML |
| 72 | +function escapeHtml(text) { |
| 73 | + const div = document.createElement('div'); |
| 74 | + div.textContent = text; |
| 75 | + return div.innerHTML; |
| 76 | +} |
| 77 | + |
| 78 | +// Add todo |
| 79 | +async function addTodo(title) { |
| 80 | + try { |
| 81 | + todoInput.disabled = true; |
| 82 | + await api('/todos', { |
| 83 | + method: 'POST', |
| 84 | + body: JSON.stringify({ title }), |
| 85 | + }); |
| 86 | + todoInput.value = ''; |
| 87 | + loadTodos(); |
| 88 | + } catch (err) { |
| 89 | + showStatus('Failed to add: ' + err.message, true); |
| 90 | + } finally { |
| 91 | + todoInput.disabled = false; |
| 92 | + todoInput.focus(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Toggle todo |
| 97 | +async function toggleTodo(id, completed) { |
| 98 | + try { |
| 99 | + const todo = await api(`/todos/${id}`); |
| 100 | + await api(`/todos/${id}`, { |
| 101 | + method: 'PUT', |
| 102 | + body: JSON.stringify({ ...todo, completed }), |
| 103 | + }); |
| 104 | + loadTodos(); |
| 105 | + } catch (err) { |
| 106 | + showStatus('Failed to update: ' + err.message, true); |
| 107 | + loadTodos(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Delete todo |
| 112 | +async function deleteTodo(id) { |
| 113 | + try { |
| 114 | + await api(`/todos/${id}`, { method: 'DELETE' }); |
| 115 | + loadTodos(); |
| 116 | + } catch (err) { |
| 117 | + showStatus('Failed to delete: ' + err.message, true); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// Event listeners |
| 122 | +todoForm.addEventListener('submit', (e) => { |
| 123 | + e.preventDefault(); |
| 124 | + const title = todoInput.value.trim(); |
| 125 | + if (title) addTodo(title); |
| 126 | +}); |
| 127 | + |
| 128 | +initBtn.addEventListener('click', initDatabase); |
| 129 | + |
| 130 | +// Initial load |
| 131 | +loadTodos(); |
0 commit comments