Advertisement
Liliana797979

Service - js advanced - exam

Jan 28th, 2022
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener('load', solve);
  2.  
  3. function solve() {
  4.     const [ receivedSection, completedSection ] = Array.from(document.querySelectorAll('#wrapper section'));
  5.     const repairForm = document.querySelector('div#right form');
  6.     const sendFormBtn = document.querySelector('div#right form button');
  7.     const [ clearBtn ] = [...Array.from(completedSection.getElementsByClassName('clear-btn'))];
  8.     clearBtn.addEventListener('click', onClear);
  9.  
  10.     sendFormBtn.addEventListener('click', onSend);
  11.  
  12.     function onSend(e) {
  13.         e.preventDefault();
  14.  
  15.         let productType = document.getElementById('type-product');
  16.         let description = document.getElementById('description');
  17.         let clientName = document.getElementById('client-name');
  18.         let clientPhone = document.getElementById('client-phone');
  19.  
  20.         if(!productType.value.trim()
  21.             && (!productType.value.trim() == 'Computer' || !productType.value.trim() == 'Phone')
  22.             || !description.value.trim() || !clientName.value.trim() || !clientPhone.value.trim()) {
  23.                 return;
  24.         }
  25.  
  26.         const newElement = createReceivedOrderElement(productType.value.trim(), description.value.trim(), clientName.value.trim(), clientPhone.value.trim());
  27.         const [ startBtn, finishBtn] = [...Array.from(newElement.querySelectorAll('button'))];
  28.         startBtn.addEventListener('click', onStart.bind(null, startBtn, finishBtn, e));
  29.         finishBtn.addEventListener('click', onFinish.bind(null, newElement));
  30.         receivedSection.appendChild(newElement);
  31.         repairForm.reset();
  32.  
  33.         function createReceivedOrderElement(type, description, name, phone) {
  34.             const newDiv = document.createElement('div');
  35.             newDiv.className = 'container';
  36.             newDiv.innerHTML = `<h2>Product type for repair: ${type}</h2>
  37.             <h3>Client information: ${name}, ${phone}</h3>
  38.             <h4>Description of the problem: ${description}</h4>
  39.             <button class="start-btn">Start repair</button>
  40.             <button class="finish-btn" disabled>Finish repair</button>`
  41.  
  42.             return newDiv;
  43.         }
  44.     }
  45.  
  46.     function onStart(startElement, endElement,e) {
  47.         startElement.disabled = true;
  48.         endElement.disabled = false;
  49.     }
  50.  
  51.     function onFinish(completedElement, e) {
  52.         completedElement.remove();
  53.         const [ startBtn, finishBtn] = [...Array.from(completedElement.querySelectorAll('button'))];
  54.         startBtn.remove();
  55.         finishBtn.remove();
  56.  
  57.         completedSection.appendChild(completedElement);
  58.     }
  59.  
  60.     function onClear(e) {
  61.         const containerDivs = Array.from(completedSection.querySelectorAll('div.container'));
  62.         if(containerDivs.length < 1) {
  63.             return;
  64.         } else {
  65.             containerDivs.forEach(containerElement => containerElement.remove());
  66.         }
  67.     }
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement