Skip to content

Instantly share code, notes, and snippets.

@Shivraj97
Created May 13, 2020 14:28
Show Gist options
  • Select an option

  • Save Shivraj97/0a3442e5255b4d1a1054a8f412cd8164 to your computer and use it in GitHub Desktop.

Select an option

Save Shivraj97/0a3442e5255b4d1a1054a8f412cd8164 to your computer and use it in GitHub Desktop.
// 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