Advertisement
Iv555

Untitled

Apr 20th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.20 KB | None | 0 0
  1.  
  2. const BASE_URL = 'http://localhost:3030/jsonstore/tasks/';
  3. const loadCoursesBtn = document.getElementById('load-course');
  4. const listDiv = document.getElementById('list');
  5. const addCourseBtn = document.getElementById('add-course');
  6. const form = document.querySelector('#form form');
  7. const editCourseBtn = document.getElementById('edit-course');
  8. // let array = [];
  9. // let obj = {};
  10. let editCourseId = null;
  11. const inputSelectors = {
  12. titleInput: document.getElementById('course-name'),
  13. typeInput: document.getElementById('course-type'),
  14. descriptionInput: document.getElementById('description'),
  15. teacherInput: document.getElementById('teacher-name')
  16. }
  17.  
  18. loadCoursesBtn.addEventListener('click', loadAllCourses);
  19. addCourseBtn.addEventListener('click', addCourse);
  20. editCourseBtn.addEventListener('click', editCourse);
  21.  
  22.  
  23. function loadAllCourses(e) {
  24. if (e) {
  25. e.preventDefault();
  26. }
  27. listDiv.innerHTML = '';
  28.  
  29. fetch(BASE_URL)
  30. .then((res) => res.json())
  31. .then((data) => {
  32. // inputsObj = Object.values(data);
  33. // console.log(inputsObj);
  34. for (const { title, type, description, teacher, _id } of Object.values(data)) {
  35.  
  36. const newDiv = createElements('div', '', false, listDiv, _id, ['container']);
  37. const titleH2 = createElements('h2', title, false, listDiv);
  38. const teacherH3 = createElements('h3', teacher, false, listDiv);
  39. const courseType = createElements('h3', type, false, listDiv);
  40. const descriptionP = createElements('p', description, false, listDiv);
  41. const editInnerBtn = createElements('button', 'Edit Course', false, listDiv, '', ['edit-btn']);
  42.  
  43. const finishBtn = createElements('button', 'Finish Course', false, listDiv, '', ['finish-btn']);
  44. finishBtn.addEventListener('click', finishCourse);
  45.  
  46. editInnerBtn.addEventListener('click', getForEditCourse)
  47. }
  48.  
  49. })
  50. .catch((err) => {
  51. console.error(err);
  52. })
  53. }
  54.  
  55. function addCourse(e) {
  56. if (e) {
  57. e.preventDefault();
  58. }
  59. const title = inputSelectors.titleInput.value;
  60. const type = inputSelectors.typeInput.value;
  61. const description = inputSelectors.descriptionInput.value;
  62. const teacher = inputSelectors.teacherInput.value;
  63. if (title && type && description && teacher) {
  64. const httpHeaders = {
  65. method: 'POST',
  66. body: JSON.stringify({
  67. title: title,
  68. type: type,
  69. description: description,
  70. teacher: teacher
  71. })
  72. }
  73.  
  74. fetch(BASE_URL, httpHeaders)
  75. .then(() => {
  76. loadAllCourses();
  77. form.reset();
  78. })
  79. .catch((err) => {
  80. console.error(err);
  81. })
  82. }
  83.  
  84.  
  85. }
  86.  
  87.  
  88.  
  89. function editCourse(e) {
  90. if (e) {
  91. e.preventDefault();
  92. }
  93. const title = inputSelectors.titleInput.value;
  94. const type = inputSelectors.typeInput.value;
  95. const description = inputSelectors.descriptionInput.value;
  96. const teacher = inputSelectors.teacherInput.value;
  97. const httpHeaders = {
  98. method: 'PATCH',
  99. body: JSON.stringify({
  100. title: title,
  101. type: type,
  102. description: description,
  103. teacher: teacher
  104. })
  105. }
  106. fetch(`${BASE_URL}}${editCourseId}`, httpHeaders)
  107. .then((res) => res.json())
  108. .then(() => {
  109. loadAllCourses();
  110. editCourseBtn.disabled = true;
  111. addCourseBtn.disabled = false;
  112. form.reset();
  113. // otherDOMSelectors.editBtn.setAttribute('disabled', true);
  114. // otherDOMSelectors.addBtn.removeAttribute('disabled');
  115. // otherDOMSelectors.form.reset();
  116. })
  117. .catch((err) => {
  118. console.error(err);
  119. })
  120. }
  121.  
  122. function getForEditCourse(e) {
  123. // const parentElement = this.parentNode;
  124. editCourseId = this.parentNode.id;
  125. // courseToBeUpdated = inputsObj.find((inp) => inp._id === id);
  126. // for (const key in inputDOMSelectors) {
  127. // inputDOMSelectors[key].value = courseToBeUpdated[key];
  128. // }
  129. addCourseBtn.disabled = true;
  130. editCourseBtn.disabled = false;
  131. // otherDOMSelectors.editBtn.removeAttribute('disabled');
  132. this.parentNode.remove();
  133. }
  134.  
  135. function finishCourse() {
  136. const id = this.parentNode.id;
  137.  
  138. const httpHeaders = {
  139. method: 'DELETE'
  140. }
  141.  
  142. fetch(`${BASE_URL}${id}`, httpHeaders)
  143. .then(() => {
  144. loadAllCourses();
  145. })
  146. .catch((err) => {
  147. console.error(err);
  148. })
  149. }
  150.  
  151.  
  152. // const BASE_URL = 'http://localhost:3030/jsonstore/tasks/';
  153. // let inputsObj = [];
  154. // let courseToBeUpdated = {};
  155.  
  156. // const inputDOMSelectors ={
  157. // title: document.getElementById('course-name'),
  158. // type: document.getElementById('course-type'),
  159. // description: document.getElementById('description'),
  160. // teacher: document.getElementById('teacher-name')
  161. // }
  162.  
  163. // const otherDOMSelectors ={
  164. // form: document.querySelector('#form form'),
  165. // addBtn: document.getElementById('add-course'),
  166. // editBtn: document.getElementById('edit-course'),
  167. // loadBtn: document.getElementById('load-course'),
  168. // coursesContainer: document.getElementById('list')
  169. // }
  170.  
  171. // otherDOMSelectors.loadBtn.addEventListener('click', loadAllCourses);
  172. // otherDOMSelectors.editBtn.addEventListener('click', editCourse);
  173. // otherDOMSelectors.addBtn.addEventListener('click', addNewCourse);
  174.  
  175. // function addNewCourse(event){
  176. // if(event){
  177. // event.preventDefault();
  178. // }
  179. // const newCourse = {
  180. // title: inputDOMSelectors.title.value,
  181. // type: inputDOMSelectors.type.value,
  182. // description: inputDOMSelectors.description.value,
  183. // teacher: inputDOMSelectors.teacher.value,
  184. // }
  185. // httpHeaders ={
  186. // method: 'POST',
  187. // body: JSON.stringify(newCourse)
  188. // }
  189.  
  190. // fetch(BASE_URL, httpHeaders)
  191. // .then(() =>{
  192. // loadAllCourses();
  193. // otherDOMSelectors.form.reset();
  194. // })
  195. // .catch((err) => {
  196. // console.error(err);
  197. // })
  198.  
  199. // }
  200.  
  201. // function editCourse(event){
  202. // if(event){
  203. // event.preventDefault();
  204. // }
  205. // const payload = {
  206. // title: inputDOMSelectors.title.value,
  207. // type: inputDOMSelectors.type.value,
  208. // description: inputDOMSelectors.description.value,
  209. // teacher: inputDOMSelectors.teacher.value,
  210. // }
  211. // httpHeaders ={
  212. // method: 'PATCH',
  213. // body: JSON.stringify(payload)
  214. // }
  215. // fetch(`${BASE_URL}${courseToBeUpdated._id}`, httpHeaders)
  216. // .then((res) => res.json)
  217. // .then(() =>{
  218. // loadAllCourses();
  219. // otherDOMSelectors.editBtn.setAttribute('disabled', true);
  220. // otherDOMSelectors.addBtn.removeAttribute('disabled');
  221. // otherDOMSelectors.form.reset();
  222. // })
  223. // .catch((err) => {
  224. // console.error(err);
  225. // })
  226. // }
  227.  
  228. // function loadAllCourses(event){
  229. // if(event){
  230. // event.preventDefault();
  231. // }
  232. // otherDOMSelectors.coursesContainer.innerHTML = '';
  233.  
  234. // fetch(BASE_URL)
  235. // .then((res) => res.json())
  236. // .then((data) =>{
  237. // inputsObj = Object.values(data);
  238. // console.log(inputsObj);
  239. // for (const course of Object.values(data)) {
  240. // const divContainer = createElement('div', '', otherDOMSelectors.coursesContainer, '', ['container'], {id: course._id});
  241. // createElement('h2', course.title, divContainer);
  242. // createElement('h3', course.teacher, divContainer);
  243. // createElement('h3', course.type, divContainer);
  244. // createElement('p', course.description, divContainer);
  245. // const editBtnInContainer = createElement('button', 'Edit Course', divContainer, '', ['edit-btn']);
  246. // editBtnInContainer.addEventListener('click', loadDataInInputForm)
  247. // const finishBtnInContainer = createElement('button', 'Finish Course', divContainer, '', ['finish-btn']);
  248. // finishBtnInContainer.addEventListener('click', deleteCourse);
  249. // }
  250.  
  251. // })
  252. // .catch((err) => {
  253. // console.error(err);
  254. // })
  255. // }
  256.  
  257. // function loadDataInInputForm(){
  258. // const parentElement = this.parentNode;
  259. // const id = this.parentNode.id;
  260. // courseToBeUpdated = inputsObj.find((inp) => inp._id === id);
  261. // for (const key in inputDOMSelectors) {
  262. // inputDOMSelectors[key].value = courseToBeUpdated[key];
  263. // }
  264. // otherDOMSelectors.addBtn.setAttribute('disabled', true);
  265. // otherDOMSelectors.editBtn.removeAttribute('disabled');
  266. // parentElement.remove();
  267. // }
  268.  
  269. // function deleteCourse(){
  270. // const id = this.parentNode.id;
  271.  
  272. // httpHeaders ={
  273. // method: 'DELETE'
  274. // }
  275.  
  276. // fetch(`${BASE_URL}${id}`, httpHeaders)
  277. // .then(() =>{
  278. // loadAllCourses();
  279. // })
  280. // .catch((err) => {
  281. // console.error(err);
  282. // })
  283. // }
  284.  
  285.  
  286. // function inputsValidator(obj){
  287. // return Object.values(obj).every((inp) => inp.value.trim() !== '');
  288. // }
  289.  
  290.  
  291.  
  292. function createElements(type, contentOrValue, useInnerHTML, parentNode, id, classes, attributes) {
  293. const htmlElement = document.createElement(type);
  294. if (contentOrValue && useInnerHTML) {
  295. htmlElement.innerHTML = contentOrValue;
  296. }
  297. else {
  298. if (contentOrValue && type === 'input') {
  299. htmlElement.value = contentOrValue;
  300. }
  301. if (contentOrValue && type !== 'input') {
  302. htmlElement.textContent = contentOrValue;
  303. }
  304. }
  305.  
  306. if (id) {
  307. htmlElement.id = id;
  308. }
  309. if (classes) {
  310. htmlElement.classList.add(...classes)
  311. }
  312. // {src: 'link', href : 'http'}
  313. if (attributes) {
  314. for (const key in attributes) {
  315. htmlElement.setAttribute(key, attributes[key])
  316. }
  317. }
  318. if (parentNode) {
  319. parentNode.appendChild(htmlElement);
  320. }
  321. return htmlElement;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement