|
|
@@ -4,7 +4,7 @@ import { Button } from '@/client/components/ui/button';
|
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/client/components/ui/card';
|
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/client/components/ui/table';
|
|
|
import { DataTablePagination } from '../components/DataTablePagination';
|
|
|
-import { Plus, Edit, Trash2, Search, Power } from 'lucide-react';
|
|
|
+import { Plus, Edit, Trash2, Search, Power, ListTree, Table as TableIcon, RotateCcw } from 'lucide-react';
|
|
|
import { useState, useCallback } from 'react';
|
|
|
import { areaClient } from '@/client/api';
|
|
|
import type { InferResponseType, InferRequestType } from 'hono/client';
|
|
|
@@ -14,7 +14,9 @@ import { Badge } from '@/client/components/ui/badge';
|
|
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/client/components/ui/dialog';
|
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/client/components/ui/alert-dialog';
|
|
|
import { AreaForm } from '../components/AreaForm';
|
|
|
+import { AreaTree } from '../components/AreaTree';
|
|
|
import type { CreateAreaInput, UpdateAreaInput } from '@/server/modules/areas/area.schema';
|
|
|
+import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/client/components/ui/tabs';
|
|
|
|
|
|
// 类型提取规范
|
|
|
type AreaResponse = InferResponseType<typeof areaClient.$get, 200>['data'][0];
|
|
|
@@ -51,6 +53,8 @@ export const AreasPage: React.FC = () => {
|
|
|
const [level, setLevel] = useState<string>('all');
|
|
|
const [parentId, setParentId] = useState<string>('');
|
|
|
const [isDisabled, setIsDisabled] = useState<string>('all');
|
|
|
+ const [viewMode, setViewMode] = useState<'table' | 'tree'>('table');
|
|
|
+ const [expandedNodes, setExpandedNodes] = useState<Set<number>>(new Set());
|
|
|
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false);
|
|
|
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false);
|
|
|
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
|
|
|
@@ -81,6 +85,26 @@ export const AreasPage: React.FC = () => {
|
|
|
gcTime: 10 * 60 * 1000,
|
|
|
});
|
|
|
|
|
|
+ // 查询树形结构数据
|
|
|
+ const { data: treeData, isLoading: isTreeLoading } = useQuery({
|
|
|
+ queryKey: ['areas-tree'],
|
|
|
+ queryFn: async () => {
|
|
|
+ const res = await areaClient.$get({
|
|
|
+ query: {
|
|
|
+ page: 1,
|
|
|
+ pageSize: 1000, // 获取所有数据用于构建树
|
|
|
+ relations: ['children']
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (res.status !== 200) throw new Error('获取省市区树形数据失败');
|
|
|
+ const response = await res.json();
|
|
|
+ return buildTree(response.data);
|
|
|
+ },
|
|
|
+ staleTime: 5 * 60 * 1000,
|
|
|
+ gcTime: 10 * 60 * 1000,
|
|
|
+ enabled: viewMode === 'tree'
|
|
|
+ });
|
|
|
+
|
|
|
// 创建省市区
|
|
|
const createMutation = useMutation({
|
|
|
mutationFn: async (data: CreateAreaRequest) => {
|
|
|
@@ -219,6 +243,54 @@ export const AreasPage: React.FC = () => {
|
|
|
setIsStatusDialogOpen(true);
|
|
|
};
|
|
|
|
|
|
+ // 构建树形结构
|
|
|
+ const buildTree = (areas: AreaResponse[]): AreaResponse[] => {
|
|
|
+ const areaMap = new Map<number, AreaResponse>();
|
|
|
+ const tree: AreaResponse[] = [];
|
|
|
+
|
|
|
+ // 创建映射
|
|
|
+ areas.forEach(area => {
|
|
|
+ areaMap.set(area.id, { ...area, children: [] });
|
|
|
+ });
|
|
|
+
|
|
|
+ // 构建树
|
|
|
+ areas.forEach(area => {
|
|
|
+ const node = areaMap.get(area.id)!;
|
|
|
+ if (area.parentId === null || area.parentId === 0) {
|
|
|
+ tree.push(node);
|
|
|
+ } else {
|
|
|
+ const parent = areaMap.get(area.parentId);
|
|
|
+ if (parent) {
|
|
|
+ parent.children!.push(node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return tree;
|
|
|
+ };
|
|
|
+
|
|
|
+ // 切换节点展开状态
|
|
|
+ const handleToggleNode = (nodeId: number) => {
|
|
|
+ setExpandedNodes(prev => {
|
|
|
+ const newSet = new Set(prev);
|
|
|
+ if (newSet.has(nodeId)) {
|
|
|
+ newSet.delete(nodeId);
|
|
|
+ } else {
|
|
|
+ newSet.add(nodeId);
|
|
|
+ }
|
|
|
+ return newSet;
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ // 重置筛选
|
|
|
+ const handleResetFilters = () => {
|
|
|
+ setKeyword('');
|
|
|
+ setLevel('all');
|
|
|
+ setParentId('');
|
|
|
+ setIsDisabled('all');
|
|
|
+ setPage(1);
|
|
|
+ };
|
|
|
+
|
|
|
// 获取层级显示名称
|
|
|
const getLevelName = (level: number) => {
|
|
|
switch (level) {
|
|
|
@@ -238,20 +310,52 @@ export const AreasPage: React.FC = () => {
|
|
|
管理省市区三级联动数据
|
|
|
</p>
|
|
|
</div>
|
|
|
- <Button onClick={() => setIsCreateDialogOpen(true)}>
|
|
|
- <Plus className="mr-2 h-4 w-4" />
|
|
|
- 新增省市区
|
|
|
- </Button>
|
|
|
+ <div className="flex gap-2">
|
|
|
+ <Button
|
|
|
+ variant={viewMode === 'table' ? 'default' : 'outline'}
|
|
|
+ size="sm"
|
|
|
+ onClick={() => setViewMode('table')}
|
|
|
+ >
|
|
|
+ <TableIcon className="mr-2 h-4 w-4" />
|
|
|
+ 表格视图
|
|
|
+ </Button>
|
|
|
+ <Button
|
|
|
+ variant={viewMode === 'tree' ? 'default' : 'outline'}
|
|
|
+ size="sm"
|
|
|
+ onClick={() => setViewMode('tree')}
|
|
|
+ >
|
|
|
+ <ListTree className="mr-2 h-4 w-4" />
|
|
|
+ 树形视图
|
|
|
+ </Button>
|
|
|
+ <Button onClick={() => setIsCreateDialogOpen(true)}>
|
|
|
+ <Plus className="mr-2 h-4 w-4" />
|
|
|
+ 新增省市区
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
|
|
|
- <Card>
|
|
|
- <CardHeader>
|
|
|
- <CardTitle>省市区列表</CardTitle>
|
|
|
- <CardDescription>
|
|
|
- 查看和管理所有省市区数据
|
|
|
- </CardDescription>
|
|
|
- </CardHeader>
|
|
|
- <CardContent>
|
|
|
+ <Tabs value={viewMode} onValueChange={(value) => setViewMode(value as 'table' | 'tree')}>
|
|
|
+ <TabsList className="mb-4">
|
|
|
+ <TabsTrigger value="table">
|
|
|
+ <TableIcon className="mr-2 h-4 w-4" />
|
|
|
+ 表格视图
|
|
|
+ </TabsTrigger>
|
|
|
+ <TabsTrigger value="tree">
|
|
|
+ <ListTree className="mr-2 h-4 w-4" />
|
|
|
+ 树形视图
|
|
|
+ </TabsTrigger>
|
|
|
+ </TabsList>
|
|
|
+
|
|
|
+ {/* 表格视图 */}
|
|
|
+ <TabsContent value="table">
|
|
|
+ <Card>
|
|
|
+ <CardHeader>
|
|
|
+ <CardTitle>省市区列表</CardTitle>
|
|
|
+ <CardDescription>
|
|
|
+ 查看和管理所有省市区数据
|
|
|
+ </CardDescription>
|
|
|
+ </CardHeader>
|
|
|
+ <CardContent>
|
|
|
{/* 搜索和筛选区域 */}
|
|
|
<div className="flex flex-col gap-4 mb-6">
|
|
|
<div className="flex gap-4">
|
|
|
@@ -261,10 +365,22 @@ export const AreasPage: React.FC = () => {
|
|
|
<Input
|
|
|
placeholder="搜索省市区名称或代码..."
|
|
|
className="pl-8"
|
|
|
- onChange={handleSearchChange}
|
|
|
+ value={keyword}
|
|
|
+ onChange={(e) => {
|
|
|
+ setKeyword(e.target.value);
|
|
|
+ debouncedSearch(e.target.value);
|
|
|
+ }}
|
|
|
/>
|
|
|
</div>
|
|
|
</div>
|
|
|
+ <Button
|
|
|
+ variant="outline"
|
|
|
+ onClick={handleResetFilters}
|
|
|
+ disabled={!keyword && level === 'all' && isDisabled === 'all'}
|
|
|
+ >
|
|
|
+ <RotateCcw className="mr-2 h-4 w-4" />
|
|
|
+ 重置
|
|
|
+ </Button>
|
|
|
</div>
|
|
|
<div className="flex gap-4">
|
|
|
<Select value={level} onValueChange={(value) => handleFilterChange('level', value)}>
|
|
|
@@ -387,6 +503,40 @@ export const AreasPage: React.FC = () => {
|
|
|
)}
|
|
|
</CardContent>
|
|
|
</Card>
|
|
|
+ </TabsContent>
|
|
|
+
|
|
|
+ {/* 树形视图 */}
|
|
|
+ <TabsContent value="tree">
|
|
|
+ <Card>
|
|
|
+ <CardHeader>
|
|
|
+ <CardTitle>省市区树形结构</CardTitle>
|
|
|
+ <CardDescription>
|
|
|
+ 以树形结构查看和管理省市区层级关系
|
|
|
+ </CardDescription>
|
|
|
+ </CardHeader>
|
|
|
+ <CardContent>
|
|
|
+ {isTreeLoading ? (
|
|
|
+ <div className="text-center py-8">
|
|
|
+ 加载中...
|
|
|
+ </div>
|
|
|
+ ) : !treeData || treeData.length === 0 ? (
|
|
|
+ <div className="text-center py-8">
|
|
|
+ 暂无数据
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <AreaTree
|
|
|
+ areas={treeData}
|
|
|
+ expandedNodes={expandedNodes}
|
|
|
+ onToggleNode={handleToggleNode}
|
|
|
+ onEdit={handleEdit}
|
|
|
+ onDelete={handleDelete}
|
|
|
+ onToggleStatus={handleToggleStatusDialog}
|
|
|
+ />
|
|
|
+ )}
|
|
|
+ </CardContent>
|
|
|
+ </Card>
|
|
|
+ </TabsContent>
|
|
|
+ </Tabs>
|
|
|
|
|
|
{/* 创建省市区对话框 */}
|
|
|
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
|