fix: handle plans API returning array instead of {plans:[]} object

The purchaser plans endpoint returns data as a plain array, not wrapped
in {plans:[...]}. Added Array.isArray check to handle both formats.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 02:07:18 +08:00
parent 69543a8aef
commit c641831251
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0
2 changed files with 5 additions and 2 deletions

View File

@ -47,7 +47,8 @@ export function useDashboardData<T = ProductionPlan | WashingPlan>({
purchaserApi.dashboardStats(),
purchaserApi.listPlans(),
]);
const plans = (plansRes.data?.plans || []) as unknown as T[];
const rawData = plansRes.data;
const plans = (Array.isArray(rawData) ? rawData : (rawData?.plans || [])) as unknown as T[];
const stats: DashboardStats = {
all: statsData.total_plans ?? 0,
producing: statsData.producing_plans ?? 0,

View File

@ -89,7 +89,9 @@ export function PurchaserDashboard() {
const fetchPlans = async () => {
try {
const res = await purchaserApi.listPlans();
const allPlans = res.data?.plans || [];
// API may return {plans: [...]} object or plain array
const rawData = res.data;
const allPlans = Array.isArray(rawData) ? rawData : (rawData?.plans || []);
const filteredPlans = allPlans.filter(p =>
!p.process_steps?.some(s => s.status === STEP_STATUS.REJECTED)