source: reactapp/src/Pages/Professor.js@ 3b6962d

main
Last change on this file since 3b6962d was 3b6962d, checked in by unknown <mlviktor23@…>, 2 years ago

moderation/reporting api in spring boot

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