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

main
Last change on this file since a5aba17 was af801e3, checked in by viktor <viktor@…>, 2 years 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
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
35 const [postModalDisplay, setPostModalDisplay] = useState("none");
36 const { auth, setAuth } = useContext(AuthApi);
37 const [postContent, setPostContent] = useState("");
38 const [fetchError, setFetchError] = useState(false);
39 const [errorMessage, setErrorMessage] = useState("");
40
41 useEffect(() => {
42 const url = `http://192.168.0.29:8080/public/professor/${params.professorId}`;
43
44 const fetchProfessor = async () => {
45 try {
46 const response = await fetch(url);
47 var cyclicGraph = await response.json();
48 var jsogStructure = JSOG.encode(cyclicGraph);
49 cyclicGraph = JSOG.decode(jsogStructure);
50 setProfessor(cyclicGraph);
51 setLoadedProfessor(true);
52 } catch (error) {
53 setFetchError(true);
54 }
55 };
56
57 fetchProfessor();
58 }, [params.professorId]);
59
60 const handleAddOpinionButtonClick = () => {
61 if (auth) {
62 setPostModalDisplay("block");
63 document.body.style.overflowY = "hidden";
64 } else {
65 navigate("/login");
66 }
67 };
68
69 const handleModalCloseClick = () => {
70 setPostModalDisplay("none");
71 document.body.style.overflowY = "auto";
72 };
73
74 const handlePostSubmit = async (e) => {
75 e.preventDefault();
76
77 if (!postContent.length < 1) {
78 const response = await axios(
79 `http://192.168.0.29:8080/secure/professor/${params.professorId}/addOpinion`,
80 {
81 method: "post",
82 data: {
83 content: postContent,
84 },
85 withCredentials: true,
86 }
87 );
88 setErrorMessage("");
89 window.location.reload();
90 } else {
91 setErrorMessage("Полето за содржина не смее да биде празно");
92 }
93 };
94
95 const handleContentChange = (e) => {
96 setPostContent(e.target.value);
97 };
98
99 if (loadedProfessor) {
100 return (
101 <div>
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>
113 <ProfessorCard>
114 <ProfessorCardName>{professor.professorName} <span style={{opacity:"50%", fontSize:"16px"}}>#{professor.professorId}</span></ProfessorCardName>
115 <ProfessorCardSeparator />
116 <div style={{ marginTop: "10px" }}>
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>
125 <div style={{ height: "20px", marginBottom: "50px" }}>
126 <h3
127 style={{
128 float: "left",
129 }}
130 >
131 {professor.relatedOpinions.length}{" "}
132 {professor.relatedOpinions.length !== 1 ? "мислења" : "мислење"}
133 </h3>
134 {auth && (
135 <AddOpinionButton onClick={handleAddOpinionButtonClick}>
136 Објави мислење
137 </AddOpinionButton>
138 )}
139 </div>
140
141 <Modal display={postModalDisplay}>
142 <ModalContent>
143 <ModalHeader>
144 <ModalClose onClick={handleModalCloseClick}>&times;</ModalClose>
145 <h3 style={{ marginTop: "5px" }}>
146 Мислење за {professor.professorName}
147 </h3>
148 </ModalHeader>
149 <form onSubmit={handlePostSubmit}>
150 <ModalBody>
151 <label htmlFor="content">
152 <b>Содржина</b>:
153 <ModalTextarea
154 id="content"
155 rows="8"
156 cols="100"
157 spellCheck={false}
158 value={postContent}
159 onChange={handleContentChange}
160 />
161 </label>
162 </ModalBody>
163 <p
164 style={{ color: "red", marginLeft: "15px", marginTop: "10px" }}
165 >
166 {errorMessage}
167 </p>
168 <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
169 </form>
170 </ModalContent>
171 </Modal>
172
173 <div className="opinionTree">
174 <OpinionTree professor={professor} />
175 </div>
176 <Outlet />
177 </div>
178 );
179 } else if (!fetchError) {
180 return (
181 <div>
182 <LoadingSpinner style={{ marginTop: "140px" }}/>
183 <Outlet />
184 </div>
185 );
186 } else {
187 return (
188 <div style={{ marginTop: "140px" }}>
189 <h1 style={{ textAlign: "center" }}>Страницата не е пронајдена.</h1>
190 </div>
191 );
192 }
193}
194
195export default Professor;
Note: See TracBrowser for help on using the repository browser.