Liliana797979

forum - js applications - topicView.js

Nov 12th, 2021
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let commentElement = document.querySelector('.comment');
  2. let replyForm = document.querySelector('.answer form');
  3. let commentsElement = document.querySelector('#user-comment');
  4. let homeElement = document.querySelector('li a');
  5.  
  6. homeElement.addEventListener('click',goHome);
  7. replyForm.addEventListener('submit',createReply);
  8.  
  9. fetch(`http://localhost:3030/jsonstore/collections/myboard/posts/${localStorage.getItem('topicId')}`)
  10. .then(res => res.json())
  11. .then(function (res) {
  12.     commentElement.appendChild(createHtmlTopic(res));
  13. });
  14. fetch(`http://localhost:3030/jsonstore/collections/myboard/comments?where=postId%3D"${localStorage.getItem('topicId')}`)
  15. .then(res => res.json())
  16. .then(function (res) {
  17.     Object.values(res).forEach(r => {
  18.         commentElement.appendChild(createHtmlReply(r));
  19.     })
  20. });
  21.  
  22. function createReply(e) {
  23.     e.preventDefault();
  24.     let form = new FormData(replyForm);
  25.     let comment = form.get('postText');
  26.     let username = form.get('username');
  27.  
  28.     replyForm.reset();
  29.     if (!username) {
  30.         alert('Username cannot be empty!');
  31.         return;
  32.     }
  33.  
  34.     fetch('http://localhost:3030/jsonstore/collections/myboard/comments', {
  35.         method: 'POST',
  36.         headers: {
  37.             'Content-Type': 'application/json'
  38.         },
  39.         body: JSON.stringify({
  40.             username,
  41.             comment,
  42.             postId: localStorage.getItem('topicId')
  43.         })
  44.     })
  45.         .then(res => res.json())
  46.         .then(res => commentElement.appendChild(createHtmlReply(res)));
  47. }
  48.  
  49. function createHtmlTopic(t) {
  50.     let headersDivElemt = document.createElement('div');
  51.     headersDivElemt.classList.add('header');
  52.  
  53.     let profilePicElement = document.createElement('img');
  54.     profilePicElement.src ='./static/profile.png'
  55.     profilePicElement.alt = 'avatar';
  56.  
  57.     let metaParagraphElement = document.createElement('p');
  58.     let usernameSpanElement = document.createElement('span');
  59.     usernameSpanElement.textContent = t.username;
  60.  
  61.     let timeElement = document.createElement('time');
  62.  
  63.     timeElement.textContent = getPostTime();
  64.     metaParagraphElement.textContent = ' posted on ';
  65.     metaParagraphElement.prepend(usernameSpanElement);
  66.     metaParagraphElement.appendChild(timeElement);
  67.  
  68.     let contetnParagraph = document.createElement('p');
  69.     contetnParagraph.classList.add('post-content');
  70.     contetnParagraph.textContent = t.post;
  71.  
  72.     headersDivElemt.appendChild(profilePicElement);
  73.     headersDivElemt.appendChild(metaParagraphElement);
  74.     headersDivElemt.appendChild(contetnParagraph);
  75.  
  76.     return headersDivElemt;
  77. }
  78. function createHtmlReply(r) {
  79.     let replyWrapperDiv = document.createElement('div');
  80.     replyWrapperDiv.classList.add('topic-name-wrapper');
  81.  
  82.     let topicNameDiv = document.createElement('div');
  83.     topicNameDiv.classList.add('topic-name');
  84.  
  85.     let infoParagraphElement = document.createElement('p');
  86.     infoParagraphElement.textContent = ' commented on ';
  87.     let nameStrong = document.createElement('strong');
  88.     nameStrong.textContent = r.username;
  89.     infoParagraphElement.prepend(nameStrong);
  90.     let timeElement = document.createElement('time');
  91.     timeElement.textContent = getReplyTime();
  92.     infoParagraphElement.appendChild(timeElement);
  93.  
  94.     let contentDiv = document.createElement('div');
  95.     contentDiv.classList.add('post-content');
  96.  
  97.     let contentParagraph = document.createElement('p');
  98.     contentParagraph.textContent = r.comment;
  99.  
  100.     contentDiv.appendChild(contentParagraph);
  101.     topicNameDiv.appendChild(infoParagraphElement);
  102.     topicNameDiv.appendChild(contentParagraph);
  103.     replyWrapperDiv.appendChild(topicNameDiv);
  104.  
  105.     return replyWrapperDiv;
  106. }
  107. function getPostTime() {
  108.     let time = new Date();
  109.     let year = time.getFullYear()
  110.     let month = time.getMonth()
  111.         .toString()
  112.         .padStart(2, 0);
  113.  
  114.     let day = time.getDay()
  115.         .toString()
  116.         .padStart(2, 0);
  117.  
  118.     let hours = time.getHours() > 12 ? (time.getHours() - 12).toString().padStart(2, 0)
  119.         : (time.getHours()).toString().padStart(2, 0);
  120.     let minutes = time.getMinutes()
  121.         .toString()
  122.         .padStart(2, 0);
  123.  
  124.     let seconds = time.getHours()
  125.         .toString()
  126.         .padStart(2, 0);
  127.  
  128.     return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  129. }
  130.  
  131. function getReplyTime() {
  132.     let time = new Date();
  133.     let year = time.getFullYear();
  134.     let month = time.getMonth();
  135.     let day = time.getDay();
  136.     let hours = time.getHours()
  137.         .toString()
  138.         .padStart(2, 0);
  139.  
  140.     let minutes = time.getMinutes()
  141.         .toString()
  142.         .padStart(2, 0);
  143.  
  144.     let seconds = time.getSeconds()
  145.         .toString()
  146.         .padStart(2, 0);
  147.  
  148.     let format = hours > 12 ? "PM" : "AM";
  149.  
  150.     return `${day}/${month}/${year}, ${hours}:${minutes}:${seconds} ${format}`;
  151. }
  152.  
  153. function goHome(){
  154.     location.assign('index.html');
  155. }
Advertisement
Add Comment
Please, Sign In to add comment