Advertisement
VeselaVideva

Task Manager 75/100

Feb 19th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const [addTask, open, inProgress, complete] = document.querySelectorAll('section');
  3.     const task = document.getElementById('task');
  4.     const description = document.getElementById('description');
  5.     const date = document.getElementById('date');
  6.  
  7.     const addBtn = document.getElementById('add');
  8.     addBtn.addEventListener('click', add);
  9.  
  10.     function add(e) {
  11.         e.preventDefault();
  12.  
  13.         if (task.value === '' || date.value === '' || isNaN(Date.parse(date.value))) { // check if all fields are valid
  14.             return;
  15.         }
  16.  
  17.         let article = factory('article', {}, '');
  18.         open.children[1].appendChild(article); // open.children[1] is the second div in section Open
  19.  
  20.         let setTitle = factory('h3', {}, `${task.value}`);
  21.         article.appendChild(setTitle);
  22.  
  23.         let setDescription = factory('p', {}, `Description: ${description.value}`);
  24.         article.appendChild(setDescription);
  25.  
  26.         let setDate = factory('p', {}, `Due Date: ${date.value}`);
  27.         article.appendChild(setDate);
  28.  
  29.         let btnsDiv = factory('div', { class: 'flex' }, '');
  30.         article.appendChild(btnsDiv);
  31.  
  32.         let btnStart = factory('button', { class: 'green' }, 'Start');
  33.         btnsDiv.appendChild(btnStart);
  34.         btnStart.addEventListener('click', start);
  35.  
  36.         let btnDelete = factory('button', { class: 'red' }, 'Delete');
  37.         btnsDiv.appendChild(btnDelete);
  38.         btnDelete.addEventListener('click', deleteTask);
  39.  
  40.         task.value = '';
  41.         description.value = '';
  42.         date.value = '';
  43.     }
  44.  
  45.     function deleteTask(e) {
  46.         e.target.parentNode.parentNode.parentNode.children[0].remove();
  47.     }
  48.  
  49.     function start(e) {
  50.         const currentArticle = e.target.parentNode.parentNode.parentNode.children[0];
  51.         inProgress.children[1].appendChild(currentArticle); // inProgress.children[1] is the second div in section inProgress
  52.  
  53.         let btnStr = currentArticle.children[3].children[0];
  54.         let btnDel = currentArticle.children[3].children[1];
  55.         btnStr.remove();
  56.         btnDel.remove();
  57.  
  58.         const btnDelNew = factory('button', { class: 'red' }, 'Delete');
  59.         currentArticle.children[3].appendChild(btnDelNew);
  60.         btnDelNew.addEventListener('click', deleteTask);
  61.  
  62.         const btnFinish = factory('button', { class: 'orange' }, 'Finish');
  63.         currentArticle.children[3].appendChild(btnFinish);
  64.         btnFinish.addEventListener('click', finish);
  65.     }
  66.  
  67.     function finish(e) {
  68.         const currentArticle = e.target.parentNode.parentNode.parentNode.children[0];
  69.         complete.children[1].appendChild(currentArticle); // complete.children[1] is the second div in section Complete
  70.  
  71.         currentArticle.children[3].remove();
  72.     }
  73.  
  74.     // function for creating DOM elements with attributes
  75.     function factory(type, attributes, text) {
  76.         const el = document.createElement(type);
  77.         for (let key in attributes) {
  78.             el.setAttribute(key, attributes[key]);
  79.         }
  80.         if (text) {
  81.             el.textContent = text;
  82.         }
  83.         return el;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement