Advertisement
Iv555

Untitled

Apr 24th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1.  
  2. const BASE_URL = 'http://localhost:3030/jsonstore/tasks/';
  3.  
  4. const inputSelectors = {
  5. titleInput: document.getElementById('course-name'),
  6. typeInput: document.getElementById('course-type'),
  7. descriptionInput: document.getElementById('description'),
  8. teacherNameInput: document.getElementById('teacher-name'),
  9. }
  10.  
  11. const otherSelectors = {
  12. addCourseBtn: document.getElementById('add-course'),
  13. loadCoursesBtn: document.getElementById('load-course'),
  14. editCourseBtn: document.getElementById('edit-course'),
  15. listDiv: document.getElementById('list'),
  16. form: document.querySelector('#form form')
  17. }
  18.  
  19. let courses = [];
  20. let editCourseId = null;
  21. otherSelectors.loadCoursesBtn.addEventListener('click', loadAllCourses);
  22. otherSelectors.addCourseBtn.addEventListener('click', addCourse);
  23. otherSelectors.editCourseBtn.addEventListener('click', editCourse)
  24.  
  25. function loadAllCourses(e) {
  26. if (e) {
  27. e.preventDefault();
  28. }
  29. fetch(BASE_URL)
  30. .then((res) => res.json())
  31. .then((data) => {
  32. otherSelectors.listDiv.innerHTML = '';
  33. courses = Object.values(data);
  34. for (const { description, teacher, title, type, _id } of Object.values(data)) {
  35. const containerDiv = createElements('div', '', false, otherSelectors.listDiv, _id, ['container']);
  36. const titleH2 = createElements('h2', title, false, containerDiv);
  37. const teacherH3 = createElements('h3', teacher, false, containerDiv);
  38. const typeH3 = createElements('h3', type, false, containerDiv);
  39. const descriptionH4 = createElements('h4', description, false, containerDiv);
  40. const editBtn = createElements('button', 'Edit Course', false, containerDiv, '', ['edit-btn']);
  41. const finishBtn = createElements('button', 'Finish Course', false, containerDiv, '', ['finish-btn']);
  42.  
  43. editBtn.addEventListener('click', loadCourseForEdit);
  44. finishBtn.addEventListener('click', deleteCourse);
  45. }
  46. })
  47. .catch((err) => console.error(err))
  48. }
  49. function addCourse(e) {
  50. if (e) {
  51. e.preventDefault();
  52. }
  53.  
  54. const title = inputSelectors.titleInput.value;
  55. const type = inputSelectors.typeInput.value;
  56. const description = inputSelectors.descriptionInput.value;
  57. const teacher = inputSelectors.teacherNameInput.value;
  58. if (title && type && description && teacher) {
  59. const httpHeaders = {
  60. method: 'POST',
  61. body: JSON.stringify({
  62. title: title,
  63. type: type,
  64. description: description,
  65. teacher: teacher
  66. })
  67. }
  68. fetch(BASE_URL, httpHeaders)
  69. .then(() => loadAllCourses());
  70. otherSelectors.form.reset();
  71. }
  72. }
  73.  
  74. function loadCourseForEdit(e) {
  75. if (e) {
  76. e.preventDefault();
  77. }
  78.  
  79. const id = e.currentTarget.parentNode.id;
  80. editCourseId = id;
  81. const course = courses.find(x => x._id === id);
  82. inputSelectors.titleInput.value = course.title;
  83. inputSelectors.typeInput.value = course.type;
  84. inputSelectors.descriptionInput.value = course.description;
  85. inputSelectors.teacherNameInput.value = course.teacher;
  86. e.currentTarget.parentNode.remove();
  87. otherSelectors.editCourseBtn.disabled = false;
  88. otherSelectors.addCourseBtn.disabled = true;
  89. }
  90.  
  91. function editCourse(e) {
  92. if (e) {
  93. e.preventDefault();
  94. }
  95.  
  96. const httpHeaders = {
  97. method: 'PUT',
  98. body: JSON.stringify({
  99. title: inputSelectors.titleInput.value,
  100. type: inputSelectors.typeInput.value,
  101. description: inputSelectors.descriptionInput.value,
  102. teacher: inputSelectors.teacherNameInput.value,
  103. _id: editCourseId
  104. })
  105. }
  106. fetch(`${BASE_URL}${editCourseId}`, httpHeaders)
  107. .then(() => loadAllCourses());
  108. otherSelectors.editCourseBtn.disabled = true;
  109. otherSelectors.addCourseBtn.disabled = false;
  110. otherSelectors.form.reset();
  111. }
  112.  
  113. function deleteCourse(e) {
  114. if (e) {
  115. e.preventDefault();
  116. }
  117. const httpHeaders = {
  118. method: 'DELETE'
  119. }
  120. const id = e.currentTarget.parentNode.id;
  121. fetch(`${BASE_URL}${id}`, httpHeaders)
  122. .then(() => loadAllCourses())
  123. }
  124.  
  125. function createElements(type, contentOrValue, useInnerHTML, parentNode, id, classes, attributes) {
  126. const htmlElement = document.createElement(type);
  127. if (contentOrValue && useInnerHTML) {
  128. htmlElement.innerHTML = contentOrValue;
  129. }
  130. else {
  131. if (contentOrValue && type === 'input') {
  132. htmlElement.value = contentOrValue;
  133. }
  134. if (contentOrValue && type !== 'input') {
  135. htmlElement.textContent = contentOrValue;
  136. }
  137. }
  138.  
  139. if (id) {
  140. htmlElement.id = id;
  141. }
  142. if (classes) {
  143. htmlElement.classList.add(...classes)
  144. }
  145. // {src: 'link', href : 'http'}
  146. if (attributes) {
  147. for (const key in attributes) {
  148. htmlElement.setAttribute(key, attributes[key])
  149. }
  150. }
  151. if (parentNode) {
  152. parentNode.appendChild(htmlElement);
  153. }
  154. return htmlElement;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement