1 | import React, { useEffect, useState, useContext } from "react";
|
---|
2 | import { Outlet, useParams } from "react-router-dom";
|
---|
3 | import JSOG from "jsog";
|
---|
4 | import OpinionTree from "../Components/OpinionTree";
|
---|
5 | import {
|
---|
6 | AddOpinionButton,
|
---|
7 | Modal,
|
---|
8 | ModalContent,
|
---|
9 | ModalClose,
|
---|
10 | ModalHeader,
|
---|
11 | ModalBody,
|
---|
12 | ModalInput,
|
---|
13 | ModalTextarea,
|
---|
14 | ModalFooter,
|
---|
15 | } from "../Components/Styled/Modal.style";
|
---|
16 | import {
|
---|
17 | ProfessorCard,
|
---|
18 | ProfessorCardDetails,
|
---|
19 | ProfessorCardName,
|
---|
20 | ProfessorCardSeparator,
|
---|
21 | } from "../Components/Styled/ProfessorCard.style";
|
---|
22 | import AuthApi from "../api/AuthApi";
|
---|
23 | import { useNavigate } from "react-router-dom";
|
---|
24 | import axios from "../api/axios";
|
---|
25 | import { CurrentPageNav } from "../Components/Styled/Main.style";
|
---|
26 | import LoadingSpinner from "../Components/Styled/LoadingSpinner.style";
|
---|
27 |
|
---|
28 | function 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 | »{" "}
|
---|
104 | <a href={"/university/" + professor.faculty.university.universityId}>
|
---|
105 | {professor.faculty.university.universityName}
|
---|
106 | </a>{" "}
|
---|
107 | »{" "}
|
---|
108 | <a href={"/faculty/" + professor.faculty.facultyId}>
|
---|
109 | {professor.faculty.facultyName}
|
---|
110 | </a>{" "}
|
---|
111 | » <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}>×</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 |
|
---|
195 | export default Professor;
|
---|