Created
May 13, 2020 14:28
-
-
Save Shivraj97/0a3442e5255b4d1a1054a8f412cd8164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // UI Class: Handle UI Tasks | |
| class UI { | |
| static displayBooks() { | |
| const books = Store.getBooks(); | |
| books.forEach((book) => UI.addBookToList(book)); | |
| } | |
| static addBookToList(book) { | |
| const list = document.querySelector("#book-list"); | |
| const row = document.createElement("tr"); | |
| row.innerHTML = ` | |
| <td>${book.title}</td> | |
| <td>${book.author}</td> | |
| <td>${book.isbn}</td> | |
| <td><a href="#" class="btn btn-danger btn-sm deleted">X</a></td> | |
| `; | |
| list.appendChild(row); | |
| } | |
| static deleteBook(el) { | |
| console.log(el); | |
| if (el.classList.contains("deleted")) { | |
| el.parentElement.parentElement.remove(); | |
| } | |
| } | |
| static showAlert(message, className) { | |
| const div = document.createElement("div"); | |
| div.className = `alert alert-${className}`; | |
| div.appendChild(document.createTextNode(message)); | |
| const container = document.querySelector(".container"); | |
| const form = document.querySelector("#book-form"); | |
| container.insertBefore(div, form); | |
| // Remove alert | |
| setTimeout(() => document.querySelector(".alert").remove(), 3000); | |
| } | |
| static clearFields() { | |
| document.querySelector("#title").value = ""; | |
| document.querySelector("#author").value = ""; | |
| document.querySelector("#isbn").value = ""; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment