JobItems.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* eslint-disable jsx-a11y/anchor-is-valid */
  2. import { Button, Form, Input, message, Popconfirm, Table } from 'antd';
  3. import React, { useContext, useEffect, useRef, useState, props, useRoutes } from 'react';
  4. import styled from 'styled-components'
  5. import { Link } from 'react-router-dom';
  6. import bus from '../../../bus.js'
  7. import { getJobDataList, deleteJob, getJobInfo } from '../services'
  8. import {routes} from '../../../routes/index.js'
  9. const EditableContext = React.createContext(null);
  10. const FormItem = Form.Item
  11. const JobItems = styled.div`
  12. background-color: #FFFFFF;
  13. height: 45vw;
  14. margin-left: 24px;
  15. .delButton {
  16. margin-left: 40px;
  17. }
  18. .ant-table-wrapper {
  19. margin-right: 12px;
  20. .ant-table {
  21. margin-left: 16px;
  22. }
  23. }
  24. `
  25. const EditableRow = ({ index, ...props }) => {
  26. const [form] = Form.useForm();
  27. return (
  28. <Form form={form} component={false}>
  29. <EditableContext.Provider value={form}>
  30. <tr {...props} />
  31. </EditableContext.Provider>
  32. </Form>
  33. );
  34. };
  35. const EditableCell = ({
  36. title,
  37. editable,
  38. children,
  39. dataIndex,
  40. record,
  41. handleSave,
  42. ...restProps
  43. }) => {
  44. const [editing, setEditing] = useState(false);
  45. const inputRef = useRef(null);
  46. const form = useContext(EditableContext);
  47. useEffect(() => {
  48. if (editing) {
  49. inputRef.current.focus();
  50. }
  51. }, [editing]);
  52. const toggleEdit = () => {
  53. setEditing(!editing);
  54. form.setFieldsValue({
  55. [dataIndex]: record[dataIndex],
  56. });
  57. };
  58. const save = async () => {
  59. try {
  60. const values = await form.validateFields();
  61. toggleEdit();
  62. handleSave({ ...record, ...values });
  63. } catch (errInfo) {
  64. console.log('Save failed:', errInfo);
  65. }
  66. };
  67. let childNode = children;
  68. if (editable) {
  69. childNode = editing ? (
  70. <FormItem
  71. style={{
  72. margin: 0,
  73. }}
  74. name={dataIndex}
  75. rules={[
  76. {
  77. required: true,
  78. message: `${title} is required.`,
  79. },
  80. ]}
  81. >
  82. <Input ref={inputRef} onPressEnter={save} onBlur={save} />
  83. </FormItem>
  84. ) : (
  85. <div
  86. className="editable-cell-value-wrap"
  87. style={{
  88. paddingRight: 24,
  89. }}
  90. onClick={toggleEdit}
  91. >
  92. {children}
  93. </div>
  94. );
  95. }
  96. return <td {...restProps}>{childNode}</td>;
  97. };
  98. const App = () => {
  99. //获取作业列表数据
  100. const getJobTables = async () => {
  101. const project_id = 'test'
  102. const { data } = await getJobDataList(project_id)
  103. if (data.code === 200) {
  104. const list = data.data.map(item => {
  105. return {
  106. key: item.id,
  107. name: item.name,
  108. type: item.type,
  109. tag: item.tag,
  110. create_time: item.create_time,
  111. update_time: item.update_time,
  112. }
  113. })
  114. setDataSource(list)
  115. }
  116. }
  117. // componentDidMount
  118. useEffect(() => {
  119. getJobTables()
  120. },[])
  121. const [dataSource, setDataSource] = useState();
  122. // [
  123. // {
  124. // key: '0',
  125. // name: '每日供应商销量预测作业2',
  126. // type: 'java',
  127. // sort: '业务预测',
  128. // creTime: '2022.8.18 12:00:00',
  129. // updTime: '2022.8.18 12:00:00',
  130. // },
  131. // {
  132. // key: '1',
  133. // name: '每日供应商销量预测作业21',
  134. // type: 'java',
  135. // sort: '业务预测',
  136. // creTime: '2022.8.18 12:00:00',
  137. // updTime: '2022.8.18 12:00:00',
  138. // },
  139. // ]
  140. const [count, setCount] = useState(2);
  141. const [JobData, setJobData] =useState()
  142. //删除作业
  143. const handleDelete = async (key) => {
  144. const { data } = await deleteJob(key)
  145. if (data.code === 200) {
  146. const newData = dataSource.filter((item) => item.key !== key);
  147. setDataSource(newData);
  148. message.success('删除成功');
  149. } else {
  150. message.error('删除失败')
  151. }
  152. };
  153. const editJobitem = async(key) => {
  154. const data = await getJobInfo(key)
  155. setJobData(data)
  156. }
  157. // 格式化事件
  158. const formatTime = time => {
  159. const date = new Date(Number(time) * 1000)
  160. const YY = date.getFullYear()
  161. const MM =
  162. date.getMonth() + 1 < 10
  163. ? '0' + (date.getMonth() + 1)
  164. : date.getMonth() + 1
  165. const DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  166. const hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  167. const mm =
  168. date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  169. const ss =
  170. date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  171. return YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss
  172. }
  173. const defaultColumns = [
  174. {
  175. title: '作业名称',
  176. dataIndex: 'name',
  177. width: '16.6%',
  178. },
  179. {
  180. title: '作业类型',
  181. dataIndex: 'type',
  182. width: '16.6%',
  183. },
  184. {
  185. title: '作业分类',
  186. dataIndex: 'tag',
  187. width: '16.6%',
  188. },
  189. {
  190. title: '创建时间',
  191. dataIndex: 'create_time',
  192. width: '16.6%',
  193. render: time => formatTime(time),
  194. },
  195. {
  196. title: '更新时间',
  197. dataIndex: 'update_time',
  198. width: '16.6%',
  199. render: time => formatTime(time),
  200. },
  201. {
  202. title: '操作',
  203. dataIndex: 'operation',
  204. width: '16.6%',
  205. render: (_, record) =>
  206. dataSource.length >= 1 ? (
  207. <>
  208. <Link to={'/work-management/cre?data=' + JobData}>
  209. <Button onClick={() => editJobitem(record.key)}>
  210. 编辑
  211. </Button>
  212. </Link>
  213. <Popconfirm
  214. title="确认删除?"
  215. okText="确认"
  216. cancelText="取消"
  217. onConfirm={() => handleDelete(record.key)}
  218. >
  219. <a className='delButton'>删除</a>
  220. </Popconfirm>
  221. </>
  222. ) : null,
  223. },
  224. ];
  225. //创建作业
  226. const handleSave = (row) => {
  227. const newData = [...dataSource];
  228. const index = newData.findIndex((item) => row.key === item.key);
  229. const item = newData[index];
  230. newData.splice(index, 1, { ...item, ...row });
  231. setDataSource(newData);
  232. };
  233. const components = {
  234. body: {
  235. row: EditableRow,
  236. cell: EditableCell,
  237. },
  238. };
  239. const columns = defaultColumns.map((col) => {
  240. if (!col.editable) {
  241. return col;
  242. }
  243. return {
  244. ...col,
  245. onCell: (record) => ({
  246. record,
  247. editable: col.editable,
  248. dataIndex: col.dataIndex,
  249. title: col.title,
  250. handleSave,
  251. }),
  252. };
  253. });
  254. useEffect (() => {
  255. bus.on('sendDataForm',data => {
  256. })
  257. },[])
  258. return (
  259. <JobItems>
  260. <Link to="/work-management/cre">
  261. <Button
  262. type="primary"
  263. style={{
  264. float: 'right',
  265. marginBottom: 16,
  266. marginTop: '18px',
  267. marginRight: '16px',
  268. fontSize: '12px',
  269. }}
  270. >
  271. 创建作业
  272. </Button>
  273. </Link>
  274. <Table
  275. components={components}
  276. rowClassName={() => 'editable-row'}
  277. bordered
  278. dataSource={dataSource}
  279. columns={columns}
  280. />
  281. </JobItems>
  282. );
  283. };
  284. export default App;