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

main
Last change on this file since c68150f was c68150f, checked in by unknown <mlviktor23@…>, 21 months ago

left: moderation, oAuth, messaging

  • Property mode set to 100644
File size: 5.6 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.17: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 } else {
63 navigate("/login");
64 }
65 };
66
67 const handleModalCloseClick = () => {
68 setPostModalDisplay("none");
69 };
70
71 const handlePostSubmit = async (e) => {
72 e.preventDefault();
73
74 if (!postContent.length < 1) {
75 const response = await axios(
76 `http://192.168.0.17:8080/secure/professor/${params.professorId}/addOpinion`,
77 {
78 method: "post",
79 data: {
80 content: postContent,
81 },
82 withCredentials: true,
83 }
84 );
85 setErrorMessage("");
86 window.location.reload(false);
87 } else {
88 setErrorMessage("Полето за содржина не смее да биде празно");
89 }
90 };
91
92 const handleContentChange = (e) => {
93 setPostContent(e.target.value);
94 };
95
96 if (loadedProfessor) {
97 return (
98 <div>
99 <CurrentPageNav>
100 &#187;{" "}
101 <a href={"/university/" + professor.faculty.university.universityId}>
102 {professor.faculty.university.universityName}
103 </a>{" "}
104 &#187;{" "}
105 <a href={"/faculty/" + professor.faculty.facultyId}>
106 {professor.faculty.facultyName}
107 </a>{" "}
108 &#187; <a href="#">{professor.professorName}</a>
109 </CurrentPageNav>
110 <ProfessorCard>
111 <ProfessorCardName>{professor.professorName}</ProfessorCardName>
112 <ProfessorCardSeparator />
113 <div style={{ marginTop: "10px" }}>
114 <ProfessorCardDetails fontSize="20px">
115 {professor.faculty.facultyName}
116 </ProfessorCardDetails>
117 <ProfessorCardDetails fontSize="15px">
118 {professor.faculty.university.universityName}
119 </ProfessorCardDetails>
120 </div>
121 </ProfessorCard>
122 <div style={{ height: "20px", marginBottom: "50px" }}>
123 <h3
124 style={{
125 float: "left",
126 }}
127 >
128 {professor.relatedOpinions.length}{" "}
129 {professor.relatedOpinions.length !== 1 ? "мислења" : "мислење"}
130 </h3>
131 {auth && (
132 <AddOpinionButton onClick={handleAddOpinionButtonClick}>
133 Објави мислење
134 </AddOpinionButton>
135 )}
136 </div>
137
138 <Modal display={postModalDisplay}>
139 <ModalContent>
140 <ModalHeader>
141 <ModalClose onClick={handleModalCloseClick}>&times;</ModalClose>
142 <h3 style={{ marginTop: "5px" }}>
143 Мислење за {professor.professorName}
144 </h3>
145 </ModalHeader>
146 <form onSubmit={handlePostSubmit}>
147 <ModalBody>
148 <label htmlFor="content">
149 <b>Содржина</b>:
150 <ModalTextarea
151 id="content"
152 rows="8"
153 cols="100"
154 value={postContent}
155 onChange={handleContentChange}
156 />
157 </label>
158 </ModalBody>
159 <p
160 style={{ color: "red", marginLeft: "15px", marginTop: "10px" }}
161 >
162 {errorMessage}
163 </p>
164 <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
165 </form>
166 </ModalContent>
167 </Modal>
168
169 <div className="opinionTree">
170 <OpinionTree professor={professor} />
171 </div>
172 <Outlet />
173 </div>
174 );
175 } else if (!fetchError) {
176 return (
177 <div>
178 <p style={{ marginTop: "140px" }}>се вчитува...</p>
179 <Outlet />
180 </div>
181 );
182 } else {
183 return (
184 <div style={{ marginTop: "140px" }}>
185 <h1 style={{ textAlign: "center" }}>Страницата не е пронајдена.</h1>
186 </div>
187 );
188 }
189}
190
191export default Professor;
Note: See TracBrowser for help on using the repository browser.