Advertisement
GeorgiLukanov87

02. Sprint Planning - exam using id

Apr 8th, 2023 (edited)
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 02. Sprint Planning
  2. // Solutin 2 Using ID
  3.  
  4. function solve() {
  5.     const totalPointsElement = document.getElementById('total-sprint-points');
  6.  
  7.     const taskIdHiddenElement = document.getElementById('task-id');
  8.     const titleElement = document.getElementById('title');
  9.     const descriptionElement = document.getElementById('description');
  10.     const labelElement = document.getElementById('label');
  11.     const pointsElement = document.getElementById('points');
  12.     const assigneeElement = document.getElementById('assignee');
  13.  
  14.     const taskBtn = document.getElementById('create-task-btn');
  15.     const deleteBtn = document.getElementById('delete-task-btn');
  16.     const taskSectionElement = document.getElementById('tasks-section');
  17.     taskBtn.addEventListener('click', taskHandler);
  18.  
  19.     let lastTaskId = '';
  20.     let data = {};
  21.     let idCounter = 0;
  22.     let pointsCounter = 0;
  23.  
  24.     let iconsMapper = {
  25.         feature: '&#8865',
  26.         low: '&#9737',
  27.         high: '&#9888',
  28.     }
  29.  
  30.     let additionClassMapper = {
  31.         feature: "feature",
  32.         low: "low-priority",
  33.         high: "high-priority",
  34.     }
  35.  
  36.     function taskHandler(e) {
  37.         e.preventDefault();
  38.         console.log(e.target)
  39.         data = {
  40.             title: titleElement.value,
  41.             description: descriptionElement.value,
  42.             label: labelElement.value,
  43.             points: pointsElement.value,
  44.             assignee: assigneeElement.value,
  45.         }
  46.         if (!data.title || !data.description || !data.label || !data.points || !data.assignee) {
  47.             alert('wrong data');
  48.             return
  49.         }
  50.  
  51.         idCounter++;
  52.         let article = document.createElement('article');
  53.         article.id = `task-${idCounter}`;
  54.         taskIdHiddenElement.value = article.id;
  55.         article.classList.add('task-card');
  56.  
  57.         let iconToAdd = '';
  58.         let classToAdd = '';
  59.  
  60.         if (data.label === 'Feature') {
  61.             iconToAdd += iconsMapper['feature']
  62.             classToAdd += additionClassMapper['feature']
  63.  
  64.         } else if (data.label === 'Low Priority Bug') {
  65.             iconToAdd += iconsMapper['low']
  66.             classToAdd += additionClassMapper['low']
  67.  
  68.         } else if (data.label === 'High Priority Bug') {
  69.             iconToAdd += iconsMapper['high']
  70.             classToAdd += additionClassMapper['high']
  71.         }
  72.  
  73.         let label = cunstomHtml('div', 'task-card-label', data.label, article);
  74.         label.innerHTML += " " + iconToAdd;
  75.         label.classList.add(classToAdd)
  76.  
  77.         label.classList.add('feature')
  78.         let title = cunstomHtml('h3', 'task-card-title', data.title, article);
  79.         let description = cunstomHtml('p', 'task-card-description', data.description, article);
  80.         let points = cunstomHtml('div', 'task-card-points', `Estimated at ${data.points} pts`, article);
  81.         let assignee = cunstomHtml('div', 'task-card-assignee', `Assigned to: ${data.assignee}`, article);
  82.         let divBtnContainer = cunstomHtml('div', 'task-card-actions', '', article)
  83.  
  84.         let delBtn = document.createElement('button');
  85.         delBtn.textContent = 'Delete';
  86.         delBtn.addEventListener('click', deleteHandler);
  87.         divBtnContainer.appendChild(delBtn);
  88.         taskSectionElement.appendChild(article)
  89.  
  90.         clearInputs();
  91.         pointsCounter += Number(data.points);
  92.         totalPointsElement.textContent = `Total Points ${pointsCounter}pts`
  93.     }
  94.     function deleteHandler(e) {
  95.         let task = e.target.parentNode.parentNode;
  96.         let label = task.children[0].textContent;
  97.  
  98.         if (label.includes('Feature')) {
  99.             labelElement.value = 'Feature'
  100.         } else if (label.includes('Low')) {
  101.             labelElement.value = "Low Priority Bug";
  102.         } else if (label.includes('High')) {
  103.             labelElement.value = "High Priority Bug";
  104.         }
  105.  
  106.         let title = task.children[1].textContent;
  107.         let description = task.children[2].textContent;
  108.         let points = task.children[3].textContent.split(' ')[2];
  109.         let assignee = task.children[4].textContent.split(': ')[1];
  110.  
  111.         titleElement.value = title;
  112.         descriptionElement.value = description;
  113.         pointsElement.value = points;
  114.         assigneeElement.value = assignee;
  115.  
  116.         taskBtn.disabled = true;
  117.         deleteBtn.disabled = false;
  118.  
  119.         labelElement.disabled = true;
  120.         titleElement.disabled = true;
  121.         descriptionElement.disabled = true;
  122.         pointsElement.disabled = true;
  123.         assigneeElement.disabled = true;
  124.         lastTaskId = task.id;
  125.     }
  126.  
  127.     deleteBtn.addEventListener('click', function deleteTask() {
  128.         document.getElementById(lastTaskId).remove();
  129.         taskBtn.disabled = false;
  130.         deleteBtn.disabled = true;
  131.  
  132.         labelElement.disabled = false;
  133.         titleElement.disabled = false;
  134.         descriptionElement.disabled = false;
  135.         pointsElement.disabled = false;
  136.         assigneeElement.disabled = false;
  137.  
  138.         pointsCounter -= Number(pointsElement.value);
  139.         totalPointsElement.textContent = `Total Points ${pointsCounter}pts`
  140.         clearInputs();
  141.     });
  142.  
  143.     function cunstomHtml(type, className, text, parent) {
  144.         let newElement = document.createElement(type);
  145.         newElement.classList.add(className);
  146.         newElement.textContent = text;
  147.         if (parent) {
  148.             parent.appendChild(newElement);
  149.         }
  150.         return newElement
  151.     }
  152.  
  153.     function clearInputs() {
  154.         titleElement.value = '';
  155.         descriptionElement.value = '';
  156.         labelElement.value = '';
  157.         pointsElement.value = '';
  158.         assigneeElement.value = '';
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement