[702ca77] | 1 | import React, { useEffect, useState, useContext } from "react";
|
---|
[3a44163] | 2 | import { Outlet, useParams } from "react-router-dom";
|
---|
[2998dc4] | 3 | import JSOG from "jsog";
|
---|
[3a44163] | 4 | import OpinionTree from "../Components/OpinionTree";
|
---|
[702ca77] | 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";
|
---|
[e958037] | 16 | import {
|
---|
| 17 | ProfessorCard,
|
---|
| 18 | ProfessorCardDetails,
|
---|
| 19 | ProfessorCardName,
|
---|
| 20 | ProfessorCardSeparator,
|
---|
[3e98cea] | 21 | } from "../Components/Styled/ProfessorCard.style";
|
---|
[702ca77] | 22 | import AuthApi from "../api/AuthApi";
|
---|
| 23 | import { useNavigate } from "react-router-dom";
|
---|
[6221ab6] | 24 | import axios from "../api/axios";
|
---|
[8d83180] | 25 | import { CurrentPageNav } from "../Components/Styled/Main.style";
|
---|
[7cb8c3c] | 26 |
|
---|
[c68150f] | 27 | function Professor() {
|
---|
[7cb8c3c] | 28 | let params = useParams();
|
---|
[702ca77] | 29 | let navigate = useNavigate();
|
---|
[c68150f] | 30 |
|
---|
| 31 | const [professor, setProfessor] = useState(null);
|
---|
| 32 | const [loadedProfessor, setLoadedProfessor] = useState(false);
|
---|
| 33 |
|
---|
| 34 | const [postModalDisplay, setPostModalDisplay] = useState("none");
|
---|
[702ca77] | 35 | const { auth, setAuth } = useContext(AuthApi);
|
---|
[6221ab6] | 36 | const [postContent, setPostContent] = useState("");
|
---|
[8d83180] | 37 | const [fetchError, setFetchError] = useState(false);
|
---|
[c68150f] | 38 | const [errorMessage, setErrorMessage] = useState("");
|
---|
[7cb8c3c] | 39 |
|
---|
| 40 | useEffect(() => {
|
---|
[62b653f] | 41 | const url = `http://192.168.0.17:8080/public/professor/${params.professorId}`;
|
---|
[7cb8c3c] | 42 |
|
---|
[c68150f] | 43 | const fetchProfessor = async () => {
|
---|
[7cb8c3c] | 44 | try {
|
---|
| 45 | const response = await fetch(url);
|
---|
[2998dc4] | 46 | var cyclicGraph = await response.json();
|
---|
[702ca77] | 47 | var jsogStructure = JSOG.encode(cyclicGraph);
|
---|
[2998dc4] | 48 | cyclicGraph = JSOG.decode(jsogStructure);
|
---|
| 49 | setProfessor(cyclicGraph);
|
---|
[c68150f] | 50 | setLoadedProfessor(true);
|
---|
[7cb8c3c] | 51 | } catch (error) {
|
---|
[8d83180] | 52 | setFetchError(true);
|
---|
[7cb8c3c] | 53 | }
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[c68150f] | 56 | fetchProfessor();
|
---|
[3e98cea] | 57 | }, [params.professorId]);
|
---|
[7cb8c3c] | 58 |
|
---|
[702ca77] | 59 | const handleAddOpinionButtonClick = () => {
|
---|
| 60 | if (auth) {
|
---|
[6221ab6] | 61 | setPostModalDisplay("block");
|
---|
[702ca77] | 62 | } else {
|
---|
| 63 | navigate("/login");
|
---|
| 64 | }
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | const handleModalCloseClick = () => {
|
---|
[6221ab6] | 68 | setPostModalDisplay("none");
|
---|
[702ca77] | 69 | };
|
---|
| 70 |
|
---|
[6221ab6] | 71 | const handlePostSubmit = async (e) => {
|
---|
| 72 | e.preventDefault();
|
---|
| 73 |
|
---|
[c68150f] | 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 | }
|
---|
[702ca77] | 90 | };
|
---|
| 91 |
|
---|
| 92 | const handleContentChange = (e) => {
|
---|
[6221ab6] | 93 | setPostContent(e.target.value);
|
---|
[702ca77] | 94 | };
|
---|
| 95 |
|
---|
[c68150f] | 96 | if (loadedProfessor) {
|
---|
[4a64cf0] | 97 | return (
|
---|
[7cb8c3c] | 98 | <div>
|
---|
[8d83180] | 99 | <CurrentPageNav>
|
---|
| 100 | »{" "}
|
---|
| 101 | <a href={"/university/" + professor.faculty.university.universityId}>
|
---|
| 102 | {professor.faculty.university.universityName}
|
---|
| 103 | </a>{" "}
|
---|
| 104 | »{" "}
|
---|
| 105 | <a href={"/faculty/" + professor.faculty.facultyId}>
|
---|
| 106 | {professor.faculty.facultyName}
|
---|
| 107 | </a>{" "}
|
---|
| 108 | » <a href="#">{professor.professorName}</a>
|
---|
| 109 | </CurrentPageNav>
|
---|
[e958037] | 110 | <ProfessorCard>
|
---|
| 111 | <ProfessorCardName>{professor.professorName}</ProfessorCardName>
|
---|
| 112 | <ProfessorCardSeparator />
|
---|
[5347491] | 113 | <div style={{ marginTop: "10px" }}>
|
---|
[e958037] | 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>
|
---|
[c68150f] | 122 | <div style={{ height: "20px", marginBottom: "50px" }}>
|
---|
[702ca77] | 123 | <h3
|
---|
| 124 | style={{
|
---|
| 125 | float: "left",
|
---|
| 126 | }}
|
---|
| 127 | >
|
---|
| 128 | {professor.relatedOpinions.length}{" "}
|
---|
| 129 | {professor.relatedOpinions.length !== 1 ? "мислења" : "мислење"}
|
---|
| 130 | </h3>
|
---|
[6221ab6] | 131 | {auth && (
|
---|
| 132 | <AddOpinionButton onClick={handleAddOpinionButtonClick}>
|
---|
| 133 | Објави мислење
|
---|
| 134 | </AddOpinionButton>
|
---|
| 135 | )}
|
---|
[702ca77] | 136 | </div>
|
---|
| 137 |
|
---|
[6221ab6] | 138 | <Modal display={postModalDisplay}>
|
---|
[702ca77] | 139 | <ModalContent>
|
---|
| 140 | <ModalHeader>
|
---|
| 141 | <ModalClose onClick={handleModalCloseClick}>×</ModalClose>
|
---|
| 142 | <h3 style={{ marginTop: "5px" }}>
|
---|
| 143 | Мислење за {professor.professorName}
|
---|
| 144 | </h3>
|
---|
| 145 | </ModalHeader>
|
---|
[6221ab6] | 146 | <form onSubmit={handlePostSubmit}>
|
---|
| 147 | <ModalBody>
|
---|
| 148 | <label htmlFor="content">
|
---|
[702ca77] | 149 | <b>Содржина</b>:
|
---|
| 150 | <ModalTextarea
|
---|
| 151 | id="content"
|
---|
| 152 | rows="8"
|
---|
| 153 | cols="100"
|
---|
[6221ab6] | 154 | value={postContent}
|
---|
[702ca77] | 155 | onChange={handleContentChange}
|
---|
| 156 | />
|
---|
| 157 | </label>
|
---|
[6221ab6] | 158 | </ModalBody>
|
---|
[c68150f] | 159 | <p
|
---|
| 160 | style={{ color: "red", marginLeft: "15px", marginTop: "10px" }}
|
---|
| 161 | >
|
---|
| 162 | {errorMessage}
|
---|
| 163 | </p>
|
---|
[6221ab6] | 164 | <ModalFooter type="submit">ОБЈАВИ</ModalFooter>
|
---|
| 165 | </form>
|
---|
[702ca77] | 166 | </ModalContent>
|
---|
| 167 | </Modal>
|
---|
| 168 |
|
---|
[3a44163] | 169 | <div className="opinionTree">
|
---|
[c68150f] | 170 | <OpinionTree professor={professor} />
|
---|
[3a44163] | 171 | </div>
|
---|
[4a64cf0] | 172 | <Outlet />
|
---|
[7cb8c3c] | 173 | </div>
|
---|
[4a64cf0] | 174 | );
|
---|
[8d83180] | 175 | } else if (!fetchError) {
|
---|
[4a64cf0] | 176 | return (
|
---|
| 177 | <div>
|
---|
[0df6b9e] | 178 | <p style={{ marginTop: "140px" }}>се вчитува...</p>
|
---|
[4a64cf0] | 179 | <Outlet />
|
---|
| 180 | </div>
|
---|
| 181 | );
|
---|
[8d83180] | 182 | } else {
|
---|
| 183 | return (
|
---|
| 184 | <div style={{ marginTop: "140px" }}>
|
---|
| 185 | <h1 style={{ textAlign: "center" }}>Страницата не е пронајдена.</h1>
|
---|
| 186 | </div>
|
---|
| 187 | );
|
---|
[4a64cf0] | 188 | }
|
---|
[7cb8c3c] | 189 | }
|
---|
| 190 |
|
---|
| 191 | export default Professor;
|
---|