source: reactapp/src/Pages/Professor.js

main
Last change on this file was 8dffe02, checked in by unknown <mlviktor23@…>, 18 months ago

prefinal reproducible

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