diff --git a/apps/web/src/app/main/Test/Page.tsx b/apps/web/src/app/main/Test/Page.tsx index ded83b0..8b26404 100755 --- a/apps/web/src/app/main/Test/Page.tsx +++ b/apps/web/src/app/main/Test/Page.tsx @@ -1,18 +1,13 @@ import { api } from '@nice/client'; -import { Pagination } from 'antd'; -import React, { useState } from 'react'; - -// 定义 TrainSituation 接口 -interface TrainSituation { - id: string; - staffId: string; - score: number; -} +import { Button, Input, Pagination, Space, Modal, Form, message, Switch } from 'antd'; +import { SearchOutlined, EditOutlined, DeleteOutlined, PlusOutlined } from '@ant-design/icons'; +import React, { useState, useRef, useEffect } from 'react'; // 定义 Staff 接口 interface Staff { id: string; username: string; + showname: string; absent: boolean; // trainSituations: TrainSituation[]; } @@ -24,64 +19,244 @@ interface PaginatedResponse { const TestPage: React.FC = () => { const [currentPage, setCurrentPage] = useState(1); + const [searchText, setSearchText] = useState(''); + const [isModalVisible, setIsModalVisible] = useState(false); + const [editingStaff, setEditingStaff] = useState(null); + const [form] = Form.useForm(); const pageSize = 10; - // 使用 findManyWithPagination 替换原来的两个查询 - const { data, isLoading } = api.staff.findManyWithPagination.useQuery({ + // 添加 useRef 处理查询 + const searchRef = useRef(false); + + // 优化查询逻辑 + const { data, isLoading, refetch } = api.staff.findManyWithPagination.useQuery({ page: currentPage, pageSize: pageSize, - where: { deletedAt: null }, + where: { + deletedAt: null, + OR: searchText ? [ + { username: { contains: searchText } }, + { showname: { contains: searchText } } + ] : undefined + }, select: { id: true, username: true, + showname: true, absent: true, } + }, { + enabled: searchRef.current, // 控制查询启用 + refetchOnWindowFocus: false, + keepPreviousData: true, // 保持之前的数据直到新数据加载完成 }); - console.log(data); - // data 中包含了分页数据和总记录数 - const staffs = (data?.items || []) as Staff[]; - const totalCount = data?.total || 0; + // 删除方法 + const deleteMutation = api.staff.softDeleteByIds.useMutation({ + onSuccess: () => { +message.success('删除成功'); + refetch(); + }, + onError: (error) => { + message.error('删除失败:' + error.message); + } + }); - // 分页处理函数 + // 更新方法 + const updateMutation = api.staff.update.useMutation({ + onSuccess: () => { + message.success('更新成功'); + setIsModalVisible(false); + refetch(); + } + }); + + // 创建方法 + const createMutation = api.staff.create.useMutation({ + onSuccess: () => { + message.success('创建成功'); + setIsModalVisible(false); + refetch(); + } + }); + + const handleSearch = () => { + setCurrentPage(1); + searchRef.current = true; + refetch(); + }; + + // 修改分页处理 const handlePageChange = (page: number) => { setCurrentPage(page); + searchRef.current = true; + refetch(); + }; + + // 添加数据监听 + useEffect(() => { + if (data && searchRef.current) { + searchRef.current = false; + } + }, [data]); + + // 处理删除的函数 + const handleDelete = async (id: string) => { + Modal.confirm({ + title: '确认删除', + content: '确定要删除这条记录吗?', + onOk: async () => { + try { + await deleteMutation.mutateAsync({ + ids: [id], + data: {} // 添加空的 data 对象 + }); + } catch (error) { + console.error('删除失败:', error); + } + } + }); + }; + + const handleEdit = (staff: Staff) => { + setEditingStaff(staff); + form.setFieldsValue(staff); + setIsModalVisible(true); + }; + + const handleAdd = () => { + setEditingStaff(null); + form.resetFields(); + setIsModalVisible(true); + }; + + const handleModalOk = () => { + form.validateFields().then(values => { + if (editingStaff) { + updateMutation.mutate({ + where: { id: editingStaff.id }, + data: values + }); + } else { + createMutation.mutate({ + data: values + }); + } + }); }; return (

培训情况记录

+ + {/* 搜索和添加按钮 */} +
+ + setSearchText(e.target.value)} + onPressEnter={handleSearch} + prefix={} + /> + + + +
+ + {/* 修改表格,添加操作列 */}
- - - + + + + - {staffs.map((staff) => ( + {(data?.items || []).map((staff) => ( - + + ))}
培训记录ID员工ID在位用户名名称是否在位 + 操作 +
- {staff.id} - {staff.username} + {staff.showname} + {staff.absent ? '在位' : '不在位'} + + + + +
+ {/* 编辑/添加模态框 */} + setIsModalVisible(false)} + > +
+ + + + + + + + + +
+
+
- `共 ${total} 条记录`}