source: reactapp/src/Pages/Professor.js@ 9bf1f8d

main
Last change on this file since 9bf1f8d was 9bf1f8d, checked in by viktor <viktor@…>, 22 months ago

prefinal

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