Advertisement
GalinaKG

Untitled

Mar 30th, 2023
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', groceryList);
  2.  
  3. function groceryList() {
  4.     const BASE_URL = 'http://localhost:3030/jsonstore/grocery/'
  5.     const loadButton = document.getElementById('load-product');
  6.     const updateButton = document.getElementById('update-product');
  7.     const addButton = document.getElementById('add-product');
  8.     const tBody = document.getElementById('tbody');
  9.     const productName = document.getElementById('product');
  10.     const productCount = document.getElementById('count');
  11.     const productPrice = document.getElementById('price');
  12.     let id = null;
  13.     loadButton.addEventListener('click', loadAllProducts);
  14.     addButton.addEventListener('click', addProduct);
  15.  
  16.     function loadAllProducts(event) {
  17.         event?.preventDefault();
  18.         fetch(BASE_URL)
  19.         .then((response) => response.json())
  20.         .then((data) => {
  21.             tBody.textContent = '';
  22.             for (key in data) {
  23.                 const id = data[key]._id;
  24.                 let tr = document.createElement('tr');
  25.                 let tdName = document.createElement('td');
  26.                 let tdCount = document.createElement('td');
  27.                 let tdPrice = document.createElement('td');
  28.                 let tdAction = document.createElement('td');
  29.                 let updateButton = document.createElement('button');
  30.                 let deleteButton = document.createElement('button');
  31.                 tdName.className = 'name';
  32.                 tdCount.className = 'count-product';
  33.                 tdPrice.className = 'product-price';
  34.                 tdAction.className = 'btn';
  35.                 updateButton.className = 'update-btn';
  36.                 deleteButton.className = 'delete';
  37.                 updateButton.id = id;
  38.                 deleteButton.id = id;
  39.                 updateButton.textContent = 'Update';
  40.                 deleteButton.textContent = 'Delete';
  41.                 updateButton.addEventListener('click', updateProduct);
  42.                 deleteButton.addEventListener('click', deleteProduct);
  43.                 tdName.textContent = data[key].product;
  44.                 tdCount.textContent = data[key].count;
  45.                 tdPrice.textContent = data[key].price;
  46.                 tdAction.append(updateButton, deleteButton);
  47.                 tr.append(tdName, tdCount, tdPrice, tdAction);
  48.                 tBody.appendChild(tr);
  49.             }
  50.         })
  51.         .catch((err) => {console.error(err)})
  52.     }
  53.  
  54.     function addProduct(event) {
  55.         event?.preventDefault();
  56.         const httpHeaders = {
  57.             method: 'POST',
  58.             body: JSON.stringify({
  59.                 product: productName.value,
  60.                 count: productCount.value,
  61.                 price: productPrice.value
  62.             })
  63.         }
  64.  
  65.         fetch(BASE_URL, httpHeaders)
  66.         .then(() => loadAllProducts())
  67.         .catch((err) => {console.error(err)})
  68.         productName.value = '';
  69.         productCount.value = '';
  70.         productPrice.value = '';
  71.     }
  72.  
  73.     function updateProduct(event) {
  74.         // updateButton.id = event.currentTarget.id;
  75.         id = event.currentTarget.id;
  76.         const currentTableRow = event.currentTarget.parentNode.parentNode;
  77.        
  78.         productName.value = currentTableRow.querySelector('.name').textContent;
  79.         productCount.value = currentTableRow.querySelector('.count-product').textContent;
  80.         productPrice.value = currentTableRow.querySelector('.product-price').textContent;  
  81.    
  82.         addButton.disabled = true;
  83.         updateButton.disabled = false;
  84.         updateButton.addEventListener('click', updateProductInfo);    
  85.     }
  86.  
  87.     function updateProductInfo(event) {
  88.         // id = event.currentTarget.id;
  89.         const httpHeaders = {
  90.             method: 'PATCH',
  91.             body: JSON.stringify({
  92.                 product: productName.value,
  93.                 count: productCount.value,
  94.                 price: productPrice.value
  95.             })
  96.         }
  97.  
  98.         fetch(`${BASE_URL}${id}`, httpHeaders)
  99.         // .then(() => loadAllProducts())
  100.         // .catch((err) => {console.error(err)})
  101.         loadAllProducts();
  102.         productName.value = '';
  103.         productCount.value = '';
  104.         productPrice.value = '';
  105.         addButton.disabled = false;
  106.         updateButton.disabled = true;
  107.     }
  108.  
  109.     function deleteProduct(event) {
  110.         const id = event.currentTarget.id;
  111.         const httpHeaders = {
  112.             method: 'DELETE'
  113.         }
  114.  
  115.         fetch(`${BASE_URL}${id}`, httpHeaders)
  116.         .then(() => loadAllProducts())
  117.         .catch((err) => {console.error(err)})
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement