[c68150f] | 1 | import React, { useEffect, useState, useContext } from "react";
|
---|
[702ca77] | 2 | import {
|
---|
| 3 | OpinionCard,
|
---|
| 4 | OpinionCardContent,
|
---|
| 5 | OpinionCardContentTime,
|
---|
| 6 | } from "../Components/Styled/OpinionCard.style";
|
---|
| 7 | import {
|
---|
| 8 | UserDetailsCard,
|
---|
| 9 | UserDetailsCardContent,
|
---|
| 10 | } from "../Components/Styled/UserDetails.style";
|
---|
| 11 | import { dateConverter } from "../Util/dateConverter";
|
---|
[c68150f] | 12 | import axios from "../api/axios";
|
---|
| 13 | import JSOG from "jsog";
|
---|
| 14 | import AuthApi from "../api/AuthApi";
|
---|
| 15 |
|
---|
| 16 | function UserDashboard() {
|
---|
| 17 | const { auth, setAuth } = useContext(AuthApi);
|
---|
| 18 |
|
---|
| 19 | const [user, setUser] = useState(null);
|
---|
| 20 | const [loadedUser, setLoadedUser] = useState(false);
|
---|
| 21 | const [fetchError, setFetchError] = useState(false);
|
---|
[702ca77] | 22 |
|
---|
| 23 | useEffect(() => {
|
---|
[c68150f] | 24 | const url = `http://192.168.0.17:8080/secure/currentUser`;
|
---|
| 25 |
|
---|
| 26 | const fetchUser = async () => {
|
---|
| 27 | try {
|
---|
| 28 | const response = await axios.get(url, { withCredentials: true });
|
---|
| 29 | var cyclicGraph = await response.data;
|
---|
| 30 | var jsogStructure = JSOG.encode(cyclicGraph);
|
---|
| 31 | cyclicGraph = JSOG.decode(jsogStructure);
|
---|
| 32 | setUser(cyclicGraph);
|
---|
| 33 | setLoadedUser(true);
|
---|
| 34 | } catch (error) {
|
---|
| 35 | setFetchError(true);
|
---|
| 36 | }
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | if (auth) fetchUser();
|
---|
[702ca77] | 40 | }, []);
|
---|
| 41 |
|
---|
[c68150f] | 42 | // useEffect(() => {
|
---|
| 43 | // const timer = setTimeout(() => {
|
---|
| 44 | // if (user === null) window.location.reload(false); <---- :-)
|
---|
| 45 | // }, 3000);
|
---|
| 46 | // return () => clearTimeout(timer);
|
---|
| 47 | // }, []);
|
---|
| 48 |
|
---|
| 49 | function findParentThread(post) {
|
---|
| 50 | if (post.parent === null) return post;
|
---|
| 51 | return findParentThread(post.parent);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | return loadedUser ? (
|
---|
[702ca77] | 55 | <>
|
---|
| 56 | <h3>Кориснички податоци:</h3>
|
---|
| 57 | <UserDetailsCard>
|
---|
[cae16b5] | 58 | {user.fullName && (
|
---|
| 59 | <UserDetailsCardContent>
|
---|
| 60 | <b>Име:</b> {user.fullName}{" "}
|
---|
| 61 | </UserDetailsCardContent>
|
---|
| 62 | )}
|
---|
[702ca77] | 63 | <UserDetailsCardContent>
|
---|
| 64 | <b>Корисничко име:</b> {user.username}{" "}
|
---|
| 65 | </UserDetailsCardContent>
|
---|
| 66 | <UserDetailsCardContent>
|
---|
| 67 | <b>E-mail:</b> {user.email}
|
---|
| 68 | </UserDetailsCardContent>
|
---|
| 69 | <UserDetailsCardContent>
|
---|
| 70 | <b>Карма:</b> {user.karma}
|
---|
| 71 | </UserDetailsCardContent>
|
---|
| 72 | </UserDetailsCard>
|
---|
| 73 | {user.authoredPosts.length > 0 ? (
|
---|
| 74 | <h3 style={{ marginBottom: "10px" }}>Ваши мислења:</h3>
|
---|
| 75 | ) : (
|
---|
| 76 | <h3>Немате објавени мислења</h3>
|
---|
| 77 | )}
|
---|
| 78 | {user.authoredPosts.map((post) => {
|
---|
| 79 | return (
|
---|
| 80 | <div key={post.postId}>
|
---|
| 81 | <OpinionCard>
|
---|
| 82 | <OpinionCardContent>
|
---|
[c68150f] | 83 | <p style={{ fontStyle: "italic", marginBottom: "10px" }}>
|
---|
| 84 | во дискусија за{" "}
|
---|
| 85 | {post.targetProfessor !== undefined ? (
|
---|
| 86 | <a href={"/professor/" + post.targetProfessor.professorId}>
|
---|
| 87 | {post.targetProfessor.professorName}
|
---|
| 88 | </a>
|
---|
| 89 | ) : (
|
---|
| 90 | <a
|
---|
| 91 | href={
|
---|
| 92 | post.parent === null
|
---|
| 93 | ? "/topic/" + post.postId
|
---|
| 94 | : "/topic/" + findParentThread(post).postId
|
---|
| 95 | }
|
---|
| 96 | >
|
---|
| 97 | {post.targetSubject.subjectName}
|
---|
| 98 | </a>
|
---|
| 99 | )}
|
---|
[702ca77] | 100 | </p>
|
---|
[c68150f] | 101 | <p style={{ marginBottom: "10px" }}>{post.content}</p>
|
---|
[702ca77] | 102 | <OpinionCardContentTime>
|
---|
| 103 | {dateConverter(
|
---|
| 104 | new Date(post.timePosted).toString().slice(4, -43)
|
---|
| 105 | )}
|
---|
| 106 | </OpinionCardContentTime>
|
---|
| 107 | </OpinionCardContent>
|
---|
| 108 | </OpinionCard>
|
---|
| 109 | </div>
|
---|
| 110 | );
|
---|
| 111 | })}
|
---|
| 112 | </>
|
---|
| 113 | ) : (
|
---|
| 114 | <>се вчитува...</>
|
---|
| 115 | );
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | export default UserDashboard;
|
---|