当前位置: 首页 > news >正文

预检查和表单提交

今天在写一个需要预检查的表单提交页面时,我把预检查冗在了提交函数里面导致很耦合且状态不分明,后面我把预检查和提交函数分开,将precheck和submit分开那么形成层级衔接关系而不是覆盖关系,感觉明了不少,同时附着Form上的brules用bv (validation) 检查实现数据层和服务层的双保险

import {BeForm,BeFormItem,BeInput,BeMButton,bv,useForm,
} from '@finfe/beetle-ui';
import { MoneyRead } from '@react/components/Money';
import { Modal } from '@ss/mtd-react3';
import type { NormalListResponseResult } from '@tmc/business';
import { precheckAudit, rejectExpense } from '@tmc/business/src/apis/approve';
import { SupportOperateTypes } from '@tmc/business/src/constance/approve';
import React, { useState } from 'react';interface RejectModalProps {visible: boolean;onClose: () => void;expenses: NormalListResponseResult[];onSuccess?: () => void;
}const RejectModal: React.FC<RejectModalProps> = ({visible,onClose,expenses,onSuccess,
}) => {// 是否有离职员工会导致中间有无警告弹窗过程const [showLeaveWarning, setShowLeaveWarning] = useState(false);const [leaveStaffList, setLeaveStaffList] = useState<any[]>([]);// 预检查是否有离职员工const handlePrecheck = async () => {const expenseNoList = expenses.map((expense) => expense.expenseNo);const precheckResult = await precheckAudit({expenseNoList,operateType: SupportOperateTypes.REJECT,});const failList = Array.isArray(precheckResult?.operateFailList)? precheckResult.operateFailList: [];if (failList.length > 0) {setLeaveStaffList(failList);setShowLeaveWarning(true);return;} else {form.submit();}};const handleSubmit = async (value: string) => {const expenseDetails = expenses.map((expense) => ({expenseNo: expense.expenseNo,expenseTagList: expense.expenseTagList?.map((tag) => ({tagCode: tag.tagCode,tagValue: 1,})),}));await rejectExpense(expenseDetails, value);Modal.success({title: '成功',message: `成功驳回 ${expenses.length} 个费用申请`,});setShowLeaveWarning(false);setLeaveStaffList([]);onClose();onSuccess?.();};const handleCancel = () => {setShowLeaveWarning(false);setLeaveStaffList([]);onClose();};const form = useForm({onSubmit: (values) => {return handleSubmit(values.comment); // 表单数据托管给form了所以绑定的肯定是submit而不是预检查},});return (<ModalonClose={handleCancel}open={visible}style={{ width: '500px' }}title={showLeaveWarning ? '离职人员提醒' : '驳回申请'}><Modal.Body>{showLeaveWarning ? (// 离职人员警告界面<div><div className="mb-4 text-orange-500">以下人员已离职,是否继续驳回?</div><div className="max-h-[120px] overflow-auto border border-yellow-300 rounded p-2 bg-yellow-50 mb-6">{leaveStaffList.map((staff, index) => (<divclassName={`text-sm text-orange-600 ${index === leaveStaffList.length - 1 ? '' : 'mb-1'}`}key={index}>{staff.staffName} - {staff.reason}</div>))}</div></div>) : (// 正常驳回界面<div><div className="mb-4"><div className="mb-2 text-gray-600 text-sm">将要驳回以下 {expenses.length} 个费用申请:</div><div className="max-h-[120px] overflow-auto border border-gray-300 rounded p-2 bg-gray-50">{expenses.map((expense, index) => (<divclassName={`text-xs text-gray-600 flex items-center gap-1 ${index === expenses.length - 1 ? '' : 'mb-1'}`}key={expense.expenseNo}><span>{expense.expenseNo} - {expense.proposerStaffName} -</span><MoneyReadcurrency={expense.expenseCurrency || 'CNY'}value={expense.expenseTotalAmount}/></div>))}</div></div><BeForm form={form}><BeFormItembrules={bv.required('请输入驳回原因')}fieldName="comment"label="驳回原因"><BeInput /></BeFormItem></BeForm></div>)}</Modal.Body><Modal.Footer>{showLeaveWarning ? (<><BeMButtononClick={() => setShowLeaveWarning(false)}style={{ marginRight: '12px' }}>返回</BeMButton><BeMButton onClick={() => form.submit()} type="primary">继续驳回</BeMButton></>) : (<><BeMButton onClick={handleCancel} style={{ marginRight: '12px' }}>取消</BeMButton><BeMButton onClick={handlePrecheck} type="primary">驳回</BeMButton></>)}</Modal.Footer></Modal>);
};export default RejectModal;

http://www.dtcms.com/a/520598.html

相关文章:

  • [MySQL] 数据库设计
  • 数据处理像搭乐高?详解 RAGFlow Ingestion Pipeline
  • 模仿网站属于侵权吗做网站视频是什么专业
  • 汽车转向控制 / 线控转向介绍——控制技术——智能驾驶专栏
  • 基于KSP密钥管理系统的汽车 ECU JTAG 调试接口动态授权与安全管控
  • 世界经理人网站手机版学生网页网站制作软件大全
  • 企业网站带新闻发布功能的建站wordpress文章显示颜色
  • SpringBoot-Web开发之Web原生组件注入
  • Spring Cloud - Spring Cloud 本地配置(本地配置概述、本地配置实现)
  • 青岛商城网站建设长春建设平台网站的公司
  • 这几年做网站怎么样wordpress adsense主题
  • 做视频网站服务器怎么选择12306网站开发时间
  • 如何做家教网站赚钱如何申请国外网站
  • 淄博做网站建设云南网站建设方案
  • 实用指南--多账号矩阵防封控
  • 前端框架Vue(Vue 的挂载点与 data 数据对象)
  • pycharm 远程连接服务器添加github copilot
  • 金融智能体的技术底座解析:AI Agent如何实现“认知+执行”闭环?
  • PostgreSQL 选择数据库:深入解析与最佳实践
  • 木材模板.网站黄山网站建设推广
  • 临沧永德网站建设电子商务公司个人博客网站制作论文
  • MOshell (13) :运维自动化与参数一致性管理
  • CS224N-Lecture01-Word Vectors
  • vscode 配置ROS项目 导入库来源
  • 探寻架构师职责(一)----建新系统
  • 广州seo效果下载班级优化大师app
  • 兴城做网站推广的做网商必备网站
  • Windows系统php8.2.9 安装imagick扩展
  • 如何用router-view实现嵌套路由,完整使用示例
  • 深度学习 GPU 两种使用方法