|
| 1 | +let myLibrary = []; |
| 2 | + |
| 3 | + |
| 4 | +function book(title, author, numOfPages, readOrNot) { |
| 5 | + this.title = title; |
| 6 | + this.author = author; |
| 7 | + this.numOfPages = numOfPages; |
| 8 | + this.readOrNot = readOrNot; |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +function removeBookFromLibrary(index) { |
| 13 | + myLibrary.splice(index, 1); |
| 14 | + displayBook(); |
| 15 | + saveLibrary(); |
| 16 | +} |
| 17 | + |
| 18 | +function loadLibrary() { |
| 19 | + if (localStorage.getItem('myLibrary')) { |
| 20 | + myLibrary = JSON.parse(localStorage.getItem('myLibrary')); |
| 21 | + } |
| 22 | + else { |
| 23 | + myLibrary = []; |
| 24 | + } |
| 25 | + displayBook(); |
| 26 | +} |
| 27 | + |
| 28 | +function saveLibrary() { |
| 29 | + localStorage.setItem('myLibrary', JSON.stringify(myLibrary)); |
| 30 | +} |
| 31 | + |
| 32 | +function displayBook() { |
| 33 | + const libraryContainer = document.getElementById('library-container'); |
| 34 | + libraryContainer.innerHTML = ''; |
| 35 | + myLibrary.forEach((element, index) => { |
| 36 | + const bookCard = document.createElement('div'); |
| 37 | + bookCard.classList.add('book-card'); |
| 38 | + |
| 39 | + const title = document.createElement('h2'); |
| 40 | + title.textContent = `Title : ${element.title}`; |
| 41 | + |
| 42 | + const author = document.createElement('p'); |
| 43 | + author.textContent = `Author: ${element.author}`; |
| 44 | + |
| 45 | + const numOfPages = document.createElement('p'); |
| 46 | + numOfPages.textContent = `NumOfPages: ${element.numOfPages}`; |
| 47 | + |
| 48 | + const readOrNot = document.createElement('p'); |
| 49 | + readOrNot.textContent = `Read Or Not: ${element.readOrNot}`; |
| 50 | + |
| 51 | + const removeButton = document.createElement('button'); |
| 52 | + removeButton.textContent = 'Remove'; |
| 53 | + removeButton.setAttribute('data-index', index); |
| 54 | + removeButton.addEventListener('click', function () { |
| 55 | + removeBookFromLibrary(this.getAttribute('data-index')); |
| 56 | + }); |
| 57 | + |
| 58 | + |
| 59 | + bookCard.appendChild(title); |
| 60 | + bookCard.appendChild(author); |
| 61 | + bookCard.appendChild(numOfPages); |
| 62 | + bookCard.appendChild(readOrNot); |
| 63 | + bookCard.appendChild(removeButton); |
| 64 | + |
| 65 | + libraryContainer.appendChild(bookCard); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +document.addEventListener('DOMContentLoaded', function () { |
| 70 | + loadLibrary(); |
| 71 | + function addBookToTheLibrary(title, author, numOfPages, readOrNot) { |
| 72 | + const bookExists = myLibrary.some(book => book.title === title && book.author === author); |
| 73 | + if (!bookExists) { |
| 74 | + const newBook = new book(title, author, numOfPages, readOrNot) |
| 75 | + myLibrary.push(newBook); |
| 76 | + saveLibrary(); |
| 77 | + displayBook(); |
| 78 | + } else { |
| 79 | + const bookAlreadyExistsComponent = document.getElementById('book-exits-component'); |
| 80 | + bookAlreadyExistsComponent.innerHTML = ''; |
| 81 | + |
| 82 | + const message = document.createElement('p'); |
| 83 | + message.textContent = "Book Already Exist!!"; |
| 84 | + bookAlreadyExistsComponent.appendChild(message); |
| 85 | + |
| 86 | + setTimeout(() => { |
| 87 | + message.remove() |
| 88 | + }, 1000); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + const newBookBtn = document.getElementById('new-book-btn'); |
| 93 | + const bookFormContainer = document.getElementById('book-form-container'); |
| 94 | + const bookForm = document.getElementById('book-form'); |
| 95 | + |
| 96 | + newBookBtn.addEventListener('click', function () { |
| 97 | + bookFormContainer.style.display = 'block'; |
| 98 | + }) |
| 99 | + |
| 100 | + bookForm.addEventListener('submit', function (e) { |
| 101 | + e.preventDefault(); |
| 102 | + |
| 103 | + const title = document.getElementById('title').value; |
| 104 | + const author = document.getElementById('author').value; |
| 105 | + const numOfPages = document.getElementById('pages').value; |
| 106 | + const readOrNot = document.getElementById('readStatus').checked ? 'Read' : 'Not Read'; |
| 107 | + |
| 108 | + addBookToTheLibrary(title, author, numOfPages, readOrNot); |
| 109 | + displayBook(); |
| 110 | + |
| 111 | + bookForm.reset(); |
| 112 | + bookFormContainer.style.display = 'none'; |
| 113 | + }); |
| 114 | +}) |
| 115 | + |
0 commit comments