Skip to content

Commit

Permalink
Merge pull request #4 from Amedzro-Elikplim/interactive-list
Browse files Browse the repository at this point in the history
INTERACTIVE LIST
  • Loading branch information
Amedzro-Elikplim authored Feb 27, 2022
2 parents b6b12e6 + 249011f commit 6d74fa0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
6 changes: 3 additions & 3 deletions dist/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 28 additions & 7 deletions modules/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ class Task {
this.refresh();
}

delete(description) {
this.array = JSON.parse(localStorage.getItem('tasks'));
const index = this.array.findIndex((item) => item.description === description);

updateIndex = (index) => {
const num = index + 1;
for (let i = num; i < this.array.length; i += 1) {
this.array[i].index -= 1;
}
}

delete(description) {
this.array = JSON.parse(localStorage.getItem('tasks'));
const index = this.array.findIndex((item) => item.description === description);

this.updateIndex(index);

this.array.splice(index, 1);
localStorage.setItem('tasks', JSON.stringify(this.array));
Expand Down Expand Up @@ -101,6 +105,12 @@ class Task {
}
};

checked = (option, i) => {
const tasks = JSON.parse(localStorage.getItem('tasks'));
tasks[i].completed = option;
localStorage.setItem('tasks', JSON.stringify(tasks));
}

createList = (description, i) => {
const li = document.createElement('li');
const div = document.createElement('div');
Expand All @@ -127,17 +137,28 @@ class Task {
const description = li.children[0].children[1];
if (checkbox.checked) {
description.style.textDecoration = 'line-through';
this.checked(true, i);
} else {
description.style.textDecoration = 'none';
this.checked(false, i);
}
});

return li;
};

clearAll() {
this.removeChild();
localStorage.clear();
removeCompleted = () => {
const tasks = JSON.parse(localStorage.getItem('tasks'));

const result = tasks.filter((item) => item.completed === true);

for (let i = 0; i < result.length; i += 1) {
this.delete(result[i].description);
}
}

clearCompleted() {
this.removeCompleted();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ input.addEventListener('keypress', (e) => {

clear.addEventListener('click', (e) => {
e.preventDefault();
task.clearAll();
task.clearCompleted();
});

task.showTasks();
1 change: 1 addition & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ body {
margin-right: auto;
box-shadow: 1px 1px 10px gray;
margin-top: 30px;
border-radius: 10px;
}

.header {
Expand Down

0 comments on commit 6d74fa0

Please sign in to comment.