source: reactapp/src/Pages/Professor.js@ 8d83180

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

forums/page nav

  • 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(user, userLoaded) {
28 let params = useParams();
29
30 let [professor, setProfessor] = useState(null);
31 let [loaded, setLoaded] = useState(null);
32 let [postModalDisplay, setPostModalDisplay] = useState("none");
33 let navigate = useNavigate();
34 const { auth, setAuth } = useContext(AuthApi);
35 const [postTitle, setPostTitle] = useState("");
36 const [postContent, setPostContent] = useState("");
37 const [fetchError, setFetchError] = useState(false);
38
39 useEffect(() => {
40 const url = `http://192.168.0.17:8080/public/professor/${params.professorId}`;
41
42 const fetchData = async () => {
43 try {
44 const response = await fetch(url);
45 var cyclicGraph = await response.json();
46 var jsogStructure = JSOG.encode(cyclicGraph);
47 cyclicGraph = JSOG.decode(jsogStructure);
48 setProfessor(cyclicGraph);
49 setLoaded(true);
50 } catch (error) {
51 setFetchError(true);
52 }
53 };
54
55 fetchData();
56 }, [params.professorId]);
57
58 const handleAddOpinionButtonClick = () => {
59 if (auth) {
60 setPostModalDisplay("block");
61 } else {
62 navigate("/login");
63 }
64 };
65
66 const handleModalCloseClick = () => {
67 setPostModalDisplay("none");
68 };
69
70 const handlePostSubmit = async (e) => {
71 e.preventDefault();
72
73 const response = await axios(
74 `http://192.168.0.17:8080/secure/professor/${professor.professorId}/addOpinion`,
75 {
76 method: "post",
77 data: {
78 title: postTitle,
79 content: postContent,
80 },
81 withCredentials: true,
82 }
83 );
84
85 window.location.reload(false);
86 };
87
88 const handleTitleChange = (e) => {
89 setPostTitle(e.target.value);
90 };
91
92 const handleContentChange = (e) => {
93 setPostContent(e.target.value);
94 };
95
96 if (loaded) {
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: "30px" }}>
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="title">
149 <b>Наслов</b>:
150 <ModalInput
151 id="title"
152 type="text"
153 value={postTitle}
154 onChange={handleTitleChange}
155 />
156 </label>
157 <label htmlFor="content">
158 <b>Содржина</b>:
159 <ModalTextarea
160 id="content"
161 rows="8"
162 cols="100"
163 value={postContent}
164 onChange={handleContentChange}
165 />
166 </label>
167 </ModalBody>
168 <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
169 </form>
170 </ModalContent>
171 </Modal>
172
173 <div className="opinionTree">
174 <OpinionTree
175 professor={professor}
176 user={user}
177 userLoaded={userLoaded}
178 />
179 </div>
180 <Outlet />
181 </div>
182 );
183 } else if (!fetchError) {
184 return (
185 <div>
186 <p style={{ marginTop: "140px" }}>се вчитува...</p>
187 <Outlet />
188 </div>
189 );
190 } else {
191 return (
192 <div style={{ marginTop: "140px" }}>
193 <h1 style={{ textAlign: "center" }}>Страницата не е пронајдена.</h1>
194 </div>
195 );
196 }
197}
198
199export default Professor;
Note: See TracBrowser for help on using the repository browser.