浏览代码

♻️ refactor(goods-parent-child-panel): 优化组件代码和逻辑

- 移除未使用的图标导入(Trash2, X, Check, ChevronDown, ChevronRight)
- 移除未使用的状态变量 expandedParentId
- 移除未使用的查询选项 isLoadingChildren 和 refetchChildren
- 修复 API 端点路径,将 ':id'.setAsParent 改为 ':id'['set-as-parent']
- 优化 hasChildren 判断逻辑,处理 childrenData?.total 可能为 undefined 的情况
- 移除 onSaveTemplate 回调中未使用的 specs 参数
yourname 1 月之前
父节点
当前提交
4dc4015891
共有 1 个文件被更改,包括 5 次插入6 次删除
  1. 5 6
      packages/goods-management-ui-mt/src/components/GoodsParentChildPanel.tsx

+ 5 - 6
packages/goods-management-ui-mt/src/components/GoodsParentChildPanel.tsx

@@ -1,7 +1,7 @@
 import React, { useState, useEffect } from 'react';
 import { useQuery, useMutation } from '@tanstack/react-query';
 import { toast } from 'sonner';
-import { Layers, Package, Plus, Trash2, Edit, X, Check, ChevronDown, ChevronRight } from 'lucide-react';
+import { Layers, Package, Plus, Edit } from 'lucide-react';
 
 import { Button } from '@d8d/shared-ui-components/components/ui/button';
 import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@d8d/shared-ui-components/components/ui/card';
@@ -81,14 +81,13 @@ export const GoodsParentChildPanel: React.FC<GoodsParentChildPanelProps> = ({
   disabled = false
 }) => {
   const [panelMode, setPanelMode] = useState<PanelMode>(PanelMode.VIEW);
-  const [expandedParentId, setExpandedParentId] = useState<number | null>(null);
   const [selectedChildren, setSelectedChildren] = useState<number[]>(childGoodsIds);
   const [localBatchSpecs, setLocalBatchSpecs] = useState<BatchSpecTemplate[]>(batchSpecs);
   const [isSetAsParentDialogOpen, setIsSetAsParentDialogOpen] = useState(false);
   const [isRemoveParentDialogOpen, setIsRemoveParentDialogOpen] = useState(false);
 
   // 获取子商品列表(编辑模式)
-  const { data: childrenData, isLoading: isLoadingChildren, refetch: refetchChildren } = useQuery({
+  const { data: childrenData } = useQuery({
     queryKey: ['goods-children', goodsId, tenantId],
     queryFn: async () => {
       if (!goodsId || mode !== 'edit') return { data: [], total: 0 };
@@ -116,7 +115,7 @@ export const GoodsParentChildPanel: React.FC<GoodsParentChildPanelProps> = ({
   const setAsParentMutation = useMutation({
     mutationFn: async () => {
       if (!goodsId) throw new Error('商品ID不能为空');
-      const res = await goodsClientManager.get()[':id'].setAsParent.$post({
+      const res = await goodsClientManager.get()[':id']['set-as-parent'].$post({
         param: { id: goodsId }
       });
       if (res.status !== 200) throw new Error('设为父商品失败');
@@ -261,7 +260,7 @@ export const GoodsParentChildPanel: React.FC<GoodsParentChildPanelProps> = ({
   // 判断当前商品状态
   const isParent = spuId === 0;
   const isChild = spuId > 0;
-  const hasChildren = childrenData?.total > 0 || selectedChildren.length > 0;
+  const hasChildren = (childrenData?.total || 0) > 0 || selectedChildren.length > 0;
 
   return (
     <Card className="mt-6">
@@ -423,7 +422,7 @@ export const GoodsParentChildPanel: React.FC<GoodsParentChildPanelProps> = ({
                   });
                 }
               }}
-              onSaveTemplate={(templateName, specs) => {
+              onSaveTemplate={(templateName) => {
                 toast.success(`模板 "${templateName}" 已保存`);
                 // 在实际应用中,这里可以保存到本地存储或后端
               }}