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 ? ({opinion.author.username} {" "} напишал
{opinion.content}
{opinion.timePosted === opinion.timeLastEdited ? (