Advertisement
GeorgiLukanov87

01. Mails Delivery - JS Advanced Exam - 13 March 2022

Mar 22nd, 2023 (edited)
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 01. Mails Delivery
  2. // JS Advanced Exam - 13 March 2022
  3. // https://judge.softuni.org/Contests/Practice/Index/3395#0
  4.  
  5. function solve() {
  6.     const recipientNameElement = document.getElementById('recipientName');
  7.     const titleElement = document.getElementById('title');
  8.     const messageElement = document.getElementById('message');
  9.  
  10.     document.getElementById('add').addEventListener('click', addMail);
  11.     document.getElementById('reset').addEventListener('click', resetFields);
  12.  
  13.     const listOfMails = document.getElementById('list');
  14.     const sentMails = document.querySelector('.sent-list');
  15.     const deletedMails = document.querySelector('.delete-list')
  16.  
  17.     function addMail(e) {
  18.         e.preventDefault();
  19.         if (recipientNameElement.value == '' || titleElement.value == '' || messageElement.value == '') {
  20.             alert('Fill all inputs')
  21.             return;
  22.         }
  23.         let li = document.createElement('li');
  24.         let div = document.createElement('div');
  25.         div.id = 'list-action';
  26.  
  27.         let sendBtn = document.createElement('button');
  28.         sendBtn.textContent = 'Send';
  29.         sendBtn.setAttribute('type', 'submit');
  30.         sendBtn.setAttribute('id', 'send');
  31.  
  32.         let deleteBtn = document.createElement('button');
  33.         deleteBtn.textContent = 'Delete';
  34.         deleteBtn.setAttribute('type', 'submit');
  35.         deleteBtn.setAttribute('id', 'delete');
  36.  
  37.         div.appendChild(sendBtn);
  38.         div.appendChild(deleteBtn);
  39.  
  40.         customHTMLelement('h4', titleElement.value, '', 'Title', li);
  41.         customHTMLelement('h4', recipientNameElement.value, '', 'Recipient Name', li);
  42.         customHTMLelement('span', messageElement.value, '', '', li);
  43.         li.appendChild(div);
  44.         listOfMails.appendChild(li);
  45.  
  46.         sendBtn.addEventListener('click', sendMail);
  47.         deleteBtn.addEventListener('click', deleteMail);
  48.         resetFields(e);
  49.     }
  50.  
  51.     function sendMail(e) {
  52.         let newLi = e.currentTarget.parentNode.parentNode;
  53.         let sentName = newLi.children[0].textContent;
  54.         let sentTitle = newLi.children[1].textContent.replace('Recipient Name', 'To');
  55.  
  56.         let liContainer = document.createElement('li');
  57.         customHTMLelement('span', sentTitle, '', '', liContainer);
  58.         customHTMLelement('span', sentName, '', '', liContainer);
  59.         let btnsDiv = document.createElement('div');
  60.         btnsDiv.classList.add('btn');
  61.         let delSentMailbtn = customHTMLelement('button', 'Delete');
  62.         delSentMailbtn.setAttribute('type', 'submit')
  63.         delSentMailbtn.classList.add('delete');
  64.         btnsDiv.appendChild(delSentMailbtn);
  65.         liContainer.appendChild(btnsDiv);
  66.  
  67.         sentMails.appendChild(liContainer);
  68.         newLi.remove();
  69.         delSentMailbtn.addEventListener('click', deleteMail)
  70.     }
  71.  
  72.     function deleteMail(e) {
  73.         let newLi = e.currentTarget.parentNode.parentNode;
  74.         let sentName = newLi.children[0].textContent;
  75.         let sentTitle = newLi.children[1].textContent.replace('Recipient Name', 'To');
  76.  
  77.         let deletedLi = document.createElement('li');
  78.         customHTMLelement('span', sentName, '', '', deletedLi)
  79.         customHTMLelement('span', sentTitle, '', '', deletedLi)
  80.         deletedMails.appendChild(deletedLi);
  81.         newLi.remove();
  82.     }
  83.  
  84.     function resetFields(e) {
  85.         e.preventDefault();
  86.         recipientNameElement.value = '';
  87.         titleElement.value = '';
  88.         messageElement.value = '';
  89.     }
  90.  
  91.     function customHTMLelement(type, content, id, moreInfo, parent) {
  92.         let newEl = document.createElement(type);
  93.         if (moreInfo) {
  94.             newEl.textContent = `${moreInfo}: ${content}`;
  95.         } else {
  96.             newEl.textContent = `${content}`;
  97.         }
  98.         if (id) {
  99.             newEl.id = id;
  100.         }
  101.         if (parent) {
  102.             parent.appendChild(newEl);
  103.         }
  104.         return newEl;
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement