/* eslint-disable no-unused-vars */ // noinspection JSUnresolvedVariable,ES6ConvertVarToLetConst,JSUnresolvedFunction,SpellCheckingInspection,JSUnusedLocalSymbols import React, { useEffect, useState, useContext } from "react"; import { OpinionCard, OpinionCardContent, OpinionCardContentTime, } from "../Components/Styled/OpinionCard.style"; import { UserDetailsCard, UserDetailsCardContent, } from "../Components/Styled/UserDetails.style"; import { dateConverter } from "../Util/dateConverter"; import axios from "../api/axios"; import JSOG from "jsog"; import AuthApi from "../api/AuthApi"; import {EntityLi, EntityTypeSelector, EntityUl} from "../Components/Styled/EntityList.style"; import { AddOpinionButton, Modal, ModalBody, ModalClose, ModalContent, ModalFooter, ModalHeader, ModalTextarea, ModalInput } from "../Components/Styled/Modal.style"; import LoadingSpinner from "../Components/Styled/LoadingSpinner.style"; function UserDashboard() { const { auth, setAuth } = useContext(AuthApi); const [user, setUser] = useState(null); const [loadedUser, setLoadedUser] = useState(false); const [fetchError, setFetchError] = useState(false); const [postReports, setPostReports] = useState(null); const [loadedPostReports, setLoadedPostReports] = useState(false); const [reportModalDisplay, setReportModalDisplay] = useState("none"); const [reportForModal, setReportForModal] = useState(null); const [actionType, setActionType] = useState(0); const [newPostContent, setNewPostContent] = useState(""); const [newOpinionTargetProfessorId, setNewOpinionTargetProfessorId] = useState(""); const [newOpinionTargetProfessor, setNewOpinionTargetProfessor] = useState(null); const [loadedNewProfessor,setLoadedNewProfessor] = useState(false); const [newParentPostId, setNewParentPostId] = useState("-1"); const [newThreadTitle, setNewThreadTitle] = useState(""); const [newParentThreadId,setNewParentThreadId] = useState("-1"); const [newTargetSubjectId, setNewTargetSubjectId] = useState(""); const [newTargetSubject, setNewTargetSubject] = useState(null); const [loadedNewSubject, setLoadedNewSubject] = useState(null); const [markResolved, setMarkResolved] = useState(false); const [errMsg, setErrMsg] = useState(""); const handleModalCloseClick = () => { setReportForModal(null); setReportModalDisplay("none"); document.body.style.overflowY = "auto"; }; const handleViewReportButtonClick = (e,report) => { e.preventDefault(); setReportForModal(report); } useEffect(() => { if(reportForModal!==null) { if (reportForModal.post !== null) { setNewPostContent(reportForModal.post.content); if(reportForModal.post.title !== null) setNewThreadTitle(reportForModal.post.title); if(reportForModal.post.targetProfessor !== undefined) setNewOpinionTargetProfessorId(reportForModal.post.targetProfessor.professorId); //prvicnoto if(reportForModal.post.targetProfessor === undefined) setNewTargetSubjectId(reportForModal.post.targetSubject.subjectId); //prvicnoto } setReportModalDisplay("block"); document.body.style.overflowY = "hidden"; } }, [reportForModal]); const[loadingProf, setLoadingProf] = useState(false); const handleNewTargetProfessorChange = async (e) => { setLoadingProf(true); e.preventDefault(); if (newOpinionTargetProfessorId!=="") { try { const response = await axios.get(`http://192.168.0.29:8080/public/professor/${newOpinionTargetProfessorId}`, {withCredentials: true}); let cyclicGraph = await response.data; var jsogStructure = JSOG.encode(cyclicGraph); cyclicGraph = JSOG.decode(jsogStructure); setNewOpinionTargetProfessor(cyclicGraph); setLoadedNewProfessor(true); setLoadingProf(false); } catch (error) { setFetchError(true); } } } const[loadingSubj, setLoadingSubj] = useState(false); const handleNewTargetSubjectChange = async (e) => { e.preventDefault(); setLoadingSubj(true); if (newTargetSubjectId!=="") { try { const response = await axios.get(`http://192.168.0.29:8080/public/subject/${newTargetSubjectId}`, {withCredentials: true}); let cyclicGraph = await response.data; var jsogStructure = JSOG.encode(cyclicGraph); cyclicGraph = JSOG.decode(jsogStructure); setNewTargetSubject(cyclicGraph); setLoadedNewSubject(true); setLoadingSubj(false); } catch (error) { setFetchError(true); } } } useEffect(() => { const url1 = `http://192.168.0.29:8080/secure/currentUser`; const url2 = `http://192.168.0.29:8080/secure/getAllPostReports`; const fetchUser = async () => { try { if(!loadedUser) { const response = await axios.get(url1, {withCredentials: true}); let cyclicGraph = await response.data; var jsogStructure = JSOG.encode(cyclicGraph); cyclicGraph = JSOG.decode(jsogStructure); setUser(cyclicGraph); setLoadedUser(true); } if(user.userRole==='MODERATOR')fetchPostReports(); } catch (error) { setFetchError(true); } }; const fetchPostReports = async () => { try { const response = await axios.get(url2, {withCredentials: true}); var cyclicGraph = await response.data; var jsogStructure = JSOG.encode(cyclicGraph); cyclicGraph = JSOG.decode(jsogStructure); setPostReports(cyclicGraph); setLoadedPostReports(true); } catch (error) { setFetchError(true); } }; if (auth) fetchUser(); }, [user]); // useEffect(() => { // const timer = setTimeout(() => { // if (user === null) window.location.reload(); <---- :-) // }, 3000); // return () => clearTimeout(timer); // }, []); function findParentThread(post) { if (post.parent === null) return post; return findParentThread(post.parent); } const handleEdit = async (e) => { e.preventDefault(); try { if(reportForModal.post !== null && reportForModal.post.targetProfessor !== undefined) { await axios(`http://192.168.0.29:8080/secure/updateOpinion/${reportForModal.post.postId}`, { method: "put", data: { newContent: newPostContent, newTargetProfessorId: reportForModal.post.targetProfessor.professorId, newParentPostId: reportForModal.post.parent !== null ? reportForModal.post.parent.postId : "-1" }, withCredentials: true, }) } else if(reportForModal.post !== null && reportForModal.post.targetProfessor === undefined) { await axios(`http://192.168.0.29:8080/secure/updateThread/${reportForModal.post.postId}`, { method: "put", data: { newTitle: newThreadTitle, newContent: newPostContent, newTargetSubjectId: reportForModal.post.targetSubject.subjectId, newParentThreadId: reportForModal.post.parent !== null ? reportForModal.post.parent.postId : "-1" }, withCredentials: true, }) } await axios(`http://192.168.0.29:8080/secure/markReportResolved/${reportForModal.postReportId}/${markResolved ? `resolve` : `open`}`,{ method: "get", withCredentials: true }) } catch (error) { setFetchError(true); } window.location.reload(); } const handleRelocate = async (e) => { e.preventDefault(); try { if(reportForModal.post !== null && reportForModal.post.targetProfessor !== undefined) { var response = await axios(`http://192.168.0.29:8080/secure/updateOpinion/${reportForModal.post.postId}`, { method: "put", data: { newContent: reportForModal.post.content, newTargetProfessorId: newOpinionTargetProfessorId, newParentPostId: newParentPostId==="Постави како самостојно мислење" ? "-1" : newParentPostId //:) }, withCredentials: true, }) } else if(reportForModal.post !== null && reportForModal.post.targetProfessor === undefined) { var response = await axios(`http://192.168.0.29:8080/secure/updateThread/${reportForModal.post.postId}`, { method: "put", data: { newTitle: newThreadTitle, newContent: reportForModal.post.content, newTargetSubjectId: newTargetSubjectId, newParentThreadId: newParentThreadId==="Постави како самостојно мислење (нова тема)" ? "-1" : newParentThreadId //:) }, withCredentials: true, }) } await axios(`http://192.168.0.29:8080/secure/markReportResolved/${reportForModal.postReportId}/${markResolved ? `resolve` : `open`}`,{ method: "get", withCredentials: true }) } catch (error) { setFetchError(true); } setErrMsg(response.data); if (response.data==="") window.location.reload(); } const handleDelete = async (e) => { e.preventDefault(); try { if(reportForModal.post !== null && reportForModal.post.targetProfessor !== undefined) { await axios(`http://192.168.0.29:8080/secure/deleteOpinion/${reportForModal.post.postId}`, { method: "delete", withCredentials: true, }) window.location.reload(); } else if(reportForModal.post !== null && reportForModal.post.targetProfessor === undefined) { await axios(`http://192.168.0.29:8080/secure/deleteThread/${reportForModal.post.postId}`, { method: "delete", withCredentials: true, }) } } catch (error) { setFetchError(true); } window.location.reload(); } const handleMarkResolved = () => { if (actionType !== 1) setMarkResolved(!markResolved); } return loadedUser ? ( <>
{dateConverter(
new Date(postReport.time).toString().slice(4, -43)
)}
{postReport.description.substring(0,45 )}{postReport.description.length >= 45 ? ("...") : ("")} {postReport.resolved ? (разрешено) : ""}
во дискусија за{" "} {post.targetProfessor !== undefined ? ( {post.targetProfessor.professorName} ) : ( {post.targetSubject.subjectName} )}
{post.content}
Пријавил: {reportForModal.user !== null ? {reportForModal.user.username} : "(избришан корисник)"}
Време на пријава: {dateConverter( new Date(reportForModal.time).toString().slice(4, -43) )}
Образложение:
{reportForModal.description}
Информации за пријавеното мислење:
{reportForModal.post !== null ?во дискусија за{" "} {reportForModal.post.targetProfessor !== undefined ? ( {reportForModal.post.targetProfessor.professorName} ) : ( {reportForModal.post.targetSubject.subjectName} )}
{reportForModal.post.title === null ?{reportForModal.post.author.username} {" "} напишал
:{reportForModal.post.author.username} {" "} отворил тема со наслов {reportForModal.post.title}
}{reportForModal.post.content}