Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
export function visualiseComments(postId,topicContainer){ getAllComments(); let homeLink=document.querySelector("a"); homeLink.addEventListener("click", function (e){ e.preventDefault(); console.log("back to all topics") }) let main=document.getElementsByTagName("main")[0]; main.appendChild(topicContainer); let submitForm=document.createElement("div"); submitForm.classname="answer-comment"; submitForm.innerHTML=`
currentUser
comment:
Username
*
Post
`; main.appendChild(submitForm); let postButton=submitForm.querySelector("button"); console.log(postButton) let nameArea=document.getElementById("comment"); let userInput=document.getElementById("username"); postButton.addEventListener("click", function(e){ e.preventDefault(); postComment() }) function postComment(){ let commentText=nameArea.value; let author=userInput.value; let newComment={commentAuthor:author, text:commentText, postId} var myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); var raw = JSON.stringify(newComment); var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow' }; fetch("http://localhost:3030/jsonstore/collections/myboard/comments", requestOptions) .then(response => response.json()) .then(result => getAllComments()) .catch(error => console.log('error', error)); } function getAllComments(){ var requestOptions = { method: 'GET', redirect: 'follow' }; fetch("http://localhost:3030/jsonstore/collections/myboard/comments", requestOptions) .then(response => response.json()) .then(result => filterComments(result)) .catch(error => console.log('error', error)); } function filterComments(data){ let arr=Object.values(data); let filteredArr=arr.filter(a=>a.postId==postId); main.innerHTML=""; main.appendChild(topicContainer); filteredArr.forEach(c => { renderComment(c) }); main.appendChild(submitForm) } function renderComment(data){ let newCommentDiv=document.createElement("div"); newCommentDiv.className="comment"; newCommentDiv.innerHTML=`
${data.commentAuthor}
posted on
${Date.now()}
${data.text}
` nameArea.value=""; userInput.value=""; main.appendChild(newCommentDiv); } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)