304 lines
11 KiB
TypeScript
304 lines
11 KiB
TypeScript
"use client";
|
|
import { useEffect, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { tokenManager } from "@/entities/auth/lib/token-manager";
|
|
import { client } from "@/shared/api";
|
|
import { ENDPOINT } from "@/shared/config";
|
|
import { KoreaBasinMap } from "../lib/KoreaBasinMap";
|
|
import {
|
|
Chart as ChartJS,
|
|
CategoryScale,
|
|
LinearScale,
|
|
PointElement,
|
|
LineElement,
|
|
BarElement,
|
|
Filler,
|
|
Legend,
|
|
Tooltip,
|
|
ArcElement,
|
|
} from "chart.js";
|
|
import { Doughnut } from "react-chartjs-2";
|
|
|
|
// Chart.js 등록
|
|
ChartJS.register(
|
|
CategoryScale,
|
|
LinearScale,
|
|
PointElement,
|
|
LineElement,
|
|
BarElement,
|
|
Filler,
|
|
Legend,
|
|
Tooltip,
|
|
ArcElement
|
|
);
|
|
|
|
interface UserInfo {
|
|
email?: string;
|
|
userId?: string;
|
|
userName?: string;
|
|
twoFactorEnabled?: boolean;
|
|
}
|
|
|
|
function DashboardPage() {
|
|
const [userInfo, setUserInfo] = useState<UserInfo | null>(null);
|
|
const [error, setError] = useState("");
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
const fetchUserInfo = async () => {
|
|
console.log("fetchUserInfo called");
|
|
|
|
try {
|
|
// tokenManager를 통해 토큰 확인
|
|
const accessToken = tokenManager.getToken("Authorization");
|
|
const userId = tokenManager.getToken("token-id");
|
|
|
|
if (!accessToken || !userId) {
|
|
console.log("No tokens found, redirecting to login");
|
|
setError("로그인이 필요합니다.");
|
|
router.push("/auth/login");
|
|
return;
|
|
}
|
|
|
|
console.log("Fetching user info with userId:", userId);
|
|
|
|
// client를 사용하여 API 호출 (자동 토큰 처리)
|
|
const response = await client.get(
|
|
`${ENDPOINT.USER_SERVICE.USERS}/${userId}`,
|
|
);
|
|
|
|
if (response.data) {
|
|
console.log("User info received:", response.data);
|
|
setUserInfo(response.data);
|
|
} else {
|
|
setError("사용자 정보를 불러올 수 없습니다.");
|
|
}
|
|
} catch (error) {
|
|
console.error("Error fetching user info:", error);
|
|
if (error instanceof Error && error.message.includes("401")) {
|
|
setError("인증이 만료되었습니다. 다시 로그인해주세요.");
|
|
tokenManager.clearAuthToken();
|
|
router.push("/auth/login");
|
|
} else {
|
|
setError("네트워크 오류가 발생했습니다.");
|
|
}
|
|
}
|
|
};
|
|
|
|
fetchUserInfo();
|
|
}, [router]);
|
|
|
|
const handleLogout = () => {
|
|
console.log("Logging out...");
|
|
// localStorage 정리
|
|
localStorage.removeItem("tempToken");
|
|
// tokenManager를 통해 쿠키 정리
|
|
tokenManager.clearAuthToken();
|
|
router.push("/auth/logout");
|
|
};
|
|
|
|
const handleChangePassword = () => {
|
|
router.push("/otp?next=change-password");
|
|
};
|
|
|
|
return (
|
|
<div className="p-6">
|
|
<div className="flex flex-col gap-1">
|
|
{/* 첫번째 라인 */}
|
|
<div className="grid grid-cols-8 gap-1">
|
|
{/* 첫번쨰 열 */}
|
|
<div className="col-span-2 h-250 !p-0">
|
|
{/* 전국 하수처리시설 가동률 */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">전국 하수처리시설 가동률</h3>
|
|
</div>
|
|
<div className="h-60 p-4">
|
|
<Doughnut
|
|
data={{
|
|
datasets: [{
|
|
data: [75, 25], // 75% 가동률
|
|
backgroundColor: ['#00DFE5', '#f5f5f5'],
|
|
borderWidth: 0
|
|
}]
|
|
}}
|
|
options={{
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
cutout: '70%',
|
|
circumference: 180, // 반원
|
|
rotation: -90, // 왼쪽에서 시작
|
|
plugins: {
|
|
legend: {
|
|
display: false
|
|
},
|
|
tooltip: {
|
|
enabled: false
|
|
}
|
|
}
|
|
}}
|
|
plugins={[{
|
|
id: 'centerText',
|
|
afterDraw: (chart) => {
|
|
const ctx = chart.ctx;
|
|
const centerX = chart.width / 2;
|
|
const centerY = chart.height / 2;
|
|
|
|
// 가동률 텍스트
|
|
ctx.save();
|
|
ctx.font = 'bold 24px Arial';
|
|
ctx.fillStyle = '#00DFE5';
|
|
ctx.textAlign = 'center';
|
|
ctx.textBaseline = 'middle';
|
|
ctx.fillText('75%', centerX, centerY + 10);
|
|
|
|
// 라벨 텍스트
|
|
ctx.font = '12px Arial';
|
|
ctx.fillStyle = '#666';
|
|
ctx.fillText('가동률', centerX, centerY + 30);
|
|
ctx.restore();
|
|
}
|
|
}]}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 오수관로 및 우수관로 현황 */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">오수관로 및 우수관로 현황</h3>
|
|
</div>
|
|
<div className="h-60 p-4"></div>
|
|
</div>
|
|
|
|
{/* 하수처리수 재이용 */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">하수처리수 재이용</h3>
|
|
</div>
|
|
<div className="h-60 p-4"></div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 두번쨰 열 */}
|
|
<div className="col-span-2 h-250 !p-0">
|
|
{/* 하수처리시설(개소) */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">하수처리시설(개소)</h3>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-1 h-60 !p-0">
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground text-center">
|
|
<h3 className="text-l font-medium" style={{textAlign: "center", width: "100%"}}>500㎥ 미만(개소)</h3>
|
|
</div>
|
|
<div style={{fontSize: "38px", textAlign: "center", fontWeight: "600", lineHeight: "1.2", color: "#0ec58a", padding: "2px 0"}}>3,567 <span style={{fontSize: "14px", color: "#000000"}}>개소</span></div>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground text-center">
|
|
<h3 className="text-l font-medium" style={{textAlign: "center", width: "100%"}}>500㎥ 이상(개소)</h3>
|
|
</div>
|
|
<div style={{fontSize: "38px", textAlign: "center", fontWeight: "600", lineHeight: "1.2", color: "#0ec58a", padding: "2px 0"}}>767 <span style={{fontSize: "14px", color: "#000000"}}>개소</span></div>
|
|
</div>
|
|
<div className="col-span-2" style={{width: "100%", textAlign: "right"}}>기준년월 : 2025.09 </div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 노후하수관로현황 */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">노후하수관로현황</h3>
|
|
</div>
|
|
<div className="h-70 p-4">
|
|
</div>
|
|
</div>
|
|
|
|
{/* 재이용시설(개소) */}
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">재이용시설(개소)</h3>
|
|
</div>
|
|
<div className="h-70 p-4">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 지도영역 */}
|
|
<div className="col-span-3 h-200 !p-0 relative z-10">
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">유역별 정보</h3>
|
|
</div>
|
|
<KoreaBasinMap />
|
|
</div>
|
|
</div>
|
|
|
|
{/* 사용자 정보 카드 */}
|
|
<div className="card card-border col-span-1 h-80 p-10">
|
|
<h2 className="mb-6 text-2xl">사용자 정보</h2>
|
|
{error && <div className="mb-4 text-red-500">{error}</div>}
|
|
{userInfo ? (
|
|
<div className="mb-4">
|
|
<div>
|
|
<b>아이디:</b> {userInfo.userId}
|
|
</div>
|
|
<div>
|
|
<b>이메일:</b> {userInfo.email || userInfo.userId}
|
|
</div>
|
|
<div>
|
|
<b>이름:</b> {userInfo.userName || "-"}
|
|
</div>
|
|
<div>
|
|
<b>2FA 활성화:</b>{" "}
|
|
{userInfo.twoFactorEnabled ? "예" : "아니오"}
|
|
</div>
|
|
</div>
|
|
) : (
|
|
!error && <div>사용자 정보를 불러오는 중...</div>
|
|
)}
|
|
<button className="btn mb-2 w-full" onClick={handleLogout}>
|
|
로그아웃
|
|
</button>
|
|
<button className="btn w-full" onClick={handleChangePassword}>
|
|
패스워드 변경
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 두번째 라인 */}
|
|
<div className="grid grid-cols-4 gap-1">
|
|
<div className="card card-border"id="grouping-statistics">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">지원현황</h3>
|
|
</div>
|
|
<div className="h-70 p-4">
|
|
|
|
</div>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">자료관 자료현황</h3>
|
|
</div>
|
|
<div className="h-70 p-4">
|
|
</div>
|
|
</div>
|
|
<div className="card card-border">
|
|
<div className="card-header text-foreground">
|
|
<h3 className="text-2xl font-medium">공지사항</h3>
|
|
</div>
|
|
<div className="h-70 p-4">
|
|
</div>
|
|
</div>
|
|
|
|
{/* 배너 영역 */}
|
|
<div className="card card-border">
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export { DashboardPage };
|