source: reactapp/src/Pages/Professor.js@ 62b653f

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

implemented upvote/downvote func. in react

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[702ca77]1import React, { useEffect, useState, useContext } from "react";
[3a44163]2import { Outlet, useParams } from "react-router-dom";
[2998dc4]3import JSOG from "jsog";
[3a44163]4import OpinionTree from "../Components/OpinionTree";
[702ca77]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";
[e958037]16import {
17 ProfessorCard,
18 ProfessorCardDetails,
19 ProfessorCardName,
20 ProfessorCardSeparator,
[3e98cea]21} from "../Components/Styled/ProfessorCard.style";
[702ca77]22import AuthApi from "../api/AuthApi";
23import { useNavigate } from "react-router-dom";
[6221ab6]24import axios from "../api/axios";
[7cb8c3c]25
[6221ab6]26function Professor(user, userLoaded) {
[7cb8c3c]27 let params = useParams();
28
[4a64cf0]29 let [professor, setProfessor] = useState(null);
30 let [loaded, setLoaded] = useState(null);
[6221ab6]31 let [postModalDisplay, setPostModalDisplay] = useState("none");
[702ca77]32 let navigate = useNavigate();
33 const { auth, setAuth } = useContext(AuthApi);
[6221ab6]34 const [postTitle, setPostTitle] = useState("");
35 const [postContent, setPostContent] = useState("");
[7cb8c3c]36
37 useEffect(() => {
[62b653f]38 const url = `http://192.168.0.17:8080/public/professor/${params.professorId}`;
[7cb8c3c]39
40 const fetchData = async () => {
41 try {
42 const response = await fetch(url);
[2998dc4]43 var cyclicGraph = await response.json();
[702ca77]44 var jsogStructure = JSOG.encode(cyclicGraph);
[2998dc4]45 cyclicGraph = JSOG.decode(jsogStructure);
46 setProfessor(cyclicGraph);
[4a64cf0]47 setLoaded(true);
[7cb8c3c]48 } catch (error) {
[702ca77]49 console.log("Fetching error", error);
[7cb8c3c]50 }
51 };
52
53 fetchData();
[3e98cea]54 }, [params.professorId]);
[7cb8c3c]55
[702ca77]56 const handleAddOpinionButtonClick = () => {
57 if (auth) {
[6221ab6]58 setPostModalDisplay("block");
[702ca77]59 } else {
60 navigate("/login");
61 }
62 };
63
64 const handleModalCloseClick = () => {
[6221ab6]65 setPostModalDisplay("none");
[702ca77]66 };
67
[6221ab6]68 const handlePostSubmit = async (e) => {
69 e.preventDefault();
70
71 const response = await axios(
[62b653f]72 `http://192.168.0.17:8080/secure/professor/${professor.professorId}/addOpinion`,
[6221ab6]73 {
74 method: "post",
75 data: {
76 title: postTitle,
77 content: postContent,
78 },
79 withCredentials: true,
80 }
81 );
82
83 window.location.reload(false);
84 };
[702ca77]85
86 const handleTitleChange = (e) => {
[6221ab6]87 setPostTitle(e.target.value);
[702ca77]88 };
89
90 const handleContentChange = (e) => {
[6221ab6]91 setPostContent(e.target.value);
[702ca77]92 };
93
[4a64cf0]94 if (loaded) {
95 return (
[7cb8c3c]96 <div>
[e958037]97 <ProfessorCard>
98 <ProfessorCardName>{professor.professorName}</ProfessorCardName>
99 <ProfessorCardSeparator />
[5347491]100 <div style={{ marginTop: "10px" }}>
[e958037]101 <ProfessorCardDetails fontSize="20px">
102 {professor.faculty.facultyName}
103 </ProfessorCardDetails>
104 <ProfessorCardDetails fontSize="15px">
105 {professor.faculty.university.universityName}
106 </ProfessorCardDetails>
107 </div>
108 </ProfessorCard>
[702ca77]109 <div style={{ height: "20px", marginBottom: "30px" }}>
110 <h3
111 style={{
112 float: "left",
113 }}
114 >
115 {professor.relatedOpinions.length}{" "}
116 {professor.relatedOpinions.length !== 1 ? "мислења" : "мислење"}
117 </h3>
[6221ab6]118 {auth && (
119 <AddOpinionButton onClick={handleAddOpinionButtonClick}>
120 Објави мислење
121 </AddOpinionButton>
122 )}
[702ca77]123 </div>
124
[6221ab6]125 <Modal display={postModalDisplay}>
[702ca77]126 <ModalContent>
127 <ModalHeader>
128 <ModalClose onClick={handleModalCloseClick}>&times;</ModalClose>
129 <h3 style={{ marginTop: "5px" }}>
130 Мислење за {professor.professorName}
131 </h3>
132 </ModalHeader>
[6221ab6]133 <form onSubmit={handlePostSubmit}>
134 <ModalBody>
135 <label htmlFor="title">
[702ca77]136 <b>Наслов</b>:
137 <ModalInput
138 id="title"
139 type="text"
[6221ab6]140 value={postTitle}
[702ca77]141 onChange={handleTitleChange}
142 />
143 </label>
[6221ab6]144 <label htmlFor="content">
[702ca77]145 <b>Содржина</b>:
146 <ModalTextarea
147 id="content"
148 rows="8"
149 cols="100"
[6221ab6]150 value={postContent}
[702ca77]151 onChange={handleContentChange}
152 />
153 </label>
[6221ab6]154 </ModalBody>
155 <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
156 </form>
[702ca77]157 </ModalContent>
158 </Modal>
159
[3a44163]160 <div className="opinionTree">
[6221ab6]161 <OpinionTree
162 professor={professor}
163 user={user}
164 userLoaded={userLoaded}
165 />
[3a44163]166 </div>
[4a64cf0]167 <Outlet />
[7cb8c3c]168 </div>
[4a64cf0]169 );
170 } else {
171 return (
172 <div>
[0df6b9e]173 <p style={{ marginTop: "140px" }}>се вчитува...</p>
[4a64cf0]174 <Outlet />
175 </div>
176 );
177 }
[7cb8c3c]178}
179
180export default Professor;
Note: See TracBrowser for help on using the repository browser.