Skip to content

Commit

Permalink
Move remove book functionality to handler
Browse files Browse the repository at this point in the history
  • Loading branch information
amejid committed Jul 3, 2022
1 parent 68210f1 commit 88fec9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const addBook = () => {
bookView.render(model.state.books);
};

const removeBook = (e) => {
const id = bookView.getRemovedId(e);
const removeBook = (id) => {
model.removeBook(id);
bookView.render(model.state.books);
};
Expand Down
17 changes: 7 additions & 10 deletions modules/views/bookView.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class BookView {
#parentEl = document.querySelector('.books-container');

#targetId;

render(books) {
this.#parentEl.innerHTML = '';
if (!books) return;
Expand All @@ -17,15 +15,14 @@ class BookView {
});
}

getRemovedId(e) {
if (e.target.classList.contains('remove')) {
this.#targetId = +e.target.dataset.bookid;
}
return this.#targetId;
}

addHandlerRemover(handler) {
this.#parentEl.addEventListener('click', handler);
this.#parentEl.addEventListener('click', (e) => {
const btn = e.target.closest('.remove');
if (!btn) return;
const bookId = +btn.dataset.bookid;

handler(bookId);
});
}
}

Expand Down

0 comments on commit 88fec9e

Please sign in to comment.