eternalmeg

Untitled

Apr 2nd, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. function attachEvents() {
  2. const BASE_URL = 'http://localhost:3030/jsonstore/grocery/';
  3.  
  4. const inputDOMSelectors = {
  5. product: document.getElementById('product'),
  6. count: document.getElementById('count'),
  7. price: document.getElementById('price')
  8. }
  9.  
  10. const otherDOMSelectors = {
  11. addProductsBtn: document.getElementById('add-product'),
  12. updateProductBtn: document.getElementById('update-product'),
  13. loadProductsBtn: document.getElementById('load-product'),
  14. productsContainer: document.getElementById('tbody')
  15. }
  16. let currentProducts = [];
  17. let productToEdit = {};
  18.  
  19.  
  20. otherDOMSelectors.loadProductsBtn.addEventListener('click', loadProductsHandler);
  21. otherDOMSelectors.updateProductBtn.addEventListener('click',updateProductHandler);
  22.  
  23. function loadProductsHandler(event) {
  24. if (event) {
  25. event.preventDefault();
  26. }
  27. fetch(BASE_URL)
  28. .then((response) => response.json())
  29. .then((allProductsResponse) => {
  30. currentProducts = Object.values(allProductsResponse);
  31. console.log(currentProducts);
  32. for (const {product, count, price, _id} of currentProducts) {
  33. const tableRow = createElement('tr', '', otherDOMSelectors.productsContainer);
  34. tableRow.id = _id;
  35. createElement('td', product, tableRow, '', ['name']);
  36. createElement('td', count, tableRow, '', ['count-product']);
  37. createElement('td', price, tableRow, '', ['product-price']);
  38. const productOptionButtons = createElement('td', '', tableRow, '', ['btn']);
  39. const updateBtn = createElement('button', 'Update', productOptionButtons, '', ['update']);
  40. const deleteBtn = createElement('button', 'Delete', productOptionButtons, '', ['delete']);
  41. updateBtn.addEventListener('click', loadUpProductToUpdate);
  42.  
  43. }
  44. })
  45. }
  46. function loadUpProductToUpdate () {
  47. const id = this.parentNode.parentNode.id;
  48. console.log(id);
  49. productToEdit = currentProducts.find((p)=> p._id ===id);
  50. for (const key in inputDOMSelectors) {
  51. inputDOMSelectors[key].value = productToEdit[key];
  52. console.log( inputDOMSelectors[key].value);
  53.  
  54. }
  55.  
  56.  
  57. }
  58.  
  59. function updateProductHandler (event){
  60.  
  61. }
  62.  
  63. }
  64.  
  65. attachEvents()
  66.  
  67.  
  68. function createElement(type, content, parentNode, id, classes, attributes, useInnerHtml) {
  69. const htmlElement = document.createElement(type);
  70.  
  71. if (content && useInnerHtml) {
  72. htmlElement.innerHTML = content;
  73. } else {
  74. if (content && type !== 'input') {
  75. htmlElement.textContent = content;
  76. }
  77. if (content && type === 'input') {
  78. htmlElement.value = content;
  79. }
  80. }
  81. if (id) {
  82. htmlElement.id = id;
  83. }
  84. if (classes && classes.length > 0) {
  85. htmlElement.classList.add(...classes);
  86. }
  87. if (parentNode) {
  88. parentNode.appendChild(htmlElement);
  89. }
  90. if (attributes) {
  91. for (const key in attributes) {
  92. htmlElement.setAttribute(key, attributes[key]);
  93. }
  94. }
  95. return htmlElement;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment