fix: resolve infinite loading in new plan page product selector

The page called a non-existent /purchaser/products/yarn-ratios endpoint.
When that 404'd, the unhandled error prevented setLoading(false) from running,
leaving the product selector stuck on "加载中". Now extracts yarn_ratios
directly from the product list response (already included in each product object).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 23:25:16 +08:00
parent b24de9ac11
commit c8b42edaa7
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0

View File

@ -2,7 +2,6 @@ import React, { useState, useEffect, useMemo } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { purchaserApi } from '../../api'; import { purchaserApi } from '../../api';
import { http } from '../../api';
import { ArrowLeft, Plus, Trash2, Search, X, ChevronDown } from 'lucide-react'; import { ArrowLeft, Plus, Trash2, Search, X, ChevronDown } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion'; import { motion, AnimatePresence } from 'framer-motion';
import { useErrorHandler } from '../../hooks/useErrorHandler'; import { useErrorHandler } from '../../hooks/useErrorHandler';
@ -88,29 +87,30 @@ export function NewPlan() {
if (!auth.company) return; if (!auth.company) return;
setLoading(true); setLoading(true);
const res = await purchaserApi.listProducts(); try {
const prodData = res.data; const res = await purchaserApi.listProducts();
const prodData = res.data || [];
if (prodData) {
setProducts(prodData as Product[]); setProducts(prodData as Product[]);
const productIds = prodData.map(p => p.id); const ratiosMap: Record<string, ProductYarnRatio[]> = {};
if (productIds.length > 0) { prodData.forEach((p: any) => {
const ratiosRes = await http.get<ProductYarnRatio[]>('/purchaser/products/yarn-ratios', { if (p.yarn_ratios && Array.isArray(p.yarn_ratios)) {
product_ids: productIds.join(',') ratiosMap[p.id] = p.yarn_ratios.map((r: any, i: number) => ({
}); id: `${p.id}-${i}`,
product_id: p.id,
if (ratiosRes.data) { yarn_name: r.yarn_name,
const ratiosMap: Record<string, ProductYarnRatio[]> = {}; ratio: r.ratio,
ratiosRes.data.forEach((r: any) => { yarn_type: r.yarn_type,
if (!ratiosMap[r.product_id]) ratiosMap[r.product_id] = []; }));
ratiosMap[r.product_id].push(r as ProductYarnRatio);
});
setProductRatios(ratiosMap);
} }
} });
setProductRatios(ratiosMap);
} catch (err) {
handleError(err, { userMessage: '加载产品列表失败' });
} finally {
setLoading(false);
} }
setLoading(false);
}; };
const fetchFactories = async () => { const fetchFactories = async () => {