import { OpinionCard, OpinionCardContent, OpinionCardContentTime, OpinionCardContentTitle, OpinionReplyCard, OpinionReplyCardContent, OpinionReplyCardContentTime, StyledFontAwesomeIcon, VoteCount, } from "./Styled/OpinionCard.style"; import { solid } from "@fortawesome/fontawesome-svg-core/import.macro"; import { dateConverter } from "../Util/dateConverter"; import AuthApi from "../api/AuthApi"; import { useNavigate } from "react-router-dom"; import { useContext, useState } from "react"; import { Modal, ModalContent, ModalClose, ModalHeader, ModalBody, ModalTextarea, ModalFooter, } from "../Components/Styled/Modal.style"; import axios from "../api/axios"; function OpinionTree({ professor, user, userLoaded }) { var renderedOpinionIds = []; var postCount; // za da ne go pokazuva ispod postot let navigate = useNavigate(); const { auth, setAuth } = useContext(AuthApi); let [replyModalDisplay, setReplyModalDisplay] = useState("none"); const [replyContent, setReplyContent] = useState(""); const [postForModal, setPostForModal] = useState(null); const handleLike = async (post) => { if ( auth && userLoaded && !post.likes.some((e) => e.id === user.user.id) && !post.dislikes.some((e) => e.id === user.user.id) ) { const response = await axios( `http://192.168.0.17:8080/secure/professor/${professor.professorId}/upvoteOpinion/${post.postId}`, { method: "get", withCredentials: true, } ); window.location.reload(false); } else { return; } }; const handleDislike = async (post) => { if ( auth && auth && userLoaded && !post.likes.some((e) => e.id === user.user.id) && !post.dislikes.some((e) => e.id === user.user.id) ) { const response = await axios( `http://192.168.0.17:8080/secure/professor/${professor.professorId}/downvoteOpinion/${post.postId}`, { method: "get", withCredentials: true, } ); window.location.reload(false); } else { return; } }; const handleReply = (opinion) => { if (auth) { setReplyModalDisplay("block"); setPostForModal(opinion); } else { navigate("/login"); } }; const handleModalCloseClick = () => { setReplyModalDisplay("none"); }; const handleContentChange = (e) => { setReplyContent(e.target.value); }; const handleReplySubmit = async (e, postId) => { e.preventDefault(); const response = await axios( `http://192.168.0.17:8080/secure/professor/${professor.professorId}/replyToOpinion/${postId}`, { method: "post", body: { content: replyContent, }, withCredentials: true, } ); window.location.reload(false); //console.log(response); }; function displayChildPosts(child, parentPostAuthorUsername, replyIndent) { if (child == null) return; postCount = renderedOpinionIds.push(child.postId); return (

{child.author.username} му реплицирал на{" "} {parentPostAuthorUsername}

{child.content}

{dateConverter( new Date(child.timePosted).toString().slice(4, -43) )} {auth && userLoaded && user.user.id !== child.author.id && ( <> e.id === user.user.id) ? "greenyellow" : "darkgrey" } onClick={() => handleLike(child)} /> {child.likes.length} e.id === user.user.id) ? "indianred" : "darkgrey" } onClick={() => handleDislike(child)} /> {child.dislikes.length} handleReply(child)} /> )}
{child.children.map((childOfChild) => displayChildPosts( childOfChild, child.author.username, replyIndent + 30 ) )}
); } return (
{professor.relatedOpinions.map((opinion) => { if (!renderedOpinionIds.includes(opinion.postId)) { postCount = renderedOpinionIds.push(opinion.postId); return (

{opinion.author.username} напишал

{opinion.title}

{opinion.content}

{dateConverter( new Date(opinion.timePosted).toString().slice(4, -43) )} {auth && userLoaded && user.user.id !== opinion.author.id && ( <> e.id === user.user.id) ? "greenyellow" : "darkgrey" } onClick={() => handleLike(opinion)} /> {opinion.likes.length} e.id === user.user.id) ? "indianred" : "darkgrey" } onClick={() => handleDislike(opinion)} /> {opinion.dislikes.length} handleReply(opinion)} /> )}
{opinion.children.map((child) => displayChildPosts(child, opinion.author.username, 30) )}
); } })} {postForModal && ( ×

Реплика на {postForModal.author.username}

handleReplySubmit(e, postForModal.postId)}> РЕПЛИЦИРАЈ
)}
); } export default OpinionTree;