petur_stoqnov

Untitled

Mar 7th, 2021
778
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function general() {
  2.     let topic = document.querySelector('#idPost');
  3.     let time = document.querySelector('#idTime');
  4.     let subscribers = document.querySelector('#some')
  5.  
  6.     let body = document.querySelector('body');
  7.     body.addEventListener('click', (e) => service(e))
  8.  
  9.     function service(e) {
  10.         e.preventDefault();
  11.         e.stopImmediatePropagation();
  12.         if (e.target.textContent === "Home") {
  13.             window.location.href = 'index.html'
  14.         } else if (e.target.textContent === "Post") {
  15.             e.preventDefault()
  16.             let comment = document.querySelector('#comment');
  17.             let username = document.querySelector('#username')
  18.             if (comment.value.trim() === "" || username.value.trim() === "") {
  19.                 alert('Empty fields are not allowed. Please try again!')
  20.             } else {
  21.                 addCommentToServer({
  22.                     comment: comment.value,
  23.                     username: username.value,
  24.                     topic: sessionStorage.getItem('target')
  25.                 })
  26.                 comment.value = ""
  27.                 username.value = ""
  28.                 addAllCommentsToHTML()
  29.             }
  30.         }
  31.     }
  32.  
  33.  
  34.     async function addAllCommentsToHTML() {
  35.         let div = document.querySelector('#allComments')
  36.         div.innerHTML = ""
  37.         let response = await fetch('http://localhost:3030/jsonstore/collections/myboard/comments')
  38.         let data = await response.json();
  39.         Object.values(data).forEach(c => {
  40.             if (sessionStorage.getItem('target') === c.topic) {
  41.                 div.insertAdjacentHTML('beforeend', returnHtmlStringComment(c.comment, c.username))
  42.             }
  43.         })
  44.     }
  45.  
  46.     async function addCommentToServer(obj) {
  47.         let response = await fetch('http://localhost:3030/jsonstore/collections/myboard/comments', {
  48.             method: 'post',
  49.             headers: {'Content-type': 'application/json'},
  50.             body: JSON.stringify(obj)
  51.         })
  52.         let data = await response.json();
  53.     }
  54.  
  55.     function returnHtmlStringComment(comment, username) {
  56.         let date = new Date();
  57.         let htmlString = ` <div class="comment">
  58.                 <header class="header">
  59.                     <p><span>${username}</span> posted on <time>${date.toLocaleString()}</time></p>
  60.                 </header>
  61.                 <div class="comment-main">
  62.                     <div class="userdetails">
  63.                         <img src="./static/profile.png" alt="avatar">
  64.                     </div>
  65.                     <div class="post-content">
  66.                         <p>${comment}</p>
  67.                     </div>
  68.                 </div>
  69.                 <div class="footer">
  70.                     <p><span>0</span> likes</p>
  71.                 </div>
  72.             </div>`;
  73.         return htmlString;
  74.     }
  75.  
  76.     topic.textContent = sessionStorage.getItem('target')
  77.     time.textContent = sessionStorage.getItem('time')
  78.     subscribers.textContent = sessionStorage.getItem('subscribers')
  79.     addAllCommentsToHTML()
  80. }
  81.  
  82. general()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×