import { forwardRef, useImperativeHandle, useState, useRef } from "react"; import { Modal, Form, Input, Select, Button, message, Space } from "antd"; import { useMutation, useQuery } from "@tanstack/react-query"; import { CreateVqdTask, UpdateVqdTask } from "../api/vqdtask"; import { GetVqdTaskTemplate } from "../api/vqdtasktemplate"; import type { CreateVqdTaskReq, VqdTaskItem } from "../types/vqdtask"; import { useGlobal } from "../Context"; import ChannelModel, { IChannelModelFunc } from "./channel/Channel"; interface AddVqdTaskProps { title: string; onSuccess: () => void; } export interface AddVqdTaskRef { open: (task?: VqdTaskItem) => void; } const AddVqdTask = forwardRef( ({ title, onSuccess }, ref) => { const [open, setOpen] = useState(false); const [editing, setEditing] = useState(false); const [channelId, setChannelId] = useState(""); const channelRef = useRef(null); const [form] = Form.useForm(); const { ErrorHandle } = useGlobal(); useImperativeHandle(ref, () => ({ open: (task?: VqdTaskItem) => { if (task) { setEditing(true); setChannelId(task.channel_id) const formValues = { name: task.name, id: task.id, channel_id: task.channel_id, channel_name: task.channel_name, task_template_id: task.task_template_id, task_template_name: task.task_template_name, enable: task.enable, }; form.setFieldsValue(formValues); } else { setEditing(false); form.resetFields(); // form.setFieldsValue({ // bid: "2", // }); } setOpen(true); }, })); const [pagination, setPagination] = useState({ page: 1, size: 999, name: "" }); // 获取任务列表 const { data: storageResponse, isLoading, refetch, } = useQuery({ queryKey: ["storage", pagination], queryFn: () => GetVqdTaskTemplate({ ...pagination }) .then((res) => res.data) .catch((err) => { ErrorHandle(err); throw err; }), // refetchInterval: 4000, retry: 1, }); const { mutate: createMutate, isPending: creating } = useMutation({ mutationFn: CreateVqdTask, onSuccess: () => { message.success("创建任务成功"); handleClose(); onSuccess?.(); }, onError: ErrorHandle, }); const { mutate: updateMutate, isPending: updating } = useMutation({ mutationFn: UpdateVqdTask, onSuccess: () => { message.success("更新任务成功"); handleClose(); onSuccess?.(); }, onError: ErrorHandle, }); const handleClose = () => { setOpen(false); setEditing(false); setChannelId(""); form.resetFields(); }; return ( form.submit()} confirmLoading={creating || updating} >
{ if (creating || updating) return console.log(values); const { name, des, channel_id, channel_name, task_template_id, task_template_name, enable } = values as { name: string; des: string; id?: number; channel_id: string; channel_name: string; task_template_id: number; task_template_name: string; enable: boolean; }; const payload: CreateVqdTaskReq = { name, des, channel_id, channel_name, task_template_id, task_template_name, enable, }; if (editing) { const id = (values as any).id; updateMutate({ id: String(id), ...payload }); } else { createMutate(payload); } }} > {editing && ( )}
{ form.setFieldsValue({ channel_id: id, channel_name: name }); setChannelId(id) }} />
); } ); export default AddVqdTask;