123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Button, Popconfirm, Modal, Form, Input, message } from 'antd';
  3. import { PlusOutlined } from '@ant-design/icons';
  4. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  5. import moment from 'moment';
  6. import PageTable from '@/components/PageTable';
  7. import { getNewsTypeList, addNewsType, deleteNewsType, updateNewsType } from '@/services/newsType';
  8. const formatterTime = (val) => {
  9. return val && val !== '-' ? moment(val).format('YYYY-MM-DD') : '-';
  10. };
  11. const FormItem = Form.Item;
  12. export default (props) => {
  13. const formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 14 } };
  14. const [form] = Form.useForm();
  15. const [editModal, setEditModal] = useState(false);
  16. const [loading, setLoading] = useState(false);
  17. const [typeId, setTypeId] = useState();
  18. const actionRef = useRef();
  19. const Submit = (values) => {
  20. setLoading(true);
  21. if (typeId) {
  22. updateNewsType(typeId, values).then(() => {
  23. setLoading(false);
  24. message.success(`修改成功`);
  25. onCancel();
  26. actionRef.current.reload();
  27. });
  28. } else {
  29. addNewsType(values)
  30. .then(() => {
  31. setLoading(false);
  32. message.success(`保存成功`);
  33. onCancel();
  34. actionRef.current.reload();
  35. })
  36. .catch((err) => {
  37. setLoading(false);
  38. message.error(err.message || err);
  39. });
  40. }
  41. };
  42. const handelEdit = (val) => {
  43. setTypeId(val.typeId);
  44. form.setFieldsValue(val);
  45. setEditModal(true);
  46. };
  47. const onCancel = () => {
  48. setTypeId();
  49. form.resetFields();
  50. setEditModal(false);
  51. };
  52. const handleDelete = (id) => {
  53. deleteNewsType(id)
  54. .then(() => {
  55. message.success('删除成功');
  56. actionRef.current.reload();
  57. })
  58. .catch((err) => {
  59. message.error(err);
  60. });
  61. };
  62. useEffect(() => {
  63. if (typeId) {
  64. } else {
  65. form.resetFields();
  66. }
  67. }, [typeId]);
  68. const actions = () => [
  69. <Button key="add" type="primary" icon={<PlusOutlined />} onClick={() => setEditModal(true)}>
  70. 新增
  71. </Button>,
  72. ];
  73. const columns = [
  74. {
  75. title: '分类名',
  76. dataIndex: 'name',
  77. key: 'name',
  78. },
  79. {
  80. title: '创建时间',
  81. dataIndex: 'createDate',
  82. key: 'createDate',
  83. render: formatterTime,
  84. search: false,
  85. width: 240,
  86. },
  87. {
  88. title: '操作',
  89. valueType: 'option',
  90. width: 160,
  91. render: (_, record) => [
  92. <Button type="link" style={{ padding: 0 }} key={1} onClick={() => handelEdit(record)}>
  93. 编辑
  94. </Button>,
  95. <Popconfirm
  96. key={2}
  97. title="您是否确认删除 ?"
  98. onConfirm={() => handleDelete(record.typeId)}
  99. okText="确定"
  100. cancelText="取消"
  101. >
  102. <Button style={{ padding: 0 }} type="link">
  103. 删除
  104. </Button>
  105. </Popconfirm>,
  106. ],
  107. },
  108. ];
  109. return (
  110. <PageHeaderWrapper>
  111. <PageTable
  112. request={getNewsTypeList}
  113. columns={columns}
  114. actionRef={actionRef}
  115. rowKey="typeId"
  116. options={false}
  117. toolBarRender={actions}
  118. scroll={{ x: 1000 }}
  119. />
  120. <Modal
  121. forceRender
  122. title={typeId ? '分类编辑' : '分类新增'}
  123. visible={editModal}
  124. onCancel={onCancel}
  125. keyboard={false}
  126. maskClosable={false}
  127. destroyOnClose={true}
  128. footer={null}
  129. >
  130. <Form {...formItemLayout} onFinish={Submit} form={form}>
  131. <FormItem label="分类名" name="name" rules={[{ required: true, message: '请输入' }]}>
  132. <Input placeholder="请输入" />
  133. </FormItem>
  134. <FormItem label=" " colon={false}>
  135. <Button type="default" onClick={onCancel}>
  136. 取消
  137. </Button>
  138. <Button
  139. type="primary"
  140. loading={loading}
  141. htmlType="Submit"
  142. style={{ marginLeft: '4em' }}
  143. >
  144. 确认
  145. </Button>
  146. </FormItem>
  147. </Form>
  148. </Modal>
  149. </PageHeaderWrapper>
  150. );
  151. };