Advertisement
GeorgiLukanov87

08. Book Library

Mar 1st, 2023
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.   let url = 'http://localhost:3030/jsonstore/collections/books'
  3.   let loadBtn = document.getElementById('loadBooks');
  4.   let table = document.querySelector('table tbody');
  5.   let form = document.getElementById('form');
  6.   let submitBtn = document.querySelector('#form > button')
  7.  
  8.   loadBtn.addEventListener('click', loadAllBooks);
  9.   submitBtn.addEventListener('click', createNewBook)
  10.  
  11.   let details = {};
  12.   let idSearch = [];
  13.  
  14.   function loadAllBooks(e) {
  15.     fetch(url)
  16.       .then(res => res.json())
  17.       .then(data => {
  18.         for (obj in data) {
  19.           let author = data[obj]['author']
  20.           let title = data[obj]['title']
  21.  
  22.           details[author] = obj;
  23.  
  24.           let newBookTr = document.createElement('tr');
  25.  
  26.           let authorTd = document.createElement('td');
  27.           authorTd.textContent = author;
  28.  
  29.           let titleTd = document.createElement('td');
  30.           titleTd.textContent = title;
  31.  
  32.           let btnsTd = document.createElement('td');
  33.           let editBtn = document.createElement('button');
  34.           editBtn.textContent = 'Edit';
  35.           let deleteBtn = document.createElement('button');
  36.           deleteBtn.textContent = 'Delete';
  37.           btnsTd.appendChild(editBtn);
  38.           btnsTd.appendChild(deleteBtn);
  39.  
  40.           newBookTr.appendChild(authorTd);
  41.           newBookTr.appendChild(titleTd);
  42.           newBookTr.appendChild(btnsTd);
  43.  
  44.           table.appendChild(newBookTr);
  45.  
  46.           editBtn.addEventListener('click', editBook);
  47.           deleteBtn.addEventListener('click', deleteBook);
  48.  
  49.         }
  50.       })
  51.  
  52.   }
  53.  
  54.   function editBook(event) {
  55.     let data = event.currentTarget.parentNode.parentNode;
  56.     let author = data.children[0].textContent;
  57.     let title = data.children[1].textContent;
  58.     idSearch.push(author, title)
  59.  
  60.     //FORM
  61.     let h3 = document.createElement('h3');
  62.     h3.textContent = 'Edit FORM';
  63.  
  64.     let titleLabel = document.createElement('label');
  65.     titleLabel.textContent = 'TITLE';
  66.  
  67.     let inputTitle = document.createElement('input');
  68.     inputTitle.setAttribute('type', 'text');
  69.     inputTitle.setAttribute('name', 'title');
  70.     inputTitle.value = title;
  71.  
  72.     let authorLabel = document.createElement('label');
  73.     authorLabel.textContent = 'AUTHOR';
  74.  
  75.     let inputAuthor = document.createElement('input');
  76.     inputAuthor.setAttribute('type', 'text');
  77.     inputAuthor.setAttribute('name', 'author');
  78.     inputAuthor.value = author;
  79.  
  80.     let saveBtn = document.createElement('button');
  81.     saveBtn.textContent = 'SAVE'
  82.     //ENDFORM
  83.     form.innerHTML = ''
  84.     form.appendChild(h3)
  85.     form.appendChild(titleLabel)
  86.     form.appendChild(inputTitle)
  87.     form.appendChild(authorLabel)
  88.     form.appendChild(inputAuthor)
  89.     form.appendChild(saveBtn)
  90.  
  91.     saveBtn.addEventListener('click', function editBookInfo(event) {
  92.       let changedTitle = form.children[2].value;
  93.       let changedAuthor = form.children[4].value;
  94.       let id = details[idSearch[0]];
  95.  
  96.       let putUrl = `http://localhost:3030/jsonstore/collections/books/${id}`
  97.       fetch(putUrl, {
  98.         method: "PUT",
  99.         headers: {
  100.           'content-type': 'application/json'
  101.         },
  102.         body: JSON.stringify({
  103.           'author': changedAuthor,
  104.           'title': changedTitle
  105.         })
  106.       })
  107.       document.location.reload()
  108.  
  109.     })
  110.  
  111.   }
  112.  
  113.   function deleteBook(event) {
  114.     let currentAuthor = event.target.parentNode.parentNode.children[0].textContent;
  115.     let id = details[currentAuthor];
  116.  
  117.     let delUrl = `http://localhost:3030/jsonstore/collections/books/${id}`
  118.     fetch(delUrl, {
  119.       method: "DELETE",
  120.       headers: {
  121.         'content-type': 'application/json'
  122.       },
  123.     })
  124.     document.location.reload()
  125.   }
  126.  
  127.  
  128.   function createNewBook(e) {
  129.     let title = e.target.parentNode.children[2].value
  130.     let author = e.target.parentNode.children[4].value
  131.     if (title === '' || author === '') {
  132.       return;
  133.     }
  134.     let putUrl = `http://localhost:3030/jsonstore/collections/books/`
  135.     fetch(putUrl, {
  136.       method: "POST",
  137.       headers: {
  138.         'content-type': 'application/json'
  139.       },
  140.       body: JSON.stringify({
  141.         'author': author,
  142.         'title': title
  143.       })
  144.     })
  145.     document.location.reload()
  146.  
  147.   }
  148.  
  149. }
  150.  
  151. attachEvents();
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement