Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Orlando_Morales | JS2_Module_Todo-List | Week3 #213

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
2 changes: 0 additions & 2 deletions week-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,5 +489,3 @@ const quotes = [
author: "Zig Ziglar",
},
];

// call pickFromArray with the quotes array to check you get a random quote
32 changes: 18 additions & 14 deletions week-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>To_do-list</title>
<link rel="stylesheet" href="style.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
/>
</head>
<body>
<form>
<div>
<input type="text" placeholder="New todo..." />
<div class="wrapper">
<header>Todo-List</header>
<div class="inputField">
<input type="text" placeholder="Add your new Todo" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user's should enter the todo, and also set the deadline date, the date input field is missing

<button><i class="fas fa-plus"></i></button>
</div>
<div>
<button type="submit">Add Todo</button>
<button
type="button"
id="remove-all-completed"
class="btn btn-primary mb-3"
>
Remove all completed
</button>
<ul class="todoList">
<!-- data are comes from local storage -->
</ul>
<div class="footer">
<span>You have <span class="pendingTasks"></span> pending tasks</span>
<button>Clear All</button>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need a clear all button from the requirements, we need a "Clear Completed Todo" button/feature

</div>
</form>
</div>

<script src="script.js"></script>
</body>
</html>
79 changes: 60 additions & 19 deletions week-3/todo-list/script.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
function populateTodoList(todos) {
let list = document.getElementById("todo-list");
// Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element.
}
const inputBox = document.querySelector(".inputField input");
const addBtn = document.querySelector(".inputField button");
const todoList = document.querySelector(".todoList");
const deleteAllBtn = document.querySelector(".footer button");

inputBox.onkeyup = () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good

let userEnteredValue = inputBox.value;
if (userEnteredValue.trim() != 0) {
addBtn.classList.add("active");
} else {
addBtn.classList.remove("active");
}
};

// These are the same todos that currently display in the HTML
// You will want to remove the ones in the current HTML after you have created them using JavaScript
let todos = [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by default, these tasks should be displayed on the home page

{ task: "Wash the dishes", completed: false },
{ task: "Do the shopping", completed: false },
];
showTasks();

populateTodoList(todos);
addBtn.onclick = () => {
let userEnteredValue = inputBox.value;
let getLocalStorageData = localStorage.getItem("New Todo");
if (getLocalStorageData == null) {
listArray = [];
} else {
listArray = JSON.parse(getLocalStorageData);
}
listArray.push(userEnteredValue);
localStorage.setItem("New Todo", JSON.stringify(listArray));

// This function will take the value of the input field and add it as a new todo to the bottom of the todo list. These new todos will need the completed and delete buttons adding like normal.
function addNewTodo(event) {
// The code below prevents the page from refreshing when we click the 'Add Todo' button.
event.preventDefault();
// Write your code here... and remember to reset the input field to be blank after creating a todo!
showTasks();
addBtn.classList.remove("active");
};

function showTasks() {
let getLocalStorageData = localStorage.getItem("New Todo");
if (getLocalStorageData == null) {
listArray = [];
} else {
listArray = JSON.parse(getLocalStorageData);
}
const pendingTasksNumb = document.querySelector(".pendingTasks");
pendingTasksNumb.textContent = listArray.length;
if (listArray.length > 0) {
deleteAllBtn.classList.add("active");
} else {
deleteAllBtn.classList.remove("active");
}
let newLiTag = "";
listArray.forEach((element, index) => {
newLiTag += `<li>${element}<span class="icon" onclick="deleteTask(${index})"><i class="fas fa-trash"></i></span> </li>`;
});
todoList.innerHTML = newLiTag;
inputBox.value = "";
}

// Advanced challenge: Write a fucntion that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not).
function deleteAllCompletedTodos() {
// Write your code here...
function deleteTask(index) {
let getLocalStorageData = localStorage.getItem("New Todo");
listArray = JSON.parse(getLocalStorageData);
listArray.splice(index, 1);
localStorage.setItem("New Todo", JSON.stringify(listArray));
showTasks();
}

deleteAllBtn.onclick = () => {
listArray = [];
localStorage.setItem("New Todo", JSON.stringify(listArray));
showTasks();
};
147 changes: 147 additions & 0 deletions week-3/todo-list/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,148 @@
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}

::selection {
color: #ffff;
background-color: rgb(142, 73, 232);
}

body {
width: 100%;
height: 100vh;
padding: 10px;
background-color: #ddd;
}

.wrapper {
background-color: #fff;
max-width: 400px;
width: 100%;
margin: 120px auto;
padding: 25px;
border-radius: 5px;
box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.1);
}

.wrapper header {
font-size: 30px;
font-weight: 600;
}

.wrapper .inputField {
margin: 20px 0;
width: 100%;
display: flex;
height: 45px;
}

.inputField input {
width: 85%;
height: 100%;
outline: none;
border-radius: 3px;
border: 1px solid #ccc;
font-size: 17px;
padding-left: 15px;
}

.inputField input:focus {
border-color: #8e49e8;
}

.inputField button {
width: 50px;
height: 100%;
border: none;
color: #fff;
margin-left: 5px;
font-size: 21px;
outline: none;
background: #8e49e8;
cursor: pointer;
border-radius: 3px;
opacity: 0.6;
pointer-events: none;
transition: all 0, 3s ease;
}

.inputField button:hover,
.footer button:hover {
background: #721ce3;
}

.inputField button.active {
opacity: 1;
pointer-events: auto;
}

.wrapper .todoList {
math-height: 250px;
overflow-y: auto;
}

.todoList li {
position: relative;
list-style: none;
margin-bottom: 8px;
background: #f2f2f2;
border-radius: 3px;
padding: 10px 15px;
cursor: default;
overflow: hidden;
word-wrap: break-word;
}

.todoList li .icon {
position: absolute;
height: 100%;
right: -50px;
top: 0%;
transform: traslateY(-50%);
background: #e74c3c;
width: 45px;
text-align: center;
color: #fff;
padding: 10px 15px;
border-radius: 0 3px 3px 0;
cursor: pointer;
transition: all 0.2s ease;
}

.todoList li:hover .icon {
right: 0px;
}

.wrapper .footer {
display: flex;
width: 100%;
margin-top: 20px;
align-items: center;
justify-content: space-between;
}

.footer button {
padding: 6px 10px;
border-radius: 3px;
border: none;
outline: none;
color: #fff;
font-weight: 400;
font-size: 16px;
margin-left: 5px;
background: #8e49e8;
cursor: pointer;
user-select: none;
opacity: 0.6;
pointer-events: none;
transition: all 0.3s ease;
}

.footer button.active {
opacity: 1;
pointer-events: auto;
}