| 1 | import React, { useState, useEffect } from "react";
|
|---|
| 2 | import axios from "axios";
|
|---|
| 3 | import { useParams, Link } from "react-router-dom";
|
|---|
| 4 | import styles from "./AdminPanelSoopstenijaViewPageStyles.module.css";
|
|---|
| 5 | import Navbar from "../../../components/nav/Navbar.jsx";
|
|---|
| 6 |
|
|---|
| 7 | const AdminPanelSoopstenijaViewPage = () => {
|
|---|
| 8 | const { classId } = useParams();
|
|---|
| 9 | const [soopstenija, setSoopstenija] = useState([]);
|
|---|
| 10 | const [teachersMap, setTeachersMap] = useState({});
|
|---|
| 11 | const [loading, setLoading] = useState(true);
|
|---|
| 12 | const [classInfo, setClassInfo] = useState(null);
|
|---|
| 13 |
|
|---|
| 14 | useEffect(() => {
|
|---|
| 15 | const fetchAllData = async () => {
|
|---|
| 16 | try {
|
|---|
| 17 | setLoading(true);
|
|---|
| 18 |
|
|---|
| 19 | const [resSoopstenija, resClassInfo] = await Promise.all([
|
|---|
| 20 | axios.get(`http://localhost:8080/api/klas/${classId}/soopstenija`, {
|
|---|
| 21 | withCredentials: true,
|
|---|
| 22 | }),
|
|---|
| 23 | axios.get(`http://localhost:8080/api/klas/${classId}`, {
|
|---|
| 24 | withCredentials: true,
|
|---|
| 25 | }),
|
|---|
| 26 | ]);
|
|---|
| 27 | const soopstenijaData = resSoopstenija.data;
|
|---|
| 28 |
|
|---|
| 29 | setClassInfo(resClassInfo.data);
|
|---|
| 30 | setSoopstenija(resSoopstenija.data);
|
|---|
| 31 |
|
|---|
| 32 | const uniqueAuthorIds = [
|
|---|
| 33 | ...new Set(soopstenijaData.map((s) => s.postiranoOdId)),
|
|---|
| 34 | ];
|
|---|
| 35 |
|
|---|
| 36 | const teachersDetails = await Promise.all(
|
|---|
| 37 | uniqueAuthorIds.map((id) =>
|
|---|
| 38 | axios
|
|---|
| 39 | .get(
|
|---|
| 40 | `http://localhost:8080/api/users/nastavnici/klasniRakovoditeli/${id}`,
|
|---|
| 41 | { withCredentials: true }
|
|---|
| 42 | )
|
|---|
| 43 | .then((res) => ({ id, data: res.data }))
|
|---|
| 44 | .catch(() => ({ id, data: null }))
|
|---|
| 45 | )
|
|---|
| 46 | );
|
|---|
| 47 |
|
|---|
| 48 | const newTeachersMap = {};
|
|---|
| 49 | teachersDetails.forEach((item) => {
|
|---|
| 50 | if (item.data) {
|
|---|
| 51 | newTeachersMap[item.id] = `${item.data.ime} ${item.data.prezime}`;
|
|---|
| 52 | } else {
|
|---|
| 53 | newTeachersMap[item.id] = "Непознат Наставник";
|
|---|
| 54 | }
|
|---|
| 55 | });
|
|---|
| 56 |
|
|---|
| 57 | setTeachersMap(newTeachersMap);
|
|---|
| 58 | } catch (err) {
|
|---|
| 59 | console.error("Грешка при вчитување податоци:", err);
|
|---|
| 60 | } finally {
|
|---|
| 61 | setLoading(false);
|
|---|
| 62 | }
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | if (classId) fetchAllData();
|
|---|
| 66 | }, [classId]);
|
|---|
| 67 |
|
|---|
| 68 | if (loading)
|
|---|
| 69 | return (
|
|---|
| 70 | <div className={styles.wrapper}>
|
|---|
| 71 | <Navbar />
|
|---|
| 72 |
|
|---|
| 73 | <div className={styles.loader}>
|
|---|
| 74 | Вчитување на соопштенијата и авторите...
|
|---|
| 75 | </div>
|
|---|
| 76 | </div>
|
|---|
| 77 | );
|
|---|
| 78 |
|
|---|
| 79 | return (
|
|---|
| 80 | <div className={styles.wrapper}>
|
|---|
| 81 | <Navbar />
|
|---|
| 82 | <div
|
|---|
| 83 | className="d-flex flex-row justify-content-center align-items-center p-2"
|
|---|
| 84 | style={{ backgroundColor: "#147176" }}
|
|---|
| 85 | >
|
|---|
| 86 | <Link
|
|---|
| 87 | to="/adminDashboard/users"
|
|---|
| 88 | className="mx-3 text-white text-decoration-none fs-6 opacity-100-hover"
|
|---|
| 89 | >
|
|---|
| 90 | Корисници
|
|---|
| 91 | </Link>
|
|---|
| 92 |
|
|---|
| 93 | <Link
|
|---|
| 94 | to="/adminDashboard/classes"
|
|---|
| 95 | className="mx-3 text-white text-decoration-none fs-6"
|
|---|
| 96 | >
|
|---|
| 97 | Класови
|
|---|
| 98 | </Link>
|
|---|
| 99 |
|
|---|
| 100 | <Link
|
|---|
| 101 | to="/adminDashboard/subjects"
|
|---|
| 102 | className="mx-3 text-white text-decoration-none fs-6"
|
|---|
| 103 | >
|
|---|
| 104 | Предмети
|
|---|
| 105 | </Link>
|
|---|
| 106 |
|
|---|
| 107 | <Link
|
|---|
| 108 | to="/adminDashboard/schedules"
|
|---|
| 109 | className="mx-3 text-white text-decoration-none fs-6"
|
|---|
| 110 | >
|
|---|
| 111 | Распореди
|
|---|
| 112 | </Link>
|
|---|
| 113 |
|
|---|
| 114 | <Link
|
|---|
| 115 | to="/adminDashboard/weeklyPlans"
|
|---|
| 116 | className="mx-3 text-white text-decoration-none fs-6"
|
|---|
| 117 | >
|
|---|
| 118 | Неделни планови
|
|---|
| 119 | </Link>
|
|---|
| 120 | </div>
|
|---|
| 121 | <div className={styles.container}>
|
|---|
| 122 | <h1>
|
|---|
| 123 | Соопштенија за клас {classInfo.oddelenie} - {classInfo.paralelka}
|
|---|
| 124 | </h1>
|
|---|
| 125 | <div className={styles.feed}>
|
|---|
| 126 | {soopstenija.length > 0 ? (
|
|---|
| 127 | soopstenija.map((s) => (
|
|---|
| 128 | <div key={s.id} className={styles.feedCard}>
|
|---|
| 129 | <div className={styles.cardHeader}>
|
|---|
| 130 | <div className={styles.avatarPlaceholder}></div>
|
|---|
| 131 | <div className={styles.authorInfo}>
|
|---|
| 132 | <span className={styles.authorName}>
|
|---|
| 133 | {teachersMap[s.postiranoOdId] || "Вчитување..."}
|
|---|
| 134 | </span>
|
|---|
| 135 | </div>
|
|---|
| 136 | </div>
|
|---|
| 137 |
|
|---|
| 138 | <div className={styles.cardBody}>
|
|---|
| 139 | <p className={styles.postContent}>{s.sodrzina}</p>
|
|---|
| 140 | </div>
|
|---|
| 141 |
|
|---|
| 142 | <div className={styles.cardFooter}>
|
|---|
| 143 | <span className={styles.postDate}>
|
|---|
| 144 | Објавено на: {s.kreiranoNa}
|
|---|
| 145 | </span>
|
|---|
| 146 | </div>
|
|---|
| 147 | </div>
|
|---|
| 148 | ))
|
|---|
| 149 | ) : (
|
|---|
| 150 | <div className={styles.emptyFeed}>
|
|---|
| 151 | <p>Нема активни соопштенија за овој клас.</p>
|
|---|
| 152 | </div>
|
|---|
| 153 | )}
|
|---|
| 154 | </div>
|
|---|
| 155 | </div>
|
|---|
| 156 | </div>
|
|---|
| 157 | );
|
|---|
| 158 | };
|
|---|
| 159 |
|
|---|
| 160 | export default AdminPanelSoopstenijaViewPage;
|
|---|