fix: Dashboard 역할 전환 시 컨텐츠 즉시 반영 기능 추가
- userRole을 정적 변수에서 useState로 변경하여 reactive state로 전환 - useEffect로 storage 및 roleChanged 이벤트 리스너 추가 - localStorage 변경 감지 시 자동 리렌더링 구현 - SystemAdmin, CEO, ProductionManager, Worker 역할별 대시보드 동적 렌더링
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -60,10 +60,32 @@ import { Checkbox } from "@/components/ui/checkbox";
|
|||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||||
|
|
||||||
export function Dashboard() {
|
export function Dashboard() {
|
||||||
// Get user role from localStorage
|
// Get user role from localStorage and make it reactive
|
||||||
const userDataStr = localStorage.getItem("user");
|
const [userRole, setUserRole] = useState<string>(() => {
|
||||||
const userData = userDataStr ? JSON.parse(userDataStr) : null;
|
const userDataStr = localStorage.getItem("user");
|
||||||
const userRole = userData?.role || "CEO"; // Default to CEO if not found
|
const userData = userDataStr ? JSON.parse(userDataStr) : null;
|
||||||
|
return userData?.role || "CEO";
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen for storage changes to update role dynamically
|
||||||
|
useEffect(() => {
|
||||||
|
const handleStorageChange = () => {
|
||||||
|
const userDataStr = localStorage.getItem("user");
|
||||||
|
const userData = userDataStr ? JSON.parse(userDataStr) : null;
|
||||||
|
const newRole = userData?.role || "CEO";
|
||||||
|
setUserRole(newRole);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Listen to custom storage event
|
||||||
|
window.addEventListener('storage', handleStorageChange);
|
||||||
|
// Listen to custom role change event
|
||||||
|
window.addEventListener('roleChanged', handleStorageChange);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('storage', handleStorageChange);
|
||||||
|
window.removeEventListener('roleChanged', handleStorageChange);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// 시스템관리자인 경우 SystemAdminDashboard 렌더링
|
// 시스템관리자인 경우 SystemAdminDashboard 렌더링
|
||||||
if (userRole === "SystemAdmin") {
|
if (userRole === "SystemAdmin") {
|
||||||
|
|||||||
Reference in New Issue
Block a user