Advertisement
Iv555

Untitled

Apr 10th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1.  
  2. function attachEvents() {
  3. const BASE_URL = 'http://localhost:3030/jsonstore/tasks/';
  4. const loadBoardBtn = document.getElementById('load-board-btn');
  5. const titleInput = document.getElementById('title');
  6. const descriptionInput = document.getElementById('description');
  7. const addTaskBtn = document.getElementById('create-task-btn');
  8.  
  9. const moveToButtons = {
  10. 'ToDo': 'Move to In Progress',
  11. 'In Progress': 'Move to Code Review',
  12. 'Code Review': 'Move to Done',
  13. 'Done': 'Close'
  14. }
  15. const ulSectionsPerStatus = {
  16. 'ToDo': document.querySelector('#todo-section .task-list'),
  17. 'In Progress': document.querySelector('#in-progress-section .task-list'),
  18. 'Code Review': document.querySelector('#code-review-section .task-list'),
  19. 'Done': document.querySelector('#done-section .task-list')
  20. }
  21.  
  22. loadBoardBtn, addEventListener('click', loadAllTasks);
  23. function loadAllTasks() {
  24. fetch(BASE_URL)
  25. .then((res) => res.json())
  26. .then((data) => {
  27. const allData = Object.values(data);
  28. for (const key in ulSectionsPerStatus) {
  29. ulSectionsPerStatus[key].innerHTML = '';
  30. }
  31.  
  32. for (const item of allData) {
  33. const newLi = createElements('li', '', false, ulSectionsPerStatus[item.status], item._id, ['task']);
  34. const titleH3 = createElements('h3', item.title, false, newLi);
  35. const descriptionPara = createElements('p', item.description, false, newLi);
  36. const btnMoveTo = createElements('button', `${moveToButtons[item.status]}`, false, newLi);
  37.  
  38. btnMoveTo.addEventListener('click', moveToNextSection)
  39. }
  40. })
  41. }
  42.  
  43. function moveToNextSection(e) {
  44. if (e) {
  45. e.preventDefault();
  46. }
  47. const btn = e.currentTarget;
  48. const nextSection = btn.textContent.slice(8);
  49. const currentLi = btn.parentNode;
  50. if (btn.textContent !== 'Close') {
  51. ulSectionsPerStatus[nextSection].appendChild(currentLi);
  52. httpHeaders = {
  53. method: 'PATCH',
  54. body: JSON.stringify({
  55. status: nextSection,
  56. })
  57. }
  58. fetch(`${BASE_URL}${currentLi.id}`, httpHeaders)
  59. .then(() => loadAllTasks());
  60.  
  61. }
  62. else {
  63. httpHeaders = {
  64. method: 'DELETE',
  65. }
  66. fetch(`${BASE_URL}${currentLi.id}`, httpHeaders)
  67. .then(() => loadAllTasks());
  68. }
  69.  
  70. }
  71.  
  72. addTaskBtn.addEventListener('click', (e) => {
  73. if (e) {
  74. e.preventDefault();
  75. }
  76. const newTitle = titleInput.value;
  77. const newDescription = descriptionInput.value;
  78. if (newTitle && newDescription) {
  79. httpHeaders = {
  80. method: 'POST',
  81. body: JSON.stringify({
  82. title: newTitle,
  83. description: newDescription,
  84. status: 'ToDo'
  85. })
  86. }
  87.  
  88. fetch(BASE_URL, httpHeaders)
  89. .then(() => loadAllTasks());
  90.  
  91. titleInput.value = '';
  92. descriptionInput.value = '';
  93.  
  94. }
  95.  
  96. })
  97.  
  98. function createElements(type, contentOrValue, useInnerHTML, parentNode, id, classes, attributes) {
  99. const htmlElement = document.createElement(type);
  100. if (contentOrValue && useInnerHTML) {
  101. htmlElement.innerHTML = contentOrValue;
  102. }
  103. else {
  104. if (contentOrValue && type === 'input') {
  105. htmlElement.value = contentOrValue;
  106. }
  107. if (contentOrValue && type !== 'input') {
  108. htmlElement.textContent = contentOrValue;
  109. }
  110. }
  111.  
  112. if (id) {
  113. htmlElement.id = id;
  114. }
  115. if (classes) {
  116. htmlElement.classList.add(...classes)
  117. }
  118. // {src: 'link', href : 'http'}
  119. if (attributes) {
  120. for (const key in attributes) {
  121. htmlElement.setAttribute(key, attributes[key])
  122. }
  123. }
  124. if (parentNode) {
  125. parentNode.appendChild(htmlElement);
  126. }
  127. return htmlElement;
  128. }
  129.  
  130. }
  131.  
  132. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement