Advertisement
svetlyoek

Untitled

Jul 11th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. import * as data from './data.js';
  2.  
  3. const loadBtn = document.getElementById('loadBooks');
  4. const createBtn = document.querySelector('form > button')
  5. const tableBody = document.querySelector('table tbody');
  6. const inputTitle = document.getElementById('title');
  7. const inputAuthor = document.getElementById('author');
  8. const inputIsbn = document.getElementById('isbn');
  9.  
  10. window.onload = (e) => {
  11. tableBody.innerHTML = '';
  12. };
  13.  
  14. loadBtn.addEventListener('click', async function loadBooks() {
  15.  
  16. let books = '';
  17.  
  18. try {
  19.  
  20. books = await data.getBooks();
  21.  
  22. books = books.sort((a, b) => b.created - a.created);
  23.  
  24. tableBody.innerHTML = '';
  25.  
  26. books.forEach(b => {
  27.  
  28. const editBtn = document.createElement('button');
  29. editBtn.classList.add('edit');
  30. editBtn.textContent = 'Edit';
  31.  
  32. const deleteBtn = document.createElement('button');
  33. deleteBtn.classList.add('delete');
  34. deleteBtn.textContent = 'Delete';
  35.  
  36. const newRow = document.createElement('tr');
  37. const titleData = document.createElement('td');
  38. const authorData = document.createElement('td');
  39. const isbnData = document.createElement('td');
  40.  
  41. titleData.textContent = b.title;
  42. authorData.textContent = b.author;
  43. isbnData.textContent = b.isbn;
  44.  
  45. newRow.appendChild(titleData);
  46. newRow.appendChild(authorData);
  47. newRow.appendChild(isbnData);
  48.  
  49. const buttonsData = document.createElement('td');
  50. buttonsData.appendChild(editBtn);
  51. buttonsData.appendChild(deleteBtn);
  52.  
  53. newRow.appendChild(buttonsData);
  54.  
  55. tableBody.appendChild(newRow);
  56.  
  57. deleteBtn.addEventListener('click', async function () {
  58.  
  59. await data.deleteBook(b.objectId);
  60. loadBooks();
  61. });
  62.  
  63. editBtn.addEventListener('click', function () {
  64.  
  65. newRow.innerHTML = '';
  66.  
  67. const titleInput = document.createElement('input');
  68. const authorInput = document.createElement('input');
  69. const isbmInput = document.createElement('input');
  70. const cancelBtn = document.createElement('button');
  71. const applyBtn = document.createElement('button');
  72.  
  73. titleInput.value = b.title;
  74. authorInput.value = b.author;
  75. isbmInput.value = b.isbn;
  76.  
  77. cancelBtn.textContent = 'Cancel';
  78. applyBtn.textContent = 'Apply';
  79.  
  80. const firstData = document.createElement('td');
  81. const secondData = document.createElement('td');
  82. const thirdData = document.createElement('td');
  83. const newButtonsData = document.createElement('td');
  84.  
  85. newButtonsData.appendChild(applyBtn);
  86. newButtonsData.appendChild(cancelBtn);
  87.  
  88. firstData.appendChild(titleInput);
  89. secondData.appendChild(authorInput);
  90. thirdData.appendChild(isbmInput);
  91.  
  92. newRow.appendChild(firstData);
  93. newRow.appendChild(secondData);
  94. newRow.appendChild(thirdData);
  95. newRow.appendChild(newButtonsData);
  96.  
  97. cancelBtn.addEventListener('click', async function () {
  98.  
  99. loadBooks();
  100.  
  101. });
  102.  
  103. applyBtn.addEventListener('click', async function () {
  104.  
  105. if (titleInput.value != '' && authorInput.value != '' && isbmInput.value != '') {
  106.  
  107. const editedObj = {
  108.  
  109. title: titleInput.value,
  110. author: authorInput.value,
  111. isbn: isbmInput.value
  112. }
  113.  
  114. await data.updateBook(editedObj, b.objectId);
  115.  
  116. loadBooks();
  117. }
  118. });
  119. });
  120. });
  121.  
  122. } catch (err) {
  123.  
  124. console.log(err);
  125. }
  126. });
  127.  
  128. createBtn.addEventListener('click', async function (e) {
  129. e.preventDefault();
  130.  
  131. if (inputTitle.value != '' && inputAuthor.value != '' && inputIsbn.value != '') {
  132.  
  133. const newBook = {
  134. title: inputTitle.value,
  135. author: inputAuthor.value,
  136. isbn: inputIsbn.value
  137. }
  138.  
  139. await data.createBook(newBook);
  140.  
  141. inputTitle.value = '';
  142. inputAuthor.value = '';
  143. inputIsbn.value = '';
  144. }
  145. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement