Advertisement
Guest User

Untitled

a guest
Aug 17th, 2023
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     let fields = Array.from(document.querySelectorAll('input')).slice(0, 5)
  4.     let nextBtn = document.querySelector('#next-btn')
  5.     let infoSection = document.querySelector('.info-list')
  6.     let confirmSection = document.querySelector('.confirm-list')
  7.     let verification = document.getElementById('verification');
  8.  
  9.     function createEl(type, content, klass) {
  10.         const result = document.createElement(type);
  11.         if (content) {
  12.           result.textContent = content;
  13.         }  
  14.         if (klass) {
  15.           result.className = klass;
  16.         }  
  17.         return result;
  18.       }
  19.  
  20.       nextBtn.addEventListener("click", onClick);
  21.     function onClick(e) {
  22.       e.preventDefault();
  23.       let[firstName, lastName, dateIn, dateOut, count] = fields
  24.       if(!firstName.value || !lastName.value || !dateIn.value
  25.          || !dateOut.value || !count.value) return
  26.  
  27.       let li = infoSection.appendChild(createEl('li', undefined, 'reservation-content'))
  28.       let article = li.appendChild(createEl('article'))
  29.       let h3 = article.appendChild(createEl('h3', `Name: ${firstName.value} ${lastName.value}`))
  30.       let p1 = article.appendChild(createEl('p', `From date: ${dateIn.value}`))
  31.       let p2 = article.appendChild(createEl('p', `To date: ${dateOut.value}`))
  32.       let p3 = article.appendChild(createEl('p', `For ${count.value} people`))
  33.       let editBtn = li.appendChild(createEl('button', 'Edit', 'edit-btn'))
  34.       let continueBtn = li.appendChild(createEl('button', 'Continue', 'continue-btn'))
  35.      
  36.  
  37.      let editArray = []
  38.      fields.forEach(x => {editArray.push(x.value); x.value = ''})
  39.  
  40.      buttonsState(true)
  41.  
  42.       editBtn.addEventListener('click', onEdit)
  43.     function onEdit(){
  44.     // let[h3, p1, p2, p3] = infoSection.querySelectorAll('article')
  45.     // let[firstNameEdit, lastNameEdit, p1Edit, p2Edit, p3Edit] = fields
  46.  
  47.      //firstNameEdit.value = h3.textContent.split('Name: ')[1]]
  48.      //lastNameEdit.value = h3.textContent.split('Name: ')[2]
  49.      //p1Edit.value = p1.textContent.split('From date: ')[1]
  50.      //p2Edit.value = p2.textContent.split('To date: ')[1]
  51.      //p3Edit.value = p3.textContent.split('For: ')[1]
  52.      fields.forEach((x,i) => {x.value = editArray[i]})
  53.      buttonsState(false)
  54.      infoSection.textContent = ''
  55.    
  56.   }
  57.   function buttonsState(nextDisabled = true){
  58.     if(nextDisabled){
  59.         nextBtn.disabled = true
  60.        editBtn.disabled = false
  61.        continueBtn.disabled = false
  62.     } else {
  63.         nextBtn.disabled = false
  64.        editBtn.disabled = true
  65.        continueBtn.disabled = true
  66.     }
  67. }
  68. continueBtn.addEventListener('click', onContinue)
  69. function onContinue(){
  70.     let[firstName, lastName, dateIn, dateOut, count] = fields
  71.     let liTwo = confirmSection.appendChild(createEl('li', undefined, 'reservation-content'))
  72.       let articleTwo = liTwo.appendChild(article)
  73.       //let h3Two = articleTwo.appendChild(createEl('h3', `Name: ${firstName.value} ${lastName.value}`))
  74.       //let p1Two = articleTwo.appendChild(createEl('p', `From date: ${dateIn.value}`))
  75.       //let p2Two = articleTwo.appendChild(createEl('p', `To date: ${dateOut.value}`))
  76.       //let p3Two = articleTwo.appendChild(createEl('p', `For ${count.value} people`))
  77.       let confirmBtn = liTwo.appendChild(createEl('button', 'Confirm', 'confirm-btn'))
  78.       let cancelBtn = liTwo.appendChild(createEl('button', 'Cancel', 'cancel-btn'))
  79.    
  80.     li.remove();
  81.  
  82.     confirmBtn.addEventListener('click', onConfirm);
  83.     function onConfirm() {
  84.         liTwo.remove();
  85.        buttonsState(false)
  86.  
  87.         verification.setAttribute('class', 'reservation-confirmed');
  88.         verification.textContent = 'Confirmed.';
  89.     }
  90.  
  91.     cancelBtn.addEventListener('click',onCancel);
  92.     function onCancel() {
  93.  
  94.         liTwo.remove();
  95.         buttonsState(false)
  96.  
  97.         verification.setAttribute('class', 'reservation-cancelled');
  98.         verification.textContent = 'Cancelled.';
  99.  
  100.     }
  101.    }
  102.   }  
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement