Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions 1. Building a To-Do List Without Framework/submission/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous"
/>
<link
href="https://unpkg.com/[email protected]/dist/aos.css"
rel="stylesheet"
/>
</head>
<body>
<div class="container-fluid">
<div class="py-5"></div>
<div class="py-2" id="save-info"></div>
<div id="confirmation" class="pt-2"></div>
<div class="row">
<div class="col-12 d-flex justify-content-center">
<button
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#exampleModal"
>
ADD A TODO LIST
</button>
<div
class="modal fade"
id="exampleModal"
tabindex="-1"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div
class="modal-dialog modal-dialog-centered modal-dialog-scrollable"
>
<div class="modal-content">
<div class="modal-header">
<h5
class="modal-title"
id="exampleModalLabel"
>
ADD A TODO LIST
</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<form id="todo-form">
<div class="modal-body">
<div class="form-floating mb-3">
<input
type="text"
class="form-control"
id="floatingInput"
placeholder="Maths Homework"
name="todo-input"
/>
<label for="floatingInput"
>Title</label
>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-outline-danger"
data-bs-dismiss="modal"
>
Close
</button>
<button
type="submit"
class="btn btn-outline-success"
id="save-todo"
onclick="{saveTodo()}"
>
SAVE CHANGES
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="pt-5" id="todo-list" data-aos="fade-up"></div>
<div class="d-flex justify-content-center pt-3">
<button
class="btn btn-outline-success"
id="delete-todo"
onclick="{clearTodo()}"
>
Clear All
</button>
</div>
<div class="py-5"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf"
crossorigin="anonymous"
></script>
<script src="https://unpkg.com/[email protected]/dist/aos.js"></script>
<script src="./index.js" type="text/javascript"></script>
<script>
AOS.init();
</script>
</body>
</html>

139 changes: 139 additions & 0 deletions 1. Building a To-Do List Without Framework/submission/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
let todoList = [];

const generateTodoItem = (itemName) => {
const timeStamp = Date.now();
return {
id: "todo-item-" + timeStamp,
name: itemName,
timeStamp,
isDone: false,
};
};

const todoElement = (item, i) => {
const elem = document.createElement("div");
elem.id = item.id;
elem.innerHTML = `
<div class="row py-2 text-center">
<div class="col-3 text-center">${i + 1}</div>
<div class="col-3" id=${item.id}>
<input type="checkbox" onchange="markItem(event)" ${
item.isDone ? "checked" : ""
}/>
</div>
<div class="col-3">${item.name}</div>
<div class="col-3" id=${item.id}>
<button class="btn btn-outline-danger" onclick="deleteItem(event)">Delete Item</button>
</div>
</div> `;
return elem;
};

const renderList = () => {
const listElement = document.getElementById("todo-list");
listElement.innerHTML = "";
for (let i = 0; i < todoList.length; i++) {
const todoItem = todoElement(todoList[i], i);
listElement.appendChild(todoItem);
}
};

document.getElementById("todo-form").addEventListener("submit", (event) => {
Copy link
Member

Choose a reason for hiding this comment

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

Define event listeners at the end.

event.preventDefault();
const itemName = event.target["todo-input"].value;
if (itemName) {
todoList.push(generateTodoItem(itemName));
renderList();
saveTodo();
const alert = `
<div class="text-center alert alert-success alert-dismissible fade show shadow border-0" role="alert" data-aos="fade-down">
<strong>Congratulations, your item has been added.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#confirmation").html(alert);
event.target.reset();
}
});

const markItem = (event) => {
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some((todoItem) => {
if (todoItem.id === itemId) {
todoItem.isDone = !todoItem.isDone;
renderList();
saveTodo();
return true;
}
});
};

const deleteItem = (event) => {
const itemId = event.target.parentElement.id; // getting the id of the parent element to search in the array
todoList.some((todoItem, i) => {
if (todoItem.id === itemId) {
const approveDelete = confirm(
"Delete this item. If not then press cancel to undo"
); //taking input from user if they sure want to delete
if (approveDelete) {
todoList.splice(i, 1);
renderList();
saveTodo();
const alert = `
<div class="text-center alert alert-danger alert-dismissible fade show shadow border-0" role="alert" data-aos="fade-down">
<strong>Your item has been removed.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#confirmation").html(alert);
}
return true;
}
});
};

const saveTodo = () => {
localStorage.setItem("todoList", JSON.stringify(todoList)); // key and its value
const saveInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>${
todoList.length > 0
? "Your changes have been successfully saved."
: "Please add any item."
}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(saveInfo);
};

const clearTodo = () => {
if (todoList.length){
const approveClear = confirm("Are you sure you want to delete your all items");
if(approveClear){
`${localStorage.removeItem("todoList")}
${(todoList = [])}
${renderList()}`
Comment on lines +111 to +113
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to call functions inside template strings unless you are storing it in a variable.
And these statements do not return anything so it is of no use to call it in a template string

Copy link
Author

Choose a reason for hiding this comment

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

Okay got it.

const clearInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>Your items have been successfully removed.</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(clearInfo);
}
}
else{
const clearInfo = `
<div class="text-center alert alert-primary alert-dismissible fade show shadow border-0" role="alert" data-aos="zoom-in-down">
<strong>Please add any item</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
$("#save-info").html(clearInfo);
Copy link
Member

Choose a reason for hiding this comment

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

You should avoid the use of JQuery or any other library for a simple task which can be easily done without it.

Copy link
Author

Choose a reason for hiding this comment

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

I used jQuery just to display confirmation messages to the user. Should I remove the display messages and keep it simple?

Copy link
Member

Choose a reason for hiding this comment

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

You can do it without jQuery

Copy link
Author

Choose a reason for hiding this comment

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

Okay bhaiya give me some time.

}
};

try {
if (localStorage.getItem("todoList")) {
todoList = JSON.parse(localStorage.getItem("todoList"));
renderList();
}
} catch (e) {
console.log(e);
}
Comment on lines +132 to +139
Copy link
Member

Choose a reason for hiding this comment

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

You can use this code just after defining the todoList array.