Mwafrika_Josue

dataBook

Feb 9th, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. const inputTitle = document.querySelector('.input-title');
  2. const inputAuthor = document.querySelector('.input-author');
  3. const bookShelf = document.querySelector('.book-shelf');
  4. const bookCard = document.createElement('div');
  5.  
  6. const Book = function BookList(title, author) {
  7. this.title = title;
  8. this.author = author;
  9.  
  10. this.removeButton = function removeButton(element) {
  11. if (element.classList.contains('remove-button')) {
  12. element.parentElement.remove(element);
  13. }
  14. };
  15.  
  16. this.getToloStorage = function getToloStorage() {
  17. let book;
  18. if (!localStorage.getItem('books')) {
  19. book = [];
  20. } else {
  21. book = JSON.parse(localStorage.getItem('books'));
  22. }
  23. return book;
  24. };
  25.  
  26. this.addToLocalStorage = function addToLocalStorage(book) {
  27. const books = this.getToloStorage();
  28. books.push(book);
  29. localStorage.setItem('books', JSON.stringify(books));
  30. };
  31.  
  32. // setTimeout(() => {
  33. this.removeToLocalStorage = function removeToLocalStorage(id) {
  34. let books = this.getToloStorage();
  35. const tempBooks = [];
  36. books.forEach((book, index) => {
  37. console.log(parseInt(id, 10));
  38. if (index !== parseInt(id, 10)) {
  39. tempBooks.push(book);
  40. }
  41. });
  42. books = tempBooks;
  43. localStorage.setItem('books', JSON.stringify(books));
  44. };
  45. // }, 10000);
  46.  
  47. this.clearFields = function clearFields() {
  48. inputTitle.value = '';
  49. inputAuthor.value = '';
  50. };
  51.  
  52. this.addBook = function addBook(book, id) {
  53. bookCard.innerHTML += `
  54. <div id=${id}>
  55. <p>${book.title}</p>
  56. <p>${book.author}</p>
  57. <button type="submit" class="remove-button">Remove</button><br><br>
  58. <hr>
  59. </div>
  60. `;
  61. bookShelf.appendChild(bookCard);
  62. };
  63.  
  64. this.showBook = function showBook() {
  65. const obj = this.getToloStorage();
  66.  
  67. obj.forEach((book, index) => {
  68. this.addBook(book, index);
  69. });
  70. };
  71. };
  72.  
  73. document.querySelector('.book-input').addEventListener('submit', (e) => {
  74. e.preventDefault();
  75. const title = inputTitle.value;
  76. const author = inputAuthor.value;
  77.  
  78. setTimeout(() => {
  79. if (title && author) {
  80. const bookList = new Book(title, author);
  81. bookList.addBook(bookList);
  82. bookList.addToLocalStorage(bookList);
  83. bookList.clearFields();
  84. }
  85. }, 1000);
  86. });
  87.  
  88. bookShelf.addEventListener('click', (event) => {
  89. const bookList = new Book();
  90. bookList.removeButton(event.target);
  91. bookList.removeToLocalStorage(event.target.parentElement.id);
  92. });
  93. const bookList = new Book();
  94.  
  95. document.addEventListener('DOMContentLoaded', (e) => {
  96. bookList.showBook();
  97. // bookList.removeToLocalStorage(e.target.parentElement.id);
  98. });
  99.  
Advertisement
Add Comment
Please, Sign In to add comment