Advertisement
GeorgiLukanov87

01. Forum posts - JS Advanced Retake Exam - 6 Apr 2022

Mar 22nd, 2023 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Forum posts
  2. // JS Advanced Retake Exam - 6 Apr 2022
  3. // https://judge.softuni.org/Contests/Practice/Index/3425#0
  4.  
  5. function solve() {
  6.   const inputFields = document.querySelectorAll('input');
  7.   const textArea = document.getElementById('post-content');
  8.   const publishBtn = document.getElementById('publish-btn');
  9.  
  10.   const reviewList = document.getElementById('review-list');
  11.   const publishedList = document.getElementById('published-list');
  12.   const clearBtn = document.getElementById('clear-btn');
  13.  
  14.   clearBtn.addEventListener('click', clearAllPublishedList);
  15.   let DataSaved = {};
  16.  
  17.   //When click on publish button.
  18.   publishBtn.addEventListener('click', publishPost);
  19.   function publishPost() {
  20.     let title = inputFields[0].value;
  21.     let category = inputFields[1].value;
  22.     let content = textArea.value;
  23.     //check is input is valid.
  24.     if (title == '' || category == '' || content == '') {
  25.       alert('Please fill all inputs')
  26.       return;
  27.     }
  28.     // Save and reNew data every time the button publish is clicked.
  29.     DataSaved['title'] = title;
  30.     DataSaved['category'] = category;
  31.     DataSaved['content'] = content;
  32.     clearFields();
  33.  
  34.     //Create elements by using custom create html elements function.
  35.     let li = createElement('li', '', '', '', 'rpost');
  36.     let article = createElement('article', '', li);
  37.     let h4 = createElement('h4', DataSaved['title'], article,);
  38.     let p1 = createElement('p', DataSaved['category'], article, 'Category');
  39.     let p2 = createElement('p', DataSaved['content'], article, 'Content');
  40.     let editBtn = createElement('button', 'Edit', li, '', 'action-btn', 'edit');
  41.     let approveBtn = createElement('button', 'Approve', li, '', 'action-btn', 'approve');
  42.     reviewList.appendChild(li);
  43.  
  44.     editBtn.addEventListener('click', editHandler);
  45.     approveBtn.addEventListener('click', approveHandler);
  46.   }
  47.  
  48.   // Clear fields!
  49.   function clearFields() {
  50.     inputFields[0].value = '';
  51.     inputFields[1].value = '';
  52.     textArea.value = '';
  53.   }
  54.  
  55.   //Edit -> extract information by current event.target and fill the input fields.
  56.   function editHandler(e) {
  57.     let item = e.currentTarget.parentNode.children[0]
  58.  
  59.     let title = item.children[0].textContent;
  60.     let category = item.children[1].textContent.split('Category: ')[1];
  61.     let content = item.children[2].textContent.split('Content: ')[1];
  62.  
  63.     inputFields[0].value = title;
  64.     inputFields[1].value = category;
  65.     textArea.value = content;
  66.  
  67.     e.target.parentNode.remove();
  68.   }
  69.  
  70.   //Remove buttons from the post and add it to the published list(last section).
  71.   function approveHandler(e) {
  72.     let item = e.currentTarget.parentNode;
  73.     Array.from(item.querySelectorAll('#review-list button'))
  74.       .forEach(btn => { btn.remove(); });
  75.     publishedList.appendChild(item);
  76.   }
  77.  
  78.   //Clear published list.
  79.   function clearAllPublishedList() {
  80.     publishedList.innerHTML = ''
  81.   }
  82.  
  83.   //Custom create html element.
  84.   function createElement(type, content, parent, moreInfo, class1, class2) {
  85.     let newEl = document.createElement(type);
  86.     if (moreInfo) {
  87.       newEl.textContent = `${moreInfo}: ${content}`;
  88.     } else {
  89.       newEl.textContent = `${content}`;
  90.     }
  91.  
  92.     if (class1) {
  93.       newEl.classList.add(class1)
  94.     }
  95.  
  96.     if (class2) {
  97.       newEl.classList.add(class2)
  98.     }
  99.  
  100.     if (parent) {
  101.       parent.appendChild(newEl);
  102.     }
  103.     return newEl;
  104.   }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement