Advertisement
Iv555

Untitled

Mar 31st, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. function attachEvents() {
  2. const BASE_URL = 'http://localhost:3030/jsonstore/collections/books/';
  3. const btnSubmit = document.querySelector('#form > button');
  4. const btnLoadBooks = document.getElementById('loadBooks');
  5. const inputTitle = document.querySelector('input[name="title"]');
  6. const inputAuthor = document.querySelector('input[name="author"]');
  7. const tableBody = document.querySelector('tbody');
  8. const formH3 = document.querySelector('#form > h3')
  9. btnLoadBooks.addEventListener('click', loadAllBooks);
  10. btnSubmit.addEventListener('click', createOrEditBook);
  11. let editBookId = null;
  12.  
  13. async function loadAllBooks() {
  14. tableBody.innerHTML = '';
  15. const initial = await fetch(BASE_URL);
  16. const data = await initial.json();
  17.  
  18. for (const key in data) {
  19. const newRow = createElements('tr', '', tableBody);
  20.  
  21. //create title and author cells (td)
  22. createElements('td', data[key].title, newRow);
  23. createElements('td', data[key].author, newRow);
  24.  
  25. const tdAction = createElements('td', '', newRow);
  26. const btnEdit = createElements('button', 'Edit', tdAction, key);
  27. const btnDelete = createElements('button', 'Delete', tdAction, key);
  28. btnEdit.addEventListener('click', getBookForEdit)
  29. btnDelete.addEventListener('click', deleteBook)
  30. }
  31. }
  32.  
  33. async function createOrEditBook() {
  34. let title = inputTitle.value;
  35. let author = inputAuthor.value;
  36. let url = BASE_URL;
  37. if (title && author) {
  38. const httpHeaders = {
  39. method: 'POST',
  40. body: JSON.stringify({ title, author }),
  41. }
  42. if (formH3.textContent === 'Edit FORM') {
  43. httpHeaders.method = 'PUT';
  44. url += editBookId;
  45. formH3.textContent = 'FORM';
  46. btnSubmit.textContent = 'Submit';
  47.  
  48. }
  49. await fetch(url, httpHeaders);
  50. loadAllBooks();
  51.  
  52. inputTitle.value = '';
  53. inputAuthor.value = '';
  54.  
  55. }
  56. }
  57.  
  58. function getBookForEdit() {
  59. editBookId = this.id;
  60. fetch(`${BASE_URL}${editBookId}`)
  61. .then((res) => res.json())
  62. .then((obj) => {
  63. formH3.textContent = 'Edit FORM';
  64. btnSubmit.textContent = 'SAVE';
  65. inputTitle.value = obj.title;
  66. inputAuthor.value = obj.author;
  67. })
  68. .catch((err)=> console.error(err));
  69. }
  70.  
  71. async function deleteBook() {
  72. const idToDelete = this.id;
  73. httpHeaders = {
  74. method: 'DELETE',
  75. }
  76. await fetch(`${BASE_URL}${idToDelete}`, httpHeaders);
  77. loadAllBooks();
  78. }
  79.  
  80. function createElements(type, contentOrValue, parentNode, id, classes, attributes) {
  81. const htmlElement = document.createElement(type);
  82.  
  83. if (contentOrValue && type === 'input') {
  84. htmlElement.value = contentOrValue;
  85. }
  86. if (contentOrValue && type !== 'input') {
  87. htmlElement.textContent = contentOrValue;
  88. }
  89.  
  90. if (id) {
  91. htmlElement.id = id;
  92. }
  93. if (classes) {
  94. htmlElement.classList.add(...classes)
  95. }
  96. if (attributes) {
  97. for (const key in attributes) {
  98. htmlElement.setAttribute(key, attributes[key])
  99. }
  100. }
  101. if (parentNode) {
  102. parentNode.appendChild(htmlElement);
  103. }
  104. return htmlElement;
  105. }
  106.  
  107. }
  108.  
  109. attachEvents();
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement