Skip to content

Commit

Permalink
Added the initial versions of connections to the backend for both com…
Browse files Browse the repository at this point in the history
…ment and answers
  • Loading branch information
berkaydoner committed Dec 8, 2021
1 parent efb7cf5 commit 3e6ff3f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
125 changes: 125 additions & 0 deletions frontend/src/Controllers/CommentAnswerController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
export function getAnswersOfComment(post_id,comment_id){
const options = {
method: 'GET',
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
}

//let response = fetch("/api/posts/"+String(post_id)+"/comments/"+String(comment_id),options)
let response = fetch("http://localhost:3000/answers/",options)
.then(response=>response.json())
.then((result)=> {
return result.items?result.items.map(d=>
({user:{username:d.actor.name},content:d.object.answer,creationDate:d.object.creationDate}))
:[]}
)
return response


}

export function getCommentByID(post_id,comment_id){

const options = {
method: 'GET',
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
}

let comment = {}
//let response = fetch("/api/posts/"+String(post_id)+"/comments/"+String(comment_id),options)
fetch("http://localhost:3000/comment/",options)
.then(response=>response.json())
.then((result)=>
{
comment.user = {username:result.actor.name};
comment.content = result.object.content;
comment.creationDate = result.object.creationDate;
}
)
getAnswersOfComment(post_id,comment_id).then(answerList=>
comment.answers=answerList
)
console.log(comment)
return comment
}

export function getCommentsAndAnswersOfEvent(post_id){

const options = {
method: 'GET',
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
}
//let response = fetch("/api/posts/"+String(post_id)+"/comments",options)
let comments = []
fetch("http://localhost:3000/comments/",options)
.then(response=>response.json())
.then(r=>{console.log(r);return r;})
.then(response=>response.items.forEach(d=>
comments.push(getCommentByID(d.object.split("/")[3],d.object.split("/")[5]))
))
return comments
}
export function postComment(post_id,owner_id,username,content){

const options = {
method: 'POST',
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
body: {
"@context": "https://www.w3.org/ns/activitystreams",
"summary": username + " created a comment",
"type": "Create",
"actor": {
"type": "Person",
"name": username
},
"object": {
"type": "Comment",
"postId": post_id,
"ownerId": owner_id,
"content": content,
"creationDate": Date.now()
}
}
}
//fetch("/api/posts/"+String(post_id)+"/comments",options)
let comment = {}
fetch("http://localhost:3000/comments/",options)
.then(response=>response.json())
.then(d=> {
comment = {user:{username:d.actor.name},
content:d.object.content,
creationDate:d.object.creationDate,
answers:[]};
})
return comment
}
export function postAnswer(post_id,comment_id,owner_id,username,content){

const options = {
method: 'POST',
headers: { 'Accept': 'application/json','Content-Type': 'application/json'},
body: {
"@context": "https://www.w3.org/ns/activitystreams",
"summary": username + " created an answer.",
"type": "Create",
"actor": {
"type": "Person",
"name": username
},
"object": {
"type": "Answer",
"answer": content,
"creationDate": Date.now()
}
}
}
//fetch("/api/posts/"+String(post_id)+"/comments/"+String(comment_id)/answers,options)
let answer = {}
fetch("http://localhost:3000/comments/",options)
.then(response=>response.json())
.then(d=> {answer= {user:{username:d.actor.name},
content:d.object.content,
creationDate:d.object.creationDate,
};
})
return answer
}
12 changes: 11 additions & 1 deletion frontend/src/Views/Comments/Comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import Button from "@mui/material/Button";
import TextField from '@mui/material/TextField';
import Comment from "./Comment";
import List from '@mui/material/List';

import {
getAnswersOfComment,
getCommentByID,
getCommentsAndAnswersOfEvent, postAnswer, postComment
} from "../../Controllers/CommentAnswerController";
import Grid from "@mui/material/Grid";
import DialogContent from "@mui/material/DialogContent";
import {useRef} from "react";
Expand All @@ -21,6 +25,12 @@ export default function Comments(props){
setNewComment(event.target.value);
};
const handlePostComment = () => {
if(newComment.includes("@")){
postAnswer();
}
else{
postComment();
}
};
const comments = [{avatar:"https://avatars.githubusercontent.com/u/52797716?v=4}",
username:"bdoner", content:"It was nice playing with you.", isAnswer:false,
Expand Down

0 comments on commit 3e6ff3f

Please sign in to comment.