georgiev955

01. Dish Manager

Oct 13th, 2023 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const firstNameEl = document.querySelector('#first-name');
  3.   const lastNameEl = document.querySelector('#last-name');
  4.   const ageEl = document.querySelector('#age');
  5.   const genderEl = document.querySelector('#genderSelect');
  6.   const dishInfoEl = document.querySelector('#task');
  7.   const inProgressUl = document.querySelector('#in-progress');
  8.   const counterEl = document.querySelector('#progress-count');
  9.   const finishedEl = document.querySelector('#finished')
  10.   const clearBtn = document.querySelector('#clear-btn');
  11.   const submitBtn = document.querySelector('#form-btn');
  12.  
  13.   submitBtn.addEventListener('click', submitData);
  14.  
  15.   function submitData(e) {
  16.     e.preventDefault();
  17.  
  18.     const firstName = firstNameEl.value;
  19.     const lastName = lastNameEl.value;
  20.     const age = ageEl.value;
  21.     const gender = genderEl.value;
  22.     const dishInfo = dishInfoEl.value;
  23.  
  24.     if (!firstName || !lastName || !age || !dishInfo) {
  25.       return;
  26.     }
  27.  
  28.     clearInputFields();
  29.     increaseCounter();
  30.     createListElement(firstName, lastName, age, gender, dishInfo);
  31.  
  32.     const editBtns = Array.from(document.querySelectorAll('.edit-btn'));
  33.     editBtns.forEach(button => button.addEventListener('click', editInfo));
  34.  
  35.     const completeBtns = Array.from(document.querySelectorAll('.complete-btn'));
  36.     completeBtns.forEach(button => button.addEventListener('click', completeDish));
  37.  
  38.     clearBtn.addEventListener('click', clearFinishedList);
  39.   }
  40.  
  41.   function increaseCounter() {
  42.     const currentValue = Number(counterEl.textContent);
  43.     counterEl.textContent = currentValue + 1;
  44.   }
  45.  
  46.   function clearInputFields() {
  47.     firstNameEl.value = '';
  48.     lastNameEl.value = '';
  49.     ageEl.value = '';
  50.     genderEl.value = '';
  51.     dishInfoEl.value = '';
  52.   }
  53.   function createListElement(firstName, lastName, age, gender, dishInfo){
  54.     const listElement = document.createElement('li');
  55.     listElement.classList.add('each-line');
  56.  
  57.     listElement.innerHTML = `<article>
  58.       <h4>${firstName} ${lastName}</h4>
  59.       <p>${gender}, ${age}</p>
  60.       <p>Dish description: ${dishInfo}</p>
  61.       </article>
  62.       <button class="edit-btn">Edit</button>
  63.       <button class="complete-btn">Mark as complete</button>`
  64.    
  65.     inProgressUl.appendChild(listElement);
  66.   }
  67.  
  68.   function editInfo(e) {
  69.     const listElementRef = e.target.parentElement;
  70.     const articleRef = e.target.parentElement.children[0].children;
  71.     const articleArr = Array.from(articleRef);
  72.  
  73.     firstNameEl.value = articleArr[0].textContent.split(' ')[0];
  74.     lastNameEl.value = articleArr[0].textContent.split(' ')[1];
  75.     ageEl.value = articleArr[1].textContent.split(', ')[1];
  76.     genderEl.value = articleArr[1].textContent.split(', ')[0];
  77.     dishInfoEl.value = articleArr[2].textContent.split(': ')[1];
  78.  
  79.     listElementRef.remove();
  80.  
  81.     reduceCounter();
  82.   }
  83.  
  84.   function reduceCounter() {
  85.     const currentValue = Number(counterEl.textContent);
  86.     counterEl.textContent = currentValue - 1;
  87.   }
  88.  
  89.   function completeDish(e) {
  90.     const listElementRef = e.target.parentElement;
  91.     const leftButtonRef = listElementRef.children[1];
  92.     const rightButtonRef = listElementRef.children[2];
  93.     leftButtonRef.remove();
  94.     rightButtonRef.remove();
  95.  
  96.     finishedEl.appendChild(listElementRef);
  97.     reduceCounter();
  98.   }
  99.  
  100.   function clearFinishedList() {
  101.     let finishedList = Array.from(finishedEl.children);
  102.     for (const child of finishedList) {
  103.       child.remove();
  104.     }
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment