TaskForm.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import React, { useState, useEffect, useRef, forwardRef } from 'react'
  2. import styled from 'styled-components'
  3. import {
  4. Form,
  5. Select,
  6. Input,
  7. Radio,
  8. Space,
  9. message,
  10. Divider,
  11. Button,
  12. } from 'antd'
  13. import { PlusOutlined, DeleteOutlined } from '@ant-design/icons'
  14. import { getTagList, deleteTag } from '../services'
  15. import CronSelect from '../../../component/CronSelect'
  16. const FormWrapper = styled.div`
  17. .singletask_form {
  18. margin-top: 20px;
  19. }
  20. .form_executionCycle {
  21. margin-left: 10px;
  22. }
  23. `
  24. const FormItem = Form.Item
  25. const { Option } = Select
  26. const RadioGroup = Radio.Group
  27. const layout = {
  28. labelCol: {
  29. span: 2,
  30. },
  31. wrapperCol: {
  32. span: 10,
  33. },
  34. }
  35. const TaskForm = ({ taskForm, taskType, taskInfo, jmWorkList }, ref) => {
  36. const [radioValue, setRadioValue] = useState(null)
  37. const [tagOptions, setTagOptions] = useState([])
  38. const [jmWorkOptions, setJmWorkOptions] = useState([])
  39. const [tag, setTag] = useState(null)
  40. const [cronData, setCronData] = useState(null)
  41. const inputRef = useRef(null)
  42. const onNameChange = event => {
  43. setTag(event.target.value)
  44. }
  45. const addTagItem = e => {
  46. if (tag) {
  47. e.preventDefault()
  48. if (!tagOptions.find(item => item === tag)) {
  49. setTagOptions([...tagOptions, tag])
  50. taskForm.setFieldValue('taskTag', tag)
  51. setTag(null)
  52. setTimeout(() => {
  53. inputRef.current?.focus()
  54. }, 0)
  55. } else {
  56. message.error('存在同名标签')
  57. }
  58. } else {
  59. message.error('请输入自定义作业标签名称')
  60. }
  61. }
  62. const onDeleteTag = async val => {
  63. const { data } = await deleteTag({ type: '任务标签', val })
  64. if (data.code === 200) {
  65. taskForm.setFieldValue('taskTag', null)
  66. message.success('删除成功')
  67. fetchTagList()
  68. } else {
  69. message.error(data.msg)
  70. }
  71. }
  72. const onRadioChange = e => {
  73. setRadioValue(e.target.value)
  74. }
  75. const fetchTagList = async () => {
  76. const { data } = await getTagList('任务标签')
  77. if (data.code === 200) {
  78. setTagOptions(data.data)
  79. } else {
  80. message.error(data.msg)
  81. }
  82. }
  83. const handleInitTask = taskInfo => {
  84. setRadioValue(taskInfo.cron_type)
  85. if (taskInfo.cron_type === 2) {
  86. setCronData(taskInfo.cron_expression_dict)
  87. }
  88. taskForm.setFieldsValue({
  89. taskName: taskInfo.name,
  90. taskTag: taskInfo.tag,
  91. job: taskInfo.nodes[0].homework_id,
  92. executionCycle: taskInfo.cron_type,
  93. })
  94. }
  95. //初始化tag列表
  96. useEffect(() => {
  97. fetchTagList()
  98. }, [])
  99. //初始化作业列表
  100. useEffect(() => {
  101. setJmWorkOptions(jmWorkList)
  102. }, [jmWorkList])
  103. useEffect(() => {
  104. if (taskInfo !== null) handleInitTask(taskInfo)
  105. }, [taskInfo])
  106. return (
  107. <FormWrapper>
  108. <Form {...layout} form={taskForm} className="singletask_form">
  109. <FormItem
  110. name="taskName"
  111. label="任务名称"
  112. rules={[{ required: true, message: '请输入任务名称!' }]}>
  113. <Input />
  114. </FormItem>
  115. <FormItem
  116. name="taskTag"
  117. label="任务标签"
  118. rules={[{ required: true, message: '请选择任务标签!' }]}>
  119. <Select
  120. placeholder="选择任务标签"
  121. allowClear
  122. optionLabelProp="label"
  123. dropdownRender={menu => (
  124. <>
  125. {menu}
  126. <Divider
  127. style={{
  128. margin: '8px 0',
  129. }}
  130. />
  131. <Space
  132. style={{
  133. padding: '0 8px 4px',
  134. }}>
  135. <Input
  136. placeholder="请输入"
  137. ref={inputRef}
  138. value={tag}
  139. onChange={onNameChange}
  140. />
  141. <Button
  142. type="text"
  143. icon={<PlusOutlined />}
  144. onClick={addTagItem}>
  145. 添加新分类
  146. </Button>
  147. </Space>
  148. </>
  149. )}>
  150. {tagOptions.map(item => (
  151. <Option key={item}>
  152. <Space>
  153. <DeleteOutlined onClick={() => onDeleteTag(item)} />
  154. <span>{item}</span>
  155. </Space>
  156. </Option>
  157. ))}
  158. </Select>
  159. </FormItem>
  160. {taskType !== 'multitasking' && (
  161. <FormItem
  162. name="job"
  163. label="选择作业"
  164. rules={[{ required: true, message: '请选择作业!' }]}>
  165. <Select placeholder="选择作业" allowClear>
  166. {jmWorkOptions.map(item => (
  167. <Option key={item.id} value={item.id}>
  168. {item.name}
  169. </Option>
  170. ))}
  171. </Select>
  172. </FormItem>
  173. )}
  174. <FormItem
  175. name="executionCycle"
  176. label="设置执行周期"
  177. rules={[{ required: true, message: '请设置执行周期!' }]}>
  178. <RadioGroup onChange={onRadioChange}>
  179. <Space direction="vertical">
  180. <Radio value={1}>只执行一次,下次手动执行</Radio>
  181. <Radio value={2}>
  182. 执行周期
  183. {radioValue === 2 ? (
  184. <Space className="form_executionCycle">
  185. <CronSelect cron_data={cronData} ref={ref} />
  186. </Space>
  187. ) : null}
  188. </Radio>
  189. </Space>
  190. </RadioGroup>
  191. </FormItem>
  192. </Form>
  193. </FormWrapper>
  194. )
  195. }
  196. export default forwardRef(TaskForm)