generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from GloireIrenge/add-remove-todo
Add and remove items from To Do List
- Loading branch information
Showing
4 changed files
with
152 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const TodoLoad = (todos) => { | ||
todos = JSON.parse(localStorage.getItem('todos')) || []; | ||
for (let i = 0; i < todos.length; i += 1) { | ||
todos[i].index = i + 1; | ||
} | ||
return todos; | ||
}; | ||
|
||
class Todo { | ||
constructor(description, index = 0, completed = false) { | ||
this.index = index; | ||
this.description = description; | ||
this.completed = completed; | ||
} | ||
|
||
addTodo = (task, todos) => { | ||
todos.push(task); | ||
localStorage.setItem('todos', JSON.stringify(todos)); | ||
this.addTodo.id = todos; | ||
} | ||
|
||
updateIndex = (arr) => { | ||
arr.forEach((elmt, index) => { | ||
elmt.id = index + 1; | ||
}); | ||
}; | ||
|
||
deleteTodo = (e, todos) => { | ||
e.target.parentElement.remove(); | ||
todos.splice(e.target.parentElement.id, 1); | ||
updateIndex(todos); | ||
localStorage.setItem('todos', JSON.stringify(todos)); | ||
} | ||
} | ||
|
||
export { TodoLoad, Todo }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,85 @@ | ||
import './style.css'; | ||
import { Todo, TodoLoad } from './Add_remove.js'; | ||
|
||
const toDo = [ | ||
{ | ||
index: 0, | ||
description: 'Learn Javascript classes', | ||
completed: false, | ||
}, | ||
{ | ||
index: 1, | ||
description: 'make a small project', | ||
completed: false, | ||
}, | ||
{ | ||
index: 2, | ||
description: 'Attend Microverse meeting', | ||
completed: false, | ||
}, | ||
|
||
]; | ||
|
||
const AllToDo = document.querySelector('.TodoList'); | ||
const showTodo = (toDo) => { | ||
for (let i = 0; i < toDo.length; i += 1) { | ||
AllToDo.innerHTML += `<li class="todo" id=${toDo[i].index}> | ||
<div class="TodoDesc"> | ||
<input type="checkbox" name="todo" class="toggle-check" /> | ||
<span class="description">${toDo[i].description}</span> | ||
let todos = []; | ||
|
||
const MyTodoList = document.querySelector('.tasks-list'); | ||
|
||
const showTodo = (todos) => { | ||
MyTodoList.innerHTML = ''; | ||
for (let i = 0; i < todos.length; i += 1) { | ||
MyTodoList.innerHTML += `<li class="task" id=${i} data-id="${todos[i].index}"> | ||
<div class="task-desc"> | ||
<input type="checkbox" name="task" class="toggle-check" /> | ||
<input class="task-edit" value="${todos[i].description}" disabled autofocus/> | ||
</div> | ||
<i class="fa-solid fa-ellipsis-vertical "></i> | ||
<i class="fa-solid fa-trash-can hidden"></i> | ||
</li>`; | ||
} | ||
}; | ||
|
||
window.addEventListener('DOMContentLoaded', showTodo(toDo)); | ||
MyTodoList.addEventListener('click', (e) => { | ||
const task = new Todo(); | ||
if (e.target.nodeName === 'INPUT') { | ||
e.target.parentElement.parentElement.style.backgroundColor = '#ffffa7'; | ||
e.target.parentElement.parentElement.children[1].style.display = 'none'; | ||
e.target.parentElement.parentElement.children[2].style.display = 'inline'; | ||
setTimeout(() => { | ||
e.target.parentElement.parentElement.style.backgroundColor = '#fff'; | ||
e.target.parentElement.parentElement.children[1].style.display = 'inline'; | ||
e.target.parentElement.parentElement.children[2].style.display = 'none'; | ||
}, 2000); | ||
} | ||
if (e.target.classList.contains('hidden')) { | ||
task.deleteTodo(e, todos); | ||
} | ||
}); | ||
|
||
const initAll = (input) => { | ||
input.value = ''; | ||
}; | ||
|
||
const TodoCreate = (input) => { | ||
const desc = input.value; | ||
const task = new Todo(desc); | ||
task.addTodo(task, todos); | ||
}; | ||
|
||
const description = document.querySelector('#new-task'); | ||
|
||
document.querySelector('#addBtn').addEventListener('click', () => { | ||
TodoCreate(description); | ||
initAll(description); | ||
showTodo(todos); | ||
}); | ||
|
||
document.querySelector('.input').addEventListener('keypress', (e) => { | ||
if (e.key === 'Enter' && (e.target.value)) { | ||
TodoCreate(description); | ||
initAll(description); | ||
showTodo(todos); | ||
} | ||
}); | ||
|
||
const TodoEdit = (e) => { | ||
const editing = e.parentElement.parentElement; | ||
const i = editing.id; | ||
editing.children[0].children[1].addEventListener('input', () => { | ||
const newValue = editing.children[0].children[1].value; | ||
editing.children[0].children[1].value = newValue; | ||
todos[i].description = newValue; | ||
localStorage.setItem('todos', JSON.stringify(todos)); | ||
}); | ||
}; | ||
|
||
MyTodoList.addEventListener('click', (e) => { | ||
if (e.target.disabled === true) { | ||
e.target.disabled = false; | ||
e.target.setAttribute('autofocus', true); | ||
TodoEdit(e.target); | ||
} | ||
}); | ||
|
||
todos = TodoLoad(); | ||
showTodo(todos); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters