nurulfp

JS-Bookshelf

Aug 15th, 2022 (edited)
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const books = [];
  2. const RENDER_EVENT = "render-book";
  3. const SAVED_EVENT = "saved-book";
  4. const STORAGE_KEY = "BOOK_APPS";
  5.  
  6. function isStorageExist() {
  7.   if (typeof Storage === undefined) {
  8.     alert("Browser kamu tidak mendukung local storage");
  9.     return false;
  10.   }
  11.   return true;
  12. }
  13.  
  14. function saveData() {
  15.   if (isStorageExist()) {
  16.     const parsed = JSON.stringify(books);
  17.     localStorage.setItem(STORAGE_KEY, parsed);
  18.     document.dispatchEvent(new Event(SAVED_EVENT));
  19.   }
  20. }
  21.  
  22. function generateBookObject(title, author, year, isCompleted) {
  23.   return {
  24.     id: +new Date(),
  25.     title,
  26.     author,
  27.     year,
  28.     isCompleted,
  29.   };
  30. }
  31.  
  32. document.addEventListener("DOMContentLoaded", function () {
  33.   const submitForm = document.getElementById("inputBook");
  34.   const searchButton = document.getElementById("searchSubmit");
  35.  
  36.   // submit form input
  37.   submitForm.addEventListener("submit", function (event) {
  38.     event.preventDefault();
  39.     addBook();
  40.   });
  41.  
  42.   // search button
  43.   searchButton.addEventListener("click", function (event) {
  44.     event.preventDefault();
  45.     searchBooks();
  46.   });
  47.  
  48.   // menutup tab edit
  49.   const closeForm = document.getElementById("closeForm");
  50.   closeForm.addEventListener("click", function (event) {
  51.     event.preventDefault();
  52.     const edit = document.querySelector(".div-edit");
  53.     edit.setAttribute("hidden", "");
  54.   });
  55.  
  56.   if (isStorageExist()) {
  57.     loadDataFromStorage();
  58.   }
  59. });
  60.  
  61. // mencari data buku sesuai Id
  62. function findBook(bookId) {
  63.   for (const bookItem of books) {
  64.     if (bookItem.id == bookId) {
  65.       return bookItem;
  66.     }
  67.   }
  68.   return null;
  69. }
  70.  
  71. // mencari index buku dari local storage
  72. function findBookIndex(bookId) {
  73.   for (const index in books) {
  74.     if (books[index].id === bookId) {
  75.       return index;
  76.     }
  77.   }
  78.   return -1;
  79. }
  80.  
  81. // membuat data buku
  82. function addBook() {
  83.   const bookTitle = document.getElementById("inputBookTitle").value;
  84.   const bookAuthor = document.getElementById("inputBookAuthor").value;
  85.   const bookYear = document.getElementById("inputBookYear").value;
  86.   const bookCompleted = document.getElementById("completed").checked;
  87.  
  88.   const bookObject = generateBookObject(bookTitle, bookAuthor, bookYear, bookCompleted);
  89.   books.push(bookObject);
  90.  
  91.   document.dispatchEvent(new Event(RENDER_EVENT));
  92.   saveData();
  93. }
  94.  
  95. // input buku
  96. function inputBook(bookObject) {
  97.   const textTitle = document.createElement("h2");
  98.   textTitle.innerText = bookObject.title;
  99.  
  100.   const textAuthor = document.createElement("p");
  101.   textAuthor.innerText = bookObject.author;
  102.  
  103.   const textYear = document.createElement("p");
  104.   textYear.innerText = bookObject.year;
  105.  
  106.   const textContainer = document.createElement("div");
  107.   textContainer.classList.add("inner");
  108.   textContainer.append(textTitle, textAuthor, textYear);
  109.  
  110.   const container = document.createElement("div");
  111.   container.classList.add("item");
  112.   container.append(textContainer);
  113.   container.setAttribute("id", `book-${bookObject.id}`);
  114.  
  115.   if (bookObject.isCompleted) {
  116.     const undoButton = document.createElement("button");
  117.     undoButton.classList.add("undo-button");
  118.  
  119.     undoButton.addEventListener("click", function () {
  120.       undoBookFromCompleted(bookObject.id);
  121.     });
  122.  
  123.     const editButton = document.createElement("button");
  124.     editButton.classList.add("edit-button");
  125.  
  126.     editButton.addEventListener("click", function () {
  127.       editBook(bookObject.id);
  128.     });
  129.  
  130.     const trashButton = document.createElement("button");
  131.     trashButton.classList.add("trash-button");
  132.  
  133.     trashButton.addEventListener("click", function () {
  134.       if (confirm("Apa anda yakin ingin menghapus buku?")) {
  135.         removeBookFromCompleted(bookObject.id);
  136.       } else {
  137.         return;
  138.       }
  139.     });
  140.  
  141.     container.append(undoButton, editButton, trashButton);
  142.   } else {
  143.     const checkButton = document.createElement("button");
  144.     checkButton.classList.add("check-button");
  145.  
  146.     checkButton.addEventListener("click", function () {
  147.       addBookToCompleted(bookObject.id);
  148.     });
  149.  
  150.     const editButton = document.createElement("button");
  151.     editButton.classList.add("edit-button");
  152.  
  153.     editButton.addEventListener("click", function () {
  154.       editBook(bookObject.id);
  155.     });
  156.  
  157.     const trashButton = document.createElement("button");
  158.     trashButton.classList.add("trash-button2");
  159.  
  160.     trashButton.addEventListener("click", function () {
  161.       if (confirm("Apa anda yakin ingin menghapus buku?")) {
  162.         removeBookFromCompleted(bookObject.id);
  163.       } else {
  164.         return;
  165.       }
  166.     });
  167.  
  168.     container.append(checkButton, editButton, trashButton);
  169.   }
  170.  
  171.   return container;
  172. }
  173.  
  174. // menambah buku ke rak selesai dibaca
  175.  
  176. function addBookToCompleted(bookId) {
  177.   const bookTarget = findBook(bookId);
  178.  
  179.   if (bookTarget == null) return;
  180.  
  181.   bookTarget.isCompleted = true;
  182.   document.dispatchEvent(new Event(RENDER_EVENT));
  183.   saveData();
  184. }
  185.  
  186. // menghapus buku dari rak selesai dibaca
  187. function removeBookFromCompleted(bookId) {
  188.   const bookTarget = findBookIndex(bookId);
  189.  
  190.   if (bookTarget === -1) return;
  191.  
  192.   books.splice(bookTarget, 1);
  193.   document.dispatchEvent(new Event(RENDER_EVENT));
  194.   saveData();
  195. }
  196.  
  197. // memindahkan buku ke rak belum selesai dibaca
  198. function undoBookFromCompleted(bookId) {
  199.   const bookTarget = findBook(bookId);
  200.  
  201.   if (bookTarget == null) return;
  202.  
  203.   bookTarget.isCompleted = false;
  204.   document.dispatchEvent(new Event(RENDER_EVENT));
  205.   saveData();
  206. }
  207.  
  208. // menampilkan tab edit dan data buku
  209. function editBook(bookId) {
  210.   const edit = document.querySelector(".div-edit");
  211.   edit.removeAttribute("hidden");
  212.  
  213.   const bookTarget = findBook(bookId);
  214.  
  215.   //menampilkan data buku di form edit
  216.   const editBookTitle = document.getElementById("editBookTitle");
  217.   editBookTitle.value = bookTarget.title;
  218.   const editBookAuthor = document.getElementById("editBookAuthor");
  219.   editBookAuthor.value = bookTarget.author;
  220.   const editYear = document.getElementById("editBookYear");
  221.   editYear.value = bookTarget.year;
  222.   const editBookCompleted = document.getElementById("editCompleted");
  223.   editBookCompleted.checked = bookTarget.isCompleted;
  224.  
  225.   const submitEdit = document.getElementById("edit-submit");
  226.   submitEdit.addEventListener("click", function (event) {
  227.     // memasukan data baru pada buku yang diedit
  228.     updateEditBook(editBookTitle.value, editBookAuthor.value, editYear.value, editBookCompleted.checked, bookObject.id);
  229.  
  230.     // menutup tab edit ketika selesai edit buku
  231.     const edit = document.querySelector(".div-edit");
  232.     edit.setAttribute("hidden", "");
  233.     event.preventDefault();
  234.   });
  235. }
  236.  
  237. // update data buku di local storage
  238. function updateEditBook(title, author, year, completed, id) {
  239.   // mengambil data pada local storage dan dikonversi dari String menjadi Objek
  240.   const StorageBook = JSON.parse(localStorage[STORAGE_KEY]);
  241.   const bookIndex = findBookIndex(bookId);
  242.  
  243.   // membentuk data baru pada buku
  244.   StorageBook[bookIndex] = {
  245.     id: id,
  246.     title: title,
  247.     author: author,
  248.     year: year,
  249.     isCompleted: completed,
  250.   };
  251.  
  252.   // konversi data menjadi String
  253.   const parsed = JSON.stringify(StorageBook);
  254.   localStorage.setItem(STORAGE_KEY, parsed);
  255.  
  256.   // memuat halaman setelah data diubah
  257.   location.reload(true);
  258. }
  259.  
  260. // menampilkan data yang disimpan dalam local storage
  261. function loadDataFromStorage() {
  262.   const serializedData = localStorage.getItem(STORAGE_KEY);
  263.   let data = JSON.parse(serializedData);
  264.  
  265.   if (data !== null) {
  266.     for (const book of data) {
  267.       books.push(book);
  268.     }
  269.   }
  270.   document.dispatchEvent(new Event(RENDER_EVENT));
  271. }
  272.  
  273. // mencari buku
  274. function searchBooks() {
  275.   const titleBook = document.getElementById("searchBookTitle").value.toLowerCase();
  276.   const bookList = document.querySelectorAll(".inner");
  277.   for (render of bookList) {
  278.     if (render.innerText.toLowerCase().includes(titleBook) === false) {
  279.       render.parentElement.remove();
  280.     }
  281.   }
  282. }
  283.  
  284. /**function searchBooks() {
  285.   const titleBook = document.getElementById("searchBookTitle").value.toLowerCase();
  286.   const bookList = document.querySelectorAll(".inner");
  287.   for (render of bookList) {
  288.     if (render.innerText.toLowerCase().includes(titleBook) === false) {
  289.       render.parentElement.style.display = "none";
  290.     } else {
  291.       render.parentElement.style.display = "block";
  292.     }
  293.   }
  294. }**/
  295.  
  296. document.addEventListener(RENDER_EVENT, function () {
  297.   const uncompletedBOOKList = document.getElementById("uncompletedBooks");
  298.   uncompletedBOOKList.innerHTML = "";
  299.  
  300.   const completedBOOKList = document.getElementById("completedBooks");
  301.   completedBOOKList.innerHTML = "";
  302.  
  303.   for (const bookItem of books) {
  304.     const bookElement = inputBook(bookItem);
  305.     if (!bookItem.isCompleted) {
  306.       uncompletedBOOKList.append(bookElement);
  307.     } else {
  308.       completedBOOKList.append(bookElement);
  309.     }
  310.   }
  311. });
  312.  
Advertisement
Add Comment
Please, Sign In to add comment