ERP-node/frontend/components/dashboard/widgets/DeliveryStatusSummaryWidget...

215 lines
6.6 KiB
TypeScript
Raw Normal View History

"use client";
import React, { useState, useEffect } from "react";
import { DashboardElement } from "@/components/admin/dashboard/types";
2025-10-24 16:08:57 +09:00
import { getApiUrl } from "@/lib/utils/apiUrl";
interface DeliveryStatusSummaryWidgetProps {
element: DashboardElement;
}
interface DeliveryStatus {
status: string;
count: number;
}
/**
*
* - , , ,
*/
export default function DeliveryStatusSummaryWidget({ element }: DeliveryStatusSummaryWidgetProps) {
const [statusData, setStatusData] = useState<DeliveryStatus[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
loadData();
2025-10-22 13:40:15 +09:00
// 자동 새로고침 (30초마다)
const interval = setInterval(loadData, 30000);
return () => clearInterval(interval);
}, [element]);
const loadData = async () => {
if (!element?.dataSource?.query) {
setError("쿼리가 설정되지 않았습니다");
setLoading(false);
return;
}
try {
setLoading(true);
const token = localStorage.getItem("authToken");
2025-10-24 16:08:57 +09:00
const response = await fetch(getApiUrl("/api/dashboards/execute-query"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
query: element.dataSource.query,
connectionType: element.dataSource.connectionType || "current",
connectionId: element.dataSource.connectionId,
}),
});
if (!response.ok) throw new Error("데이터 로딩 실패");
const result = await response.json();
2025-10-22 13:40:15 +09:00
// 데이터 처리
if (result.success && result.data?.rows) {
const rows = result.data.rows;
2025-10-22 13:40:15 +09:00
// 상태별 카운트 계산
const statusCounts = rows.reduce((acc: any, row: any) => {
const status = row.status || "알 수 없음";
acc[status] = (acc[status] || 0) + 1;
return acc;
}, {});
const formattedData: DeliveryStatus[] = [
{ status: "배송중", count: statusCounts["배송중"] || statusCounts["delivering"] || 0 },
{ status: "완료", count: statusCounts["완료"] || statusCounts["delivered"] || 0 },
{ status: "지연", count: statusCounts["지연"] || statusCounts["delayed"] || 0 },
{ status: "픽업 대기", count: statusCounts["픽업 대기"] || statusCounts["pending"] || 0 },
];
setStatusData(formattedData);
}
2025-10-22 13:40:15 +09:00
setError(null);
} catch (err) {
setError(err instanceof Error ? err.message : "데이터 로딩 실패");
} finally {
setLoading(false);
}
};
const getBorderColor = (status: string) => {
switch (status) {
case "배송중":
2025-10-29 17:53:03 +09:00
return "border-primary";
case "완료":
2025-10-29 17:53:03 +09:00
return "border-success";
case "지연":
2025-10-29 17:53:03 +09:00
return "border-destructive";
case "픽업 대기":
2025-10-29 17:53:03 +09:00
return "border-warning";
default:
2025-10-29 17:53:03 +09:00
return "border-border";
}
};
const getDotColor = (status: string) => {
switch (status) {
case "배송중":
2025-10-29 17:53:03 +09:00
return "bg-primary";
case "완료":
2025-10-29 17:53:03 +09:00
return "bg-success";
case "지연":
2025-10-29 17:53:03 +09:00
return "bg-destructive";
case "픽업 대기":
2025-10-29 17:53:03 +09:00
return "bg-warning/100";
default:
2025-10-29 17:53:03 +09:00
return "bg-muted0";
}
};
const getTextColor = (status: string) => {
switch (status) {
case "배송중":
2025-10-29 17:53:03 +09:00
return "text-primary";
case "완료":
2025-10-29 17:53:03 +09:00
return "text-success";
case "지연":
2025-10-29 17:53:03 +09:00
return "text-destructive";
case "픽업 대기":
2025-10-29 17:53:03 +09:00
return "text-warning";
default:
2025-10-29 17:53:03 +09:00
return "text-foreground";
}
};
if (loading) {
return (
<div className="flex h-full items-center justify-center">
<div className="text-center">
<div className="border-primary mx-auto h-8 w-8 animate-spin rounded-full border-2 border-t-transparent" />
2025-10-29 17:53:03 +09:00
<p className="mt-2 text-sm text-muted-foreground"> ...</p>
</div>
</div>
);
}
if (error) {
return (
<div className="flex h-full items-center justify-center">
2025-10-29 17:53:03 +09:00
<div className="text-center text-destructive">
<p className="text-sm"> {error}</p>
<button
onClick={loadData}
2025-10-29 17:53:03 +09:00
className="mt-2 rounded bg-destructive/10 px-3 py-1 text-xs text-destructive hover:bg-destructive/20"
>
</button>
</div>
</div>
);
}
if (!element?.dataSource?.query) {
return (
<div className="flex h-full items-center justify-center">
2025-10-29 17:53:03 +09:00
<div className="text-center text-muted-foreground">
2025-10-22 13:40:15 +09:00
<p className="text-sm"> </p>
</div>
</div>
);
}
const totalCount = statusData.reduce((sum, item) => sum + item.count, 0);
return (
2025-10-29 17:53:03 +09:00
<div className="flex h-full w-full flex-col overflow-hidden bg-gradient-to-br from-background to-primary/10 p-2">
{/* 헤더 */}
<div className="mb-2 flex flex-shrink-0 items-center justify-between">
<div className="flex-1">
2025-10-29 17:53:03 +09:00
<h3 className="text-sm font-bold text-foreground">📊 </h3>
{totalCount > 0 ? (
2025-10-29 17:53:03 +09:00
<p className="text-xs text-muted-foreground"> {totalCount.toLocaleString()}</p>
) : (
2025-10-29 17:53:03 +09:00
<p className="text-xs text-warning"> </p>
)}
</div>
<button
onClick={loadData}
2025-10-29 17:53:03 +09:00
className="border-border hover:bg-accent flex h-7 w-7 items-center justify-center rounded border bg-background p-0 text-xs disabled:opacity-50"
disabled={loading}
>
{loading ? "⏳" : "🔄"}
</button>
</div>
{/* 스크롤 가능한 콘텐츠 영역 */}
<div className="flex-1 overflow-y-auto">
{/* 상태별 카드 */}
<div className="grid grid-cols-2 gap-1.5">
{statusData.map((item) => (
<div
key={item.status}
2025-10-29 17:53:03 +09:00
className={`rounded border-l-2 bg-background p-1.5 shadow-sm ${getBorderColor(item.status)}`}
>
<div className="mb-0.5 flex items-center gap-1">
<div className={`h-1.5 w-1.5 rounded-full ${getDotColor(item.status)}`}></div>
2025-10-29 17:53:03 +09:00
<div className="text-xs font-medium text-foreground">{item.status}</div>
</div>
<div className={`text-lg font-bold ${getTextColor(item.status)}`}>{item.count.toLocaleString()}</div>
</div>
))}
</div>
</div>
</div>
);
}