source: reactapp/src/Pages/Professor.js@ af801e3

main
Last change on this file since af801e3 was af801e3, checked in by viktor <viktor@…>, 19 months ago

finished edit/delete/displace opinion/thread from report (react); todo reporting user/opinion/thread interface, public user pages and messaging (springboot)

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[702ca77]1import React, { useEffect, useState, useContext } from "react";
[3a44163]2import { Outlet, useParams } from "react-router-dom";
[2998dc4]3import JSOG from "jsog";
[3a44163]4import OpinionTree from "../Components/OpinionTree";
[702ca77]5import {
6 AddOpinionButton,
7 Modal,
8 ModalContent,
9 ModalClose,
10 ModalHeader,
11 ModalBody,
12 ModalInput,
13 ModalTextarea,
14 ModalFooter,
15} from "../Components/Styled/Modal.style";
[e958037]16import {
17 ProfessorCard,
18 ProfessorCardDetails,
19 ProfessorCardName,
20 ProfessorCardSeparator,
[3e98cea]21} from "../Components/Styled/ProfessorCard.style";
[702ca77]22import AuthApi from "../api/AuthApi";
23import { useNavigate } from "react-router-dom";
[6221ab6]24import axios from "../api/axios";
[8d83180]25import { CurrentPageNav } from "../Components/Styled/Main.style";
[af801e3]26import LoadingSpinner from "../Components/Styled/LoadingSpinner.style";
[7cb8c3c]27
[c68150f]28function Professor() {
[7cb8c3c]29 let params = useParams();
[702ca77]30 let navigate = useNavigate();
[c68150f]31
32 const [professor, setProfessor] = useState(null);
33 const [loadedProfessor, setLoadedProfessor] = useState(false);
34
35 const [postModalDisplay, setPostModalDisplay] = useState("none");
[702ca77]36 const { auth, setAuth } = useContext(AuthApi);
[6221ab6]37 const [postContent, setPostContent] = useState("");
[8d83180]38 const [fetchError, setFetchError] = useState(false);
[c68150f]39 const [errorMessage, setErrorMessage] = useState("");
[7cb8c3c]40
41 useEffect(() => {
[af801e3]42 const url = `http://192.168.0.29:8080/public/professor/${params.professorId}`;
[7cb8c3c]43
[c68150f]44 const fetchProfessor = async () => {
[7cb8c3c]45 try {
46 const response = await fetch(url);
[2998dc4]47 var cyclicGraph = await response.json();
[702ca77]48 var jsogStructure = JSOG.encode(cyclicGraph);
[2998dc4]49 cyclicGraph = JSOG.decode(jsogStructure);
50 setProfessor(cyclicGraph);
[c68150f]51 setLoadedProfessor(true);
[7cb8c3c]52 } catch (error) {
[8d83180]53 setFetchError(true);
[7cb8c3c]54 }
55 };
56
[c68150f]57 fetchProfessor();
[3e98cea]58 }, [params.professorId]);
[7cb8c3c]59
[702ca77]60 const handleAddOpinionButtonClick = () => {
61 if (auth) {
[6221ab6]62 setPostModalDisplay("block");
[3b6962d]63 document.body.style.overflowY = "hidden";
[702ca77]64 } else {
65 navigate("/login");
66 }
67 };
68
69 const handleModalCloseClick = () => {
[6221ab6]70 setPostModalDisplay("none");
[3b6962d]71 document.body.style.overflowY = "auto";
[702ca77]72 };
73
[6221ab6]74 const handlePostSubmit = async (e) => {
75 e.preventDefault();
76
[c68150f]77 if (!postContent.length < 1) {
78 const response = await axios(
[af801e3]79 `http://192.168.0.29:8080/secure/professor/${params.professorId}/addOpinion`,
[c68150f]80 {
81 method: "post",
82 data: {
83 content: postContent,
84 },
85 withCredentials: true,
86 }
87 );
88 setErrorMessage("");
[af801e3]89 window.location.reload();
[c68150f]90 } else {
91 setErrorMessage("Полето за содржина не смее да биде празно");
92 }
[702ca77]93 };
94
95 const handleContentChange = (e) => {
[6221ab6]96 setPostContent(e.target.value);
[702ca77]97 };
98
[c68150f]99 if (loadedProfessor) {
[4a64cf0]100 return (
[7cb8c3c]101 <div>
[8d83180]102 <CurrentPageNav>
103 &#187;{" "}
104 <a href={"/university/" + professor.faculty.university.universityId}>
105 {professor.faculty.university.universityName}
106 </a>{" "}
107 &#187;{" "}
108 <a href={"/faculty/" + professor.faculty.facultyId}>
109 {professor.faculty.facultyName}
110 </a>{" "}
111 &#187; <a href="#">{professor.professorName}</a>
112 </CurrentPageNav>
[e958037]113 <ProfessorCard>
[af801e3]114 <ProfessorCardName>{professor.professorName} <span style={{opacity:"50%", fontSize:"16px"}}>#{professor.professorId}</span></ProfessorCardName>
[e958037]115 <ProfessorCardSeparator />
[5347491]116 <div style={{ marginTop: "10px" }}>
[e958037]117 <ProfessorCardDetails fontSize="20px">
118 {professor.faculty.facultyName}
119 </ProfessorCardDetails>
120 <ProfessorCardDetails fontSize="15px">
121 {professor.faculty.university.universityName}
122 </ProfessorCardDetails>
123 </div>
124 </ProfessorCard>
[c68150f]125 <div style={{ height: "20px", marginBottom: "50px" }}>
[702ca77]126 <h3
127 style={{
128 float: "left",
129 }}
130 >
131 {professor.relatedOpinions.length}{" "}
132 {professor.relatedOpinions.length !== 1 ? "мислења" : "мислење"}
133 </h3>
[6221ab6]134 {auth && (
135 <AddOpinionButton onClick={handleAddOpinionButtonClick}>
136 Објави мислење
137 </AddOpinionButton>
138 )}
[702ca77]139 </div>
140
[6221ab6]141 <Modal display={postModalDisplay}>
[702ca77]142 <ModalContent>
143 <ModalHeader>
144 <ModalClose onClick={handleModalCloseClick}>&times;</ModalClose>
145 <h3 style={{ marginTop: "5px" }}>
146 Мислење за {professor.professorName}
147 </h3>
148 </ModalHeader>
[6221ab6]149 <form onSubmit={handlePostSubmit}>
150 <ModalBody>
151 <label htmlFor="content">
[702ca77]152 <b>Содржина</b>:
153 <ModalTextarea
154 id="content"
155 rows="8"
156 cols="100"
[3b6962d]157 spellCheck={false}
[6221ab6]158 value={postContent}
[702ca77]159 onChange={handleContentChange}
160 />
161 </label>
[6221ab6]162 </ModalBody>
[c68150f]163 <p
164 style={{ color: "red", marginLeft: "15px", marginTop: "10px" }}
165 >
166 {errorMessage}
167 </p>
[6221ab6]168 <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
169 </form>
[702ca77]170 </ModalContent>
171 </Modal>
172
[3a44163]173 <div className="opinionTree">
[c68150f]174 <OpinionTree professor={professor} />
[3a44163]175 </div>
[4a64cf0]176 <Outlet />
[7cb8c3c]177 </div>
[4a64cf0]178 );
[8d83180]179 } else if (!fetchError) {
[4a64cf0]180 return (
181 <div>
[af801e3]182 <LoadingSpinner style={{ marginTop: "140px" }}/>
[4a64cf0]183 <Outlet />
184 </div>
185 );
[8d83180]186 } else {
187 return (
188 <div style={{ marginTop: "140px" }}>
189 <h1 style={{ textAlign: "center" }}>Страницата не е пронајдена.</h1>
190 </div>
191 );
[4a64cf0]192 }
[7cb8c3c]193}
194
195export default Professor;
Note: See TracBrowser for help on using the repository browser.