Advertisement
Iv555

Untitled

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