-
Notifications
You must be signed in to change notification settings - Fork 0
Agregar funcionalidad de edición de título de tareas #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,8 +37,9 @@ async function fetchTasks() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| function addTaskToDOM(task) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const li = document.createElement('li'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| li.innerHTML = ` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span class="${task.completed ? 'completed' : ''}">${task.title}</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span class="${task.completed ? 'completed' : ''}" data-task-id="${task.id}">${task.title}</span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button onclick="editTask('${task.id}')" title="Edit task">✎</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button onclick="toggleComplete('${task.id}', ${!task.completed})">✓</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button onclick="deleteTask('${task.id}')">✕</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
37
to
45
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -73,4 +74,90 @@ async function deleteTask(id) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| fetchTasks(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function editTask(id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const span = document.querySelector(`span[data-task-id="${id}"]`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!span) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const currentTitle = span.textContent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const wasCompleted = span.classList.contains('completed'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create input field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const input = document.createElement('input'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.type = 'text'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.value = currentTitle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.className = 'edit-input'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.setAttribute('data-task-id', id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create save button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const saveBtn = document.createElement('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveBtn.textContent = '✓'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveBtn.className = 'save-btn'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveBtn.title = 'Save changes'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveBtn.onclick = () => saveEdit(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create cancel button | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const cancelBtn = document.createElement('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelBtn.textContent = '✕'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelBtn.className = 'cancel-btn'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelBtn.title = 'Cancel editing'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cancelBtn.onclick = () => cancelEdit(id, currentTitle, wasCompleted); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Replace span with input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| span.replaceWith(input); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Replace action buttons with save/cancel buttons | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const buttonDiv = input.parentElement.querySelector('div'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buttonDiv.innerHTML = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buttonDiv.appendChild(saveBtn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| buttonDiv.appendChild(cancelBtn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Focus input and select text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.focus(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.select(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Allow Enter key to save | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.addEventListener('keypress', (e) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (e.key === 'Enter') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+119
to
+120
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input.addEventListener('keypress', (e) => { | |
| if (e.key === 'Enter') { | |
| input.addEventListener('keydown', (e) => { | |
| if (e.key === 'Enter') { | |
| e.preventDefault(); |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saveEdit does not check the HTTP response status. fetch only rejects on network errors, so non-2xx responses will still call fetchTasks() and make failures hard to notice. Capture the response, check res.ok (and optionally parse/display server error details) before refreshing the list.
| await fetch(`${API_URL}/${id}`, { | |
| method: 'PUT', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ title: newTitle }) | |
| }); | |
| const res = await fetch(`${API_URL}/${id}`, { | |
| method: 'PUT', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ title: newTitle }) | |
| }); | |
| if (!res.ok) { | |
| let errorMessage = 'Error al actualizar la tarea'; | |
| try { | |
| const data = await res.json(); | |
| if (data && (data.message || data.error)) { | |
| errorMessage = data.message || data.error; | |
| } | |
| } catch (parseError) { | |
| // Ignore JSON parse errors and use the default message | |
| } | |
| alert(errorMessage); | |
| return; | |
| } |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cancelEdit accepts originalTitle and wasCompleted but does not use them, and always re-fetches the full task list. Either remove the unused parameters or restore the original DOM state locally to avoid an unnecessary network request on cancel.
| fetchTasks(); | |
| // Try to restore the original DOM state locally to avoid a full refetch | |
| const editedElement = document.querySelector(`[data-task-id="${id}"]`); | |
| // If we cannot find the element for some reason, fall back to refetching | |
| if (!editedElement) { | |
| fetchTasks(); | |
| return; | |
| } | |
| const li = editedElement.closest('li'); | |
| if (!li) { | |
| fetchTasks(); | |
| return; | |
| } | |
| // Restore the original markup, mirroring addTaskToDOM | |
| li.innerHTML = ` | |
| <span class="${wasCompleted ? 'completed' : ''}" data-task-id="${id}">${originalTitle}</span> | |
| <div> | |
| <button onclick="editTask('${id}')" title="Edit task">✎</button> | |
| <button onclick="toggleComplete('${id}', ${!wasCompleted})">✓</button> | |
| <button onclick="deleteTask('${id}')">✕</button> | |
| </div> | |
| `; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -173,6 +173,42 @@ li button:hover { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| li button:active { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: var(--button-active-bg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .edit-input { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| flex: 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding: 0.5em 0.75em; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| border: 2px solid var(--primary-color); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| border-radius: 4px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: var(--input-bg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| color: var(--text-color); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-size: 1.05em; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| outline: none; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| transition: box-shadow 0.2s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .edit-input:focus { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| box-shadow: 0 0 0 3px var(--input-focus-shadow); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .save-btn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: #4caf50 !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| color: white !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| border-radius: 4px !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: auto !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding: 0 0.75em !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-weight: 500; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .save-btn:hover { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: #45a049 !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .cancel-btn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: #f44336 !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| color: white !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| border-radius: 4px !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: auto !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding: 0 0.75em !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-weight: 500; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .cancel-btn:hover { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background: #da190b !important; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+190
to
+210
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .save-btn { | |
| background: #4caf50 !important; | |
| color: white !important; | |
| border-radius: 4px !important; | |
| width: auto !important; | |
| padding: 0 0.75em !important; | |
| font-weight: 500; | |
| } | |
| .save-btn:hover { | |
| background: #45a049 !important; | |
| } | |
| .cancel-btn { | |
| background: #f44336 !important; | |
| color: white !important; | |
| border-radius: 4px !important; | |
| width: auto !important; | |
| padding: 0 0.75em !important; | |
| font-weight: 500; | |
| } | |
| .cancel-btn:hover { | |
| background: #da190b !important; | |
| li button.save-btn { | |
| background: #4caf50; | |
| color: white; | |
| border-radius: 4px; | |
| width: auto; | |
| padding: 0 0.75em; | |
| font-weight: 500; | |
| } | |
| li button.save-btn:hover { | |
| background: #45a049; | |
| } | |
| li button.cancel-btn { | |
| background: #f44336; | |
| color: white; | |
| border-radius: 4px; | |
| width: auto; | |
| padding: 0 0.75em; | |
| font-weight: 500; | |
| } | |
| li button.cancel-btn:hover { | |
| background: #da190b; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new edit button is icon-only (✎) and relies on
title. For accessibility/screen readers, add an explicit accessible name (e.g.,aria-label="Edit task") and consider doing the same for the other icon-only action buttons for consistency.