Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 8 additions & 16 deletions 1. Building a To-Do List Without Framework/submission/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<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
Expand Down Expand Up @@ -73,16 +74,18 @@
<div class="modal-footer">
<button
type="button"
class="btn btn-outline-danger fw-bold"
class="btn btn-outline-danger"
data-bs-dismiss="modal"
>
Close
</button>
<button
type="submit"
class="btn btn-outline-success fw-bold"
class="btn btn-outline-success"
id="save-todo"
onclick="{saveTodo()}"
>
Save
SAVE CHANGES
</button>
</div>
</form>
Expand All @@ -91,19 +94,8 @@
</div>
</div>
</div>
<div id="confirmation" class="pt-5"></div>
<div id="todo-list" data-aos="fade-up"></div>

<div class="d-flex justify-content-center py-5 pt-5">
<button
class="btn btn-outline-success"
id="save-todo"
onclick="{saveTodo()}"
>
Save Changes
</button>
</div>
<div class="d-flex justify-content-center">
<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"
Expand Down
41 changes: 24 additions & 17 deletions 1. Building a To-Do List Without Framework/submission/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const todoElement = (item, i) => {
const elem = document.createElement("div");
elem.id = item.id;
elem.innerHTML = `
<div class="row py-2 pb-2 text-center">
<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)" ${
Expand Down Expand Up @@ -44,9 +44,10 @@ document.getElementById("todo-form").addEventListener("submit", (event) => {
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. Save Changes.
<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);
Expand All @@ -60,7 +61,7 @@ const markItem = (event) => {
if (todoItem.id === itemId) {
todoItem.isDone = !todoItem.isDone;
renderList();

saveTodo();
return true;
}
});
Expand Down Expand Up @@ -89,7 +90,7 @@ const deleteItem = (event) => {
});
};

const saveTodo = (e) => {
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">
Expand All @@ -104,22 +105,28 @@ const saveTodo = (e) => {
};

const clearTodo = () => {
const clearInfo = `
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
Copy Markdown
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
Copy Markdown
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>${
todoList.length > 0
? "Your items have been successfully removed."
: "Please add any item."
}
<strong>Your items have been successfully removed.</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
</div>`;
$("#save-info").html(clearInfo);
todoList.length > 0 // if array length is null then do not show confirm msg.
? `${confirm("Are you sure you want to delete your all items")}
${localStorage.removeItem("todoList")}
${(todoList = [])} // array made empty
${renderList()}`
: "";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should not do this. This is not at all readable and may not get properly formatted by eslint or other formatters.

Use a simple if condition.

}
}
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 {
Expand Down