drak138

06-blog

Jul 1st, 2024
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function attachEvents() {
  2.     const urls={
  3.         "posts":`http://localhost:3030/jsonstore/blog/posts`,
  4.         "comments":`http://localhost:3030/jsonstore/blog/comments`
  5.     }
  6.     const postComments=document.getElementById("post-comments")
  7.     const postBody=document.getElementById("post-body")
  8.     const postTitleRef=document.getElementById("post-title")
  9.     const postsRef=document.getElementById("posts")
  10.     const btnViewPostRef=document.getElementById("btnViewPost")
  11.     const btnLoadPostsRef=document.getElementById("btnLoadPosts")
  12.     btnLoadPostsRef.addEventListener("click", loadPosts)
  13.     btnViewPostRef.addEventListener("click",onView)
  14.  
  15.    
  16.  
  17.     async function loadPosts(){
  18.         const posts=await getPosts()
  19.         postsRef.innerHTML=""
  20.         Object.entries(posts).forEach(async(row)=>{
  21.             const[id,data]=row
  22.             const option= createOption(data)
  23.             postsRef.appendChild(option)
  24.         })
  25.    
  26.     }
  27.  
  28.     async function onView(e){
  29.         try{
  30.         const comments = await getComments();
  31.         const posts=await getPosts();
  32.         console.log(comments);
  33.         const id = postsRef.value;
  34.         const text = postsRef.options[postsRef.selectedIndex].text;
  35.         console.log(text);
  36.    
  37.         console.log(Object.values(posts));
  38.    
  39.         let post = Object.values(posts).find(post => post.title === text);
  40.         let postDetails = Object.values(comments).filter(c => c.postId === id);
  41.    
  42.         displayPost(post);
  43.         postDetails.forEach(async(data)=>{
  44.                 const comment= createComment(data)
  45.                 postComments.appendChild(comment)
  46.         })
  47.     }
  48.     catch(err){
  49.         alert(err.message);
  50.     }
  51.  
  52.     }
  53.  
  54.     function displayPost(post){
  55.         postTitleRef.textContent=post.title
  56.         postBody.textContent=post.body
  57.  
  58.     }
  59.  
  60.     async function getComments(){
  61.         const response=await fetch(urls.comments)
  62.         if (!response.ok) {
  63.             throw new Error(`Error ${response.status} (${response.statusText})`)
  64.         }
  65.        
  66.         return await response.json()
  67.     }
  68.    function createOption(data){
  69.         const option=document.createElement("option")
  70.         option.setAttribute("value",`${data.id}`)
  71.         option.id=data.id
  72.         option.textContent=`${data.title}`
  73.         return option
  74.     }
  75.     async function getPosts(){
  76.         const response=await fetch(urls.posts)
  77.         if (!response.ok) {
  78.             throw new Error(`Error ${response.status} (${response.statusText})`)
  79.         }
  80.         return await response.json()
  81.     }
  82.     function createComment(data){
  83.         const li=document.createElement("li")
  84.         li.setAttribute("id",`${data.id}`)
  85.         li.textContent=`${data.text}`
  86.         console.log(li)
  87.         return li
  88.     }
  89. }
  90. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment