Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. //Book Class: Represents a Book
  2. class Book {
  3. constructor(title, author, isbn) {
  4. this.title = title;
  5. this.author = author;
  6. this.isbn = isbn;
  7.  
  8. }
  9.  
  10. }
  11. //UI Class: Handle UI Taksks
  12. class UI {
  13. static displayBooks() {
  14. const books = Store.getBooks();
  15.  
  16. books.forEach((book) => UI.addBookToList());
  17. }
  18. static addBookTolist(book) {
  19.  
  20. const list = document.querySelector('#book-list');
  21.  
  22. const row = document.createElement('tr');
  23.  
  24. row.innerHTML = `
  25. <td>${book.title}</td>
  26. <td>${book.auther}</td>
  27. <td>${book.isbn}</td>
  28. <td><a herf="#" class"btn-danger btn-sm delete=">x</a></td>
  29. `;
  30.  
  31. list.appendChild(row);
  32. }
  33.  
  34. static deleteBook(el) {
  35. if(el.classList.contains('delete')) {
  36. el.parentElement.parentElement.remove();
  37.  
  38. }
  39.  
  40. }
  41.  
  42. static showAlert(messege, className) {
  43. const div = document.createElement('div');
  44. div.className = `alert alert-${className}`;
  45. div.appendChild(document.creatTextNode(message));
  46. const container = document.querySelector('.container');
  47. const form = document.querySelector('#book-form');
  48. container.insertBefore(dive, form);
  49.  
  50. //vanish in 3 seconds
  51. setTimeout(() => document.querySelector('.alert').remove(),
  52. 3000);
  53. }
  54. static clearFields(){
  55. document.querySelector('#title').value = '';
  56. document.querySelector('author').value = '';
  57. document.querySelector('#isbn').value = '';
  58.  
  59. }
  60. }
  61.  
  62. //store Class: Hndless Storage
  63. class Store {
  64. static getBooks() {
  65. let books;
  66. if(localStorage.getItem('books') === null) {
  67. books = [];
  68. } else {
  69. books = JSON.parse(localstorage.getItem('books'));
  70. }
  71. return books;
  72. }
  73.  
  74. static addBook(book) {
  75.  
  76. const books = Store.getBooks();
  77. books.push(book);
  78. localStorage.setItem('books', JSON.stringify(books));
  79. }
  80. static removeBook(isbn) {
  81. const books = Store.getBooks();
  82.  
  83. books.forEach((book, index) => {
  84. if(book.isbn === isbn) {
  85. books.splice(index,1);
  86.  
  87. }
  88. });
  89.  
  90. localStorage.setItem('books',JSON.stringify(books));
  91. }
  92. }
  93.  
  94. //Event: Display Books
  95. document.addEventListener('DOMContentLoaded', UI.displayBooks);
  96.  
  97. // Event: Add a Book
  98. document.querrySelector('#book-form').addEventListener('submit', (e)
  99. => {
  100.  
  101. // Prevent actual submit
  102. e.preventDefult();
  103.  
  104. //Get form values
  105.  
  106. const title = document.querySelector('#title').value;
  107. const author = document.querySelector('#author').value;
  108. const isbn = document.querySelector('#isbn').value;
  109.  
  110. //Validate
  111. if(title === '' || author === '' || isbn === '') {
  112. UI.showAlert('Please fill in all fields', 'danger');
  113. } else {
  114. //instatiate book
  115. const book = new Book(title, author, isbn);
  116. //Add Book to UI
  117. UI.addBookTolist(book);
  118.  
  119. //add book to store
  120. Store.addBook(book);
  121.  
  122. //Show success message
  123. UI.showAlert('Book Added', 'success');
  124.  
  125. //Clear fields
  126. UI.clearFields();
  127. }
  128. });
  129.  
  130. //Event: Remove a book
  131. document.querySelector('#book-list').addEventListener('click',(e) =>
  132. {
  133. // Remove Book from UI
  134. UI.deleteBook(e.target);
  135.  
  136. //remove book from store
  137. Store.removeBook(e.target.parentElement.previousElementSibling.textContent);
  138.  
  139. //show success message
  140. UI.showAlert('Book Removed', 'success');
  141. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement