Sichanov

04. Blog

Apr 2nd, 2023
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     const BASE_URL_POSTS = 'http://localhost:3030/jsonstore/blog/posts/';
  3.     const BASE_URL_COMMENTS = 'http://localhost:3030/jsonstore/blog/comments/';
  4.     const btnLoadPosts = document.getElementById('btnLoadPosts');
  5.     const viewBtn = document.getElementById('btnViewPost');
  6.     const optionPosts = document.getElementById('posts');
  7.     const postTitle = document.getElementById('post-title');
  8.     const postBody = document.getElementById('post-body');
  9.     const commentsBody = document.getElementById('post-comments');
  10.     btnLoadPosts.addEventListener('click', loadPostHandler);
  11.     viewBtn.addEventListener('click', loadCommentHandler);
  12.     let posts = null;
  13.     let comments = null;
  14.  
  15.     async function loadPostHandler() {
  16.         const response = await fetch(BASE_URL_POSTS);
  17.         posts = await response.json();
  18.         for (const [_key, post] of Object.entries(posts)) {
  19.             createElement('option', post.title, optionPosts, '', '', {value: post.id});
  20.         }
  21.     }
  22.  
  23.     async function loadCommentHandler(e) {
  24.         const selected = e.currentTarget.previousElementSibling.value;
  25.         const responseComments = await fetch(BASE_URL_COMMENTS);
  26.         comments = await responseComments.json();
  27.         console.log(selected);
  28.         const post = Object.entries(posts).find(el => selected === el[0]);
  29.         const [_postID, postData] = post;
  30.         const comment = Object.entries(comments).filter(el => el[1].postId === selected);
  31.         console.log(comment);
  32.         postTitle.textContent = postData.title;
  33.         postBody.textContent = postData.body;
  34.         commentsBody.innerHTML = '';
  35.         comment.forEach(el => createElement('li', el[1].text, commentsBody, el[0]));
  36.        
  37.     }
  38.  
  39.     function createElement(type, content, parentNode, id, classes, attributes) {
  40.         const htmlElement = document.createElement(type);
  41.  
  42.         if (content && type !== 'input') {
  43.             htmlElement.textContent = content;
  44.         }
  45.  
  46.         if (content && type === 'input') {
  47.             htmlElement.value = content;
  48.         }
  49.  
  50.         if (id) {
  51.             htmlElement.id = id;
  52.         }
  53.  
  54.         if (classes) {
  55.             htmlElement.classList.add(...classes);
  56.         }
  57.  
  58.         if (parentNode) {
  59.             parentNode.appendChild(htmlElement);
  60.         }
  61.  
  62.         if (attributes) {
  63.             for (const key in attributes) {
  64.                 htmlElement.setAttribute(key, attributes[key]);
  65.             }
  66.         }
  67.  
  68.         return htmlElement;
  69.     }
  70. }
  71.  
  72. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment