import { OpinionCard, OpinionCardContent, OpinionCardContentTime, 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, useEffect } from "react"; import JSOG from "jsog"; import { Modal, ModalContent, ModalClose, ModalHeader, ModalBody, ModalTextarea, ModalFooter, } from "../Components/Styled/Modal.style"; import axios from "../api/axios"; function OpinionTree({ professor }) { var renderedOpinionIds = []; var postCount; // za da ne go pokazuva ispod postot const { auth, setAuth } = useContext(AuthApi); let navigate = useNavigate(); const [replyModalDisplay, setReplyModalDisplay] = useState("none"); const [replyContent, setReplyContent] = useState(""); const [postForModal, setPostForModal] = useState(null); const [user, setUser] = useState(null); const [loadedUser, setLoadedUser] = useState(false); const [fetchError, setFetchError] = useState(false); const [errorMessage, setErrorMessage] = useState(""); useEffect(() => { const url = `http://192.168.0.19:8080/secure/currentUser`; const fetchUser = async () => { try { const response = await axios.get(url, { withCredentials: true }); var cyclicGraph = await response.data; var jsogStructure = JSOG.encode(cyclicGraph); cyclicGraph = JSOG.decode(jsogStructure); setUser(cyclicGraph); setLoadedUser(true); } catch (error) { setFetchError(true); } }; if (auth) fetchUser(); }, []); const handleLike = async (post) => { if (auth) { if ( loadedUser && user && !post.votes.some((e) => e.user.id === user.id) ) { const response = await axios( `http://192.168.0.19:8080/secure/upvoteOpinion/${post.postId}`, { method: "get", withCredentials: true, } ); window.location.reload(false); } else { return; } } else { navigate("/login"); } }; const handleDislike = async (post) => { if (auth) { if ( loadedUser && user && !post.votes.some((e) => e.user.id === user.id) ) { const response = await axios( `http://192.168.0.19:8080/secure/downvoteOpinion/${post.postId}`, { method: "get", withCredentials: true, } ); window.location.reload(false); } else { return; } } else { navigate("/login"); } }; const handleReply = (opinion) => { if (auth) { setReplyModalDisplay("block"); setPostForModal(opinion); document.body.style.overflowY = "hidden"; } else { navigate("/login"); } }; const handleModalCloseClick = () => { setReplyModalDisplay("none"); document.body.style.overflowY = "auto"; }; const handleContentChange = (e) => { setReplyContent(e.target.value); }; const handleReplySubmit = async (e, postId) => { e.preventDefault(); if (!replyContent.length < 1) { const response = await axios( `http://192.168.0.19:8080/secure/professor/${professor.professorId}/replyToOpinion/${postId}`, { method: "post", data: { content: replyContent, }, withCredentials: true, } ); setErrorMessage(""); window.location.reload(false); } else { setErrorMessage("Полето за содржина не смее да биде празно"); } }; function displayChildPosts(child, parentPostAuthorUsername, replyIndent) { if (child == null) return; postCount = renderedOpinionIds.push(child.postId); return (

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

{child.content}

{child.timePosted === child.timeLastEdited ? ( {dateConverter( new Date(child.timePosted).toString().slice(4, -43) )} ) : ( {dateConverter( new Date(child.timeLastEdited).toString().slice(4, -43) )}{" "} (едитирано од модератор) )}
e.vote === "UPVOTE" && e.user.id === user.id ) ? "green" : "darkgrey" : "darkgrey" } onClick={() => handleLike(child)} /> {child.votes.filter((v) => v.vote === "UPVOTE").length} e.vote === "DOWNVOTE" && e.user.id === user.id ) ? "indianred" : "darkgrey" : "darkgrey" } onClick={() => handleDislike(child)} /> {child.votes.filter((v) => v.vote === "DOWNVOTE").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.content}

{opinion.timePosted === opinion.timeLastEdited ? ( {dateConverter( new Date(opinion.timePosted).toString().slice(4, -43) )} ) : ( {dateConverter( new Date(opinion.timeLastEdited) .toString() .slice(4, -43) )}{" "} (едитирано од модератор) )}
e.vote === "UPVOTE" && e.user.id === user.id ) ? "green" : "darkgrey" : "darkgrey" } onClick={() => handleLike(opinion)} /> {opinion.votes.filter((v) => v.vote === "UPVOTE").length} e.vote === "DOWNVOTE" && e.user.id === user.id ) ? "indianred" : "darkgrey" : "darkgrey" } onClick={() => handleDislike(opinion)} /> { opinion.votes.filter((v) => v.vote === "DOWNVOTE") .length } handleReply(opinion)} />
{opinion.children.map((child) => displayChildPosts(child, opinion.author.username, 30) )}
); } return null; })} {postForModal && ( ×

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

handleReplySubmit(e, postForModal.postId)}>

{errorMessage}

РЕПЛИЦИРАЈ
)}
); } export default OpinionTree;