fix: handle null API response data to prevent infinite loading
When API returns {"data": null} for empty lists, accessing .plans on
null throws an exception. fetchPlans never reaches setLoading(false),
causing the dashboard to spin forever. Added optional chaining,
fallback defaults, and try-catch-finally to ensure loading stops.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
6b866738be
commit
69543a8aef
@ -47,11 +47,11 @@ export function useDashboardData<T = ProductionPlan | WashingPlan>({
|
|||||||
purchaserApi.dashboardStats(),
|
purchaserApi.dashboardStats(),
|
||||||
purchaserApi.listPlans(),
|
purchaserApi.listPlans(),
|
||||||
]);
|
]);
|
||||||
const plans = plansRes.data.plans as unknown as T[];
|
const plans = (plansRes.data?.plans || []) as unknown as T[];
|
||||||
const stats: DashboardStats = {
|
const stats: DashboardStats = {
|
||||||
all: statsData.total_plans,
|
all: statsData.total_plans ?? 0,
|
||||||
producing: statsData.producing_plans,
|
producing: statsData.producing_plans ?? 0,
|
||||||
completed: statsData.completed_plans,
|
completed: statsData.completed_plans ?? 0,
|
||||||
};
|
};
|
||||||
return { plans, stats, rejectedPlanIds: new Set<string>() };
|
return { plans, stats, rejectedPlanIds: new Set<string>() };
|
||||||
}, []);
|
}, []);
|
||||||
@ -61,7 +61,7 @@ export function useDashboardData<T = ProductionPlan | WashingPlan>({
|
|||||||
textileApi.dashboardStats(),
|
textileApi.dashboardStats(),
|
||||||
textileApi.listPlans(),
|
textileApi.listPlans(),
|
||||||
]);
|
]);
|
||||||
const plans = plansRes.data as unknown as T[];
|
const plans = (plansRes.data || []) as unknown as T[];
|
||||||
const stats: DashboardStats = {
|
const stats: DashboardStats = {
|
||||||
all: statsData.total_plans,
|
all: statsData.total_plans,
|
||||||
producing: statsData.producing_plans,
|
producing: statsData.producing_plans,
|
||||||
|
|||||||
@ -87,15 +87,20 @@ export function PurchaserDashboard() {
|
|||||||
}, [auth.company, dataLoaded, plans.length]);
|
}, [auth.company, dataLoaded, plans.length]);
|
||||||
|
|
||||||
const fetchPlans = async () => {
|
const fetchPlans = async () => {
|
||||||
const res = await purchaserApi.listPlans();
|
try {
|
||||||
const allPlans = res.data.plans;
|
const res = await purchaserApi.listPlans();
|
||||||
|
const allPlans = res.data?.plans || [];
|
||||||
|
|
||||||
const filteredPlans = allPlans.filter(p =>
|
const filteredPlans = allPlans.filter(p =>
|
||||||
!p.process_steps?.some(s => s.status === STEP_STATUS.REJECTED)
|
!p.process_steps?.some(s => s.status === STEP_STATUS.REJECTED)
|
||||||
);
|
);
|
||||||
setPlans(filteredPlans);
|
setPlans(filteredPlans);
|
||||||
setLoading(false);
|
} catch (err) {
|
||||||
setDataLoaded(true);
|
console.error('Failed to fetch plans:', err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
setDataLoaded(true);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// 监听页面可见性变化,返回页面时刷新数据
|
// 监听页面可见性变化,返回页面时刷新数据
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user