diff --git a/src/pages/Dashboard.jsx b/src/pages/Dashboard.jsx index 035387a..131d0fc 100644 --- a/src/pages/Dashboard.jsx +++ b/src/pages/Dashboard.jsx @@ -1,20 +1,186 @@ import React, { useEffect, useState } from "react"; +import config from "../config"; + +const thStyle = { + padding: "0.75rem", + backgroundColor: "#333", + borderBottom: "1px solid #555", + color: "#fff", + position: "sticky", + top: 0, + zIndex: 1, + textAlign: "center", +}; + +const tdStyle = { + padding: "0.5rem 1rem", + borderBottom: "1px solid #444", + textAlign: "center", + color: "#fff", +}; const Dashboard = () => { const [role, setRole] = useState(""); + const [users, setUsers] = useState([]); + const [newRoles, setNewRoles] = useState({}); + const [selectedUsers, setSelectedUsers] = useState([]); useEffect(() => { - const role = localStorage.getItem("role"); - if (role) { - setRole(role); + const currentRole = localStorage.getItem("role"); + setRole(currentRole); + + if (currentRole === "SUPER") { + fetchUsers(); } }, []); + const fetchUsers = async () => { + const token = localStorage.getItem("token"); + + try { + const res = await fetch(`${config.apiBaseUrl}/api/Auth/users`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (!res.ok) throw new Error("Failed to fetch users"); + + const data = await res.json(); + setUsers(data); + } catch (err) { + console.error("Error fetching users:", err); + } + }; + + const handleRoleChange = (username, value) => { + setNewRoles({ ...newRoles, [username]: value }); + }; + + const updateUserRole = async (username) => { + const token = localStorage.getItem("token"); + const newRole = newRoles[username]; + + try { + const res = await fetch(`${config.apiBaseUrl}/api/Auth/role`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ username, newRole }), + }); + + if (!res.ok) throw new Error("Failed to update role"); + + fetchUsers(); + } catch (err) { + console.error("Error updating role:", err); + } + }; + + const toggleUserSelection = (username) => { + setSelectedUsers((prev) => + prev.includes(username) + ? prev.filter((u) => u !== username) + : [...prev, username] + ); + }; + return (
Welcome! You are logged in.
- {role === "SUPER" &&You are SUPER
} + + {role === "SUPER" && ( + <> +You are SUPER
+ +| + | Username | +Role | + {selectedUsers.length > 0 && ( +Action | + )} +
|---|---|---|---|
| + toggleUserSelection(user.username)} + /> + | +{user.username} | ++ {isSelected ? ( + + ) : ( + user.role + )} + | + {selectedUsers.length > 0 && ( ++ {isSelected && ( + + )} + | + )} +