source: frontend/web3-eDnevnik_Frontend/src/pages/DirektorAdmin_Pages/CreateKlasenRakovoditel/CreateKlasenRakovoditel.jsx

Last change on this file was 81cdf2d, checked in by Nikola Janev <nikolajanev57@…>, 9 days ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1import React, { useState, useEffect } from "react";
2import axios from "axios";
3import { useNavigate } from "react-router-dom";
4import styles from "./CreateKlasenRakovoditelStyles.module.css";
5import Navbar from "../../../components/nav/Navbar.jsx";
6
7const CreateKlsenRakovoditel = () => {
8 const navigate = useNavigate();
9
10 const [nastavnici, setNastavnici] = useState([]);
11
12 const [adminId, setAdminId] = useState("");
13
14 const [error, setError] = useState("");
15 const [loading, setLoading] = useState(true);
16
17 const [formData, setFormData] = useState({
18 nastavnikId: "",
19 });
20
21 useEffect(() => {
22 const fetchInitialData = async () => {
23 try {
24 setLoading(true);
25
26 const [resNastavnici, resMe] = await Promise.all([
27 axios.get("http://localhost:8080/api/users/nastavnici", {
28 withCredentials: true,
29 }),
30
31 axios.get("http://localhost:8080/api/auth/me", {
32 withCredentials: true,
33 }),
34 ]);
35
36 setNastavnici(resNastavnici.data);
37
38 setAdminId(resMe.data.id || resMe.data.userId);
39 } catch (err) {
40 console.error("Грешка при вчитување податоци:", err);
41 setError("Не успеавме да ги вчитаме листите со родители и класови.");
42 } finally {
43 setLoading(false);
44 }
45 };
46 fetchInitialData();
47 }, []);
48
49 const handleChange = (e) => {
50 const { name, value } = e.target;
51 setFormData((prev) => ({
52 ...prev,
53 [name]: value,
54 }));
55 };
56
57 const handleSubmit = async (e) => {
58 e.preventDefault();
59 setError("");
60
61 const klasenRakovoditelRequestDTO = {
62 nastavnikId: formData.nastavnikId,
63
64 kreiranOdId: adminId,
65 };
66
67 try {
68 const response = await axios.post(
69 "http://localhost:8080/api/users/nastavnici/klasniRakovoditeli",
70 klasenRakovoditelRequestDTO,
71 {
72 withCredentials: true,
73 headers: { "Content-Type": "application/json" },
74 }
75 );
76
77 if (response.status === 200 || response.status === 201) {
78 alert("Класниот раководител е успешно креиран!");
79 navigate("/adminDashboard/users");
80 }
81 } catch (err) {
82 console.error("Грешка при POST:", err.response?.data);
83 const serverMsg =
84 err.response?.data?.message ||
85 "Грешка при валидација. Проверете ги сите полиња.";
86 setError(serverMsg);
87 }
88 };
89
90 if (loading)
91 return (
92 <div style={{ padding: "50px", textAlign: "center" }}>
93 <Navbar />
94 <h2>Вчитување на формата...</h2>
95 </div>
96 );
97
98 return (
99 <div style={{ backgroundColor: "#fdf8e1", minHeight: "100vh" }}>
100 <Navbar />
101 <div className={styles.container}>
102 <h2 className={styles.title}>Додади нов класен раководител</h2>
103
104 {error && <div className={styles.errorBanner}>{error}</div>}
105
106 <form onSubmit={handleSubmit} className={styles.formGrid}>
107 <div className={styles.inputGroup}>
108 <label>Избери Наставник *</label>
109 <select
110 name="nastavnikId"
111 value={formData.nastavnikId}
112 required
113 onChange={handleChange}
114 className={styles.inputField}
115 >
116 <option value="">-- Избери наставник --</option>
117 {nastavnici.map((n) => (
118 <option key={n.id} value={n.id}>
119 {n.ime} {n.prezime} ({n.e_posta})
120 </option>
121 ))}
122 </select>
123 </div>
124
125 <div className={styles.fullWidth}>
126 <button type="submit" className={styles.submitBtn}>
127 Креирај класен раководител
128 </button>
129 </div>
130 </form>
131 </div>
132 </div>
133 );
134};
135
136export default CreateKlsenRakovoditel;
Note: See TracBrowser for help on using the repository browser.